MyBatis(十):MyBatis延迟加载/懒加载详解
- 延迟加载介绍 MyBatis中的延迟加载,也成为懒加载,是指在进行关联查询时,按照设置的延迟规则推迟对关联对象的查询。延迟加载可以有效的减少数据库的压力。延迟加载只是针对有延迟设置的关联对象的推迟查询,对于主主查询是直接进行执行SQL语句。
- MyBatis关联查询加载时机 直接加载:执行完主对象的查询后,马上执行对关联对象的查询语句 侵入式延迟:执行完对主对象对查询后,不会执行对关联对象的查询,但当访问主对象的属性详情是,就会执行关联对象的查询 深度延迟:只有当真正访问关联对象的详情时,才会执行查询语句
- MyBatis延迟加载实现步骤 全局延迟 在MyBatis核心配置类中添加标签 <settings> <!-- 延迟加载总开关 --> <setting name="lazyLoadingEnabled" value="true"/> <!-- 侵入式延迟加载开关 --> <setting name="aggressiveLazyLoading" value="true"/> </settings> 部分延迟 在关联查询collection、association标签上添加 fetchType 属性,lazy表示延迟加载,eager表示立即加载,指定属性后,将在映射中忽略全局配置参数 lazyLoadingEnabled,使用属性的 <resultMap id="userMap" type="com.rangers.entity.User"> <result column="id" property="id"></result> <result column="name" property="name"></result> <result column="address" property="address"></result> <collection property="orderList" column="id" ofType="com.rangers.entity.Order" select="com.rangers.dao.OrderMapper.findOrderById" fetchType="lazy"> </collection> </resultMap> <select id="findUserById" resultMap="userMap"> select * from user where id=#{id} </select> <select id="findOrderById" parameterType="int" resultType="com.rangers.entity.Order"> select * from `order` where uid=#{id} </select>
- 注意点 在延迟加载的测试过程中,有发现延迟加载未生效问题,就是在用户表查询后,订单信息也有值。 经过探索发现,在debug模式下查看变量时,debug会另起一个线程,然后重新调用代码,debug弹出框显示用户信息时,会自动调用User类的hashCode()与toString()方法。 解决一:不进行debug打断电,在查询后直接打印一段分割线,查看控制台信息 @org.junit.Test public void testResult(){ User user = userMapper.findUserById(1); System.out.println("------------------查询OrderList分割线------------------"); System.out.println(user.getOrderList()); } 控制台显示 17:33:31,229 DEBUG findUserById:159 - ==> Preparing: select * from user where id=? 17:33:31,253 DEBUG findUserById:159 - ==> Parameters: 1(Integer) 17:33:31,283 DEBUG findUserById:159 - <== Total: 1 ------------------查询OrderList分割线------------------ 17:33:31,284 DEBUG findOrderById:159 - ==> Preparing: select * from `order` where uid=? 17:33:31,284 DEBUG findOrderById:159 - ==> Parameters: 1(Integer) 17:33:31,290 DEBUG findOrderById:159 - <== Total: 2 [Order{id=1, time=Wed Mar 10 17:22:30 CST 2021, total=1.11, uid=1, flag=0}, Order{id=2, time=Wed Mar 10 17:22:45 CST 2021, total=2.22, uid=1, flag=1}] 解决二:在MyBatis核心配置文件中添加setting标签,指定lazyLoadTriggerMethods属性为空 <setting name="lazyLoadTriggerMethods" value=""/> lazyLoadTriggerMethods:指定对象的方法触发一次延迟加载。 默认值:equals() clone() hashCode() ) toString()
