mybatis SQL调优 延迟加载

链接:https://pan.baidu.com/s/1V5wvZPPwo1j6drgKEcvmeg 
提取码:a55j 
附带数据库

在一些多表连接的查询中,只要用到主表数据,不需要使用关联表数据的时候不查询关联表,就是延迟加载 。

 

mybatis延迟加载配置:

1.在主配置文件中设置两个setting

settings>
    
        
        
        
        
    

lazyLoadingEnabled需要为true(默认true),表示开启延迟加载

aggressiveLazyLoading需要为false(默认为true) 表示取消积极加载策略

(积极加载策略的意思就是当前的对象关联的属性也会立即加载)

2.在关联查询的xml或者注解中配置Fetch=lazy

//查询学生信息,同时获取其所有成绩信息
    // fetchType = FetchType.LAZY  延迟加载设置 默认值为 EAGER
    @Select("select * from tb_student")
    @Results({

            @Result(column = "stu_id",property = "stuId"),
            @Result(column = "stu_id",property = "results",
                    many = @Many(select = "cn.cgr.dao.TbResultMapper.selectBySid",fetchType = FetchType.LAZY))
    })
    List selectAll();

 

学生于课程表关联  一对多

fetchType = FetchType.LAZY 
或者 

设置其中一个即可

测试   启用

 @Test
    public void testSelectAll(){
        List students = studentMapper.selectAll();
        //如果启用懒加载 只有在启用到关联表数据时才执行sql
        System.out.println(students.get(0).getResults());
//        System.out.println(students);
//        for (Student student : students) {
//            System.out.println(student);
//            System.out.println(student.getResults());
//        }
    }

效果

"C:\Program Files\jdk-9.0.1\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA 2019.1.1\lib\idea_rt.jar=53824:D:\IntelliJ IDEA 2019.1.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA 2019.1.1\lib\idea_rt.jar;D:\IntelliJ IDEA 2019.1.1\plugins\junit\lib\junit-rt.jar;D:\IntelliJ IDEA 2019.1.1\plugins\junit\lib\junit5-rt.jar;D:\老梁文件\1908\day60\代码\demo_otm_lazy\target\test-classes;D:\老梁文件\1908\day60\代码\demo_otm_lazy\target\classes;D:\ktc_reponsitory\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar;D:\ktc_reponsitory\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar;D:\ktc_reponsitory\junit\junit\4.12\junit-4.12.jar;D:\ktc_reponsitory\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\ktc_reponsitory\log4j\log4j\1.2.17\log4j-1.2.17.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 TestMybatis,testSelectAll
DEBUG - ==>  Preparing: select * from tb_student 
DEBUG - ==> Parameters: 
TRACE - <==    Columns: stu_id, stu_name, stu_age, stu_password
TRACE - <==        Row: 1001, 鸠摩智, 47, 123456
TRACE - <==        Row: 1003, 虚竹, 21, 123
TRACE - <==        Row: 1005, 王语嫣, 17, 123
TRACE - <==        Row: 1006, 王语嫣, 17, 123
TRACE - <==        Row: 1007, 王语嫣, 17, 123
TRACE - <==        Row: 1017, 萧峰, 30, 123
TRACE - <==        Row: 1021, 萧峰, 30, null
TRACE - <==        Row: 1022, 虚竹, 20, null
TRACE - <==        Row: 1023, 虚竹, 18, null
TRACE - <==        Row: 1024, 段誉, 18, null
TRACE - <==        Row: 1031, stu_4, 30, 123
TRACE - <==        Row: 1032, stu_5, 30, 123
TRACE - <==        Row: 1033, stu_1, 30, 123
TRACE - <==        Row: 1034, stu_2, 30, 123
TRACE - <==        Row: 1035, stu_3, 30, 123
TRACE - <==        Row: 1036, stu_4, 30, 123
TRACE - <==        Row: 1037, stu_5, 30, 123
TRACE - <==        Row: 1038, stu_1, 30, 123
TRACE - <==        Row: 1039, stu_2, 30, 123
TRACE - <==        Row: 1040, stu_3, 30, 123
TRACE - <==        Row: 1041, stu_4, 30, 123
TRACE - <==        Row: 1042, stu_5, 30, 123
DEBUG - <==      Total: 22
DEBUG - ==>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ==> Parameters: 1001(Long)
TRACE - <==    Columns: res_id, res_subject, res_score, stu_id
TRACE - <==        Row: 1, 语文, 90, 1001
TRACE - <==        Row: 2, 数学, 56, 1001
TRACE - <==        Row: 5, 英语, 66, 1001
DEBUG - <==      Total: 3
[TbResult{resId=1, resSubject='语文', resScore=90.0, stuId=1001}, TbResult{resId=2, resSubject='数学', resScore=56.0, stuId=1001}, TbResult{resId=5, resSubject='英语', resScore=66.0, stuId=1001}]

Process finished with exit code 0

 

不启用即

fetchType = FetchType.EAGER

可以不设置默认就是

测试效果

"C:\Program Files\jdk-9.0.1\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJ IDEA 2019.1.1\lib\idea_rt.jar=54284:D:\IntelliJ IDEA 2019.1.1\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJ IDEA 2019.1.1\lib\idea_rt.jar;D:\IntelliJ IDEA 2019.1.1\plugins\junit\lib\junit-rt.jar;D:\IntelliJ IDEA 2019.1.1\plugins\junit\lib\junit5-rt.jar;D:\老梁文件\1908\day60\代码\demo_otm_lazy\target\test-classes;D:\老梁文件\1908\day60\代码\demo_otm_lazy\target\classes;D:\ktc_reponsitory\mysql\mysql-connector-java\5.1.35\mysql-connector-java-5.1.35.jar;D:\ktc_reponsitory\org\mybatis\mybatis\3.5.2\mybatis-3.5.2.jar;D:\ktc_reponsitory\junit\junit\4.12\junit-4.12.jar;D:\ktc_reponsitory\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;D:\ktc_reponsitory\log4j\log4j\1.2.17\log4j-1.2.17.jar" com.intellij.rt.execution.junit.JUnitStarter -ideVersion5 -junit4 TestMybatis,testSelectAll
DEBUG - ==>  Preparing: select * from tb_student 
DEBUG - ==> Parameters: 
TRACE - <==    Columns: stu_id, stu_name, stu_age, stu_password
TRACE - <==        Row: 1001, 鸠摩智, 47, 123456
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1001(Long)
TRACE - <====    Columns: res_id, res_subject, res_score, stu_id
TRACE - <====        Row: 1, 语文, 90, 1001
TRACE - <====        Row: 2, 数学, 56, 1001
TRACE - <====        Row: 5, 英语, 66, 1001
DEBUG - <====      Total: 3
TRACE - <==        Row: 1003, 虚竹, 21, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1003(Long)
TRACE - <====    Columns: res_id, res_subject, res_score, stu_id
TRACE - <====        Row: 3, 语文, 77, 1003
TRACE - <====        Row: 4, 数学, 89, 1003
DEBUG - <====      Total: 2
TRACE - <==        Row: 1005, 王语嫣, 17, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1005(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1006, 王语嫣, 17, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1006(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1007, 王语嫣, 17, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1007(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1017, 萧峰, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1017(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1021, 萧峰, 30, null
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1021(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1022, 虚竹, 20, null
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1022(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1023, 虚竹, 18, null
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1023(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1024, 段誉, 18, null
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1024(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1031, stu_4, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1031(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1032, stu_5, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1032(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1033, stu_1, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1033(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1034, stu_2, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1034(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1035, stu_3, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1035(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1036, stu_4, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1036(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1037, stu_5, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1037(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1038, stu_1, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1038(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1039, stu_2, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1039(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1040, stu_3, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1040(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1041, stu_4, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1041(Long)
DEBUG - <====      Total: 0
TRACE - <==        Row: 1042, stu_5, 30, 123
DEBUG - ====>  Preparing: select * from tb_result where stu_id=? 
DEBUG - ====> Parameters: 1042(Long)
DEBUG - <====      Total: 0
DEBUG - <==      Total: 22
[TbResult{resId=1, resSubject='语文', resScore=90.0, stuId=1001}, TbResult{resId=2, resSubject='数学', resScore=56.0, stuId=1001}, TbResult{resId=5, resSubject='英语', resScore=66.0, stuId=1001}]

Process finished with exit code 0

 

你可能感兴趣的:(mybatis SQL调优 延迟加载)