2021-01-18

1.mybatis延迟加载策略

什么是延迟加载?

延迟加载就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载. 好处: 先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。

坏处: 因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗 时间,所以可能造成用户等待时间变长,造成用户体验下降。

开启懒加载

    <settings>
        <!-- 开启全局懒加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
       
    </settings>

 

association延迟加载

1.在IScoreDao接口中增加

  List<Score> findAllScoreWithStudentLazy();

2.在IScoreDao.xml 中增加

​
    <!-- 定义封装StudentScore和user的resultMap -->
    <resultMap id="findAllScoreWithStudentLazyMap" type="Score">
        <id property="scoreid" column="scoreid"></id>
        <result property="coursename" column="coursename"></result>
        <result property="score" column="score"></result>
​
        <!-- 一对一的关系映射:配置封装Student的内容-->
        <association property="student"  javaType="Student"
                     select="com.wgz.dao.IStudentDao.findStudentById" column="studentid" fetchType="lazy">
        </association>
    </resultMap>
​
​
    <select id="findAllScoreWithStudentLazy" resultMap="findAllScoreWithStudentLazyMap">
            select * from score_tb;
    </select>

select:为我们调用其他映射的id

column:为传递的参数

javaType:为查询到的java 数据类型

fetchType:是否使用懒加载,如果不设置与全局设置保持一致

3.测试

  IScoreDao scoreDao = sqlSession.getMapper(IScoreDao.class);
​
            List<Score> scoreList =  scoreDao.findAllScoreWithStudentLazy();
            for (Score score:scoreList){
                // 不调用学生相关信息,不请求查数据库
                System.out.println("score:"+score.getScore());
                // 调用学生信息,发起二次请求查询学生相关的数据库
                //System.out.println("student:"+score.getStudent());
            }

collection延迟加载

1.在IStudentDao增加

     /**
      * 延迟加载学生的成绩
      * @return
      */
     List<Student>  findAllStudentWithScoreListLazy();

2.在IStudentDao.xml 中增加

  <!--
        一对多懒加载
    -->
    <resultMap id="findAllStudentWithScoreListLazyMap" type="Student">
        <id property="id" column="id"></id>
        <result property="name" column="name"></result>
        <result property="sex" column="name"></result>
        <result property="age" column="age"></result>
        <result property="height" column="height"></result>
        <result property="birthday" column="birthday"></result>
​
​
        <collection property="scoreList" column="id" select="com.wgz.dao.IScoreDao.findScoreByStudentId" fetchType="lazy">
        </collection>
​
    </resultMap>
​
    <select id="findAllStudentWithScoreListLazy" resultMap="findAllStudentWithScoreListLazyMap">
                select * from student_tb
    </select>
​

3.测试

​
            // 方式一 通过代理:
            IStudentDao studentDao = sqlSession.getMapper(IStudentDao.class);
​
            List<Student> studentList =  studentDao.findAllStudentWithScoreListLazy();
            for (Student student:studentList){
                // 不调用学生成绩相关信息,不请求查数据库
                System.out.println("Student:"+student.getName());
                // 调用学生成绩信息,发起二次请求查询学生成绩相关的数据库
                System.out.println("ScoreList:"+student.getScoreList());
            }
            

注意

默认情况mybatis开延迟加载只要调用toString()方法包括Object的object的都会触发懒加载可以通过设置一下解决

        <!--解决 懒加载时 打印对象toString 触发 懒加载
            lazyLoadTriggerMethods:指定哪个对象的方法触发一次延迟加载。默认值:equals,clone,hashCode,toString
        -->
        <setting name="lazyLoadTriggerMethods" value="false"/>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值