Mybatis学习第25节 -- 懒加载 积极与不积极

Posted 积水成渊

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Mybatis学习第25节 -- 懒加载 积极与不积极相关的知识,希望对你有一定的参考价值。

积极懒加载是指如果你访问一个对象的属性,Mybatis就会帮你把需要进步一查询的该属性或者其他属性在数据库中查询出来.
不积极懒加载是指,有这种必要的时候,采取进行必要的数据库检索
我看的教程中的Mybatis版本默认是积极的lazy加载, 而我实际用的Mybatis3.4.6实际上默认不积极的lazy加载.
 
mybatis-config.xml文件
<settings>
<!-- 打印查询语句 -->
<setting name="logImpl" value="LOG4J" />
<setting name="useGeneratedKeys" value="true" />
<!-- 懒加载开关 -->
<setting name="lazyLoadingEnabled" value="true"/>
<!--积极懒加载启动-->
<setting name="aggressiveLazyLoading" value="true"/>
</settings>
 
测试用例
// 测试懒加载
@Test
public void testGetShopByIdLazy() {
SqlSession session = MyBatisUtil.getSqlSession();
ShopMapper mapper = session.getMapper(ShopMapper.class);
Shop shop = mapper.getShopById(29);

session.close();
System.out.printf("懒加载查询结束%n");

System.out.println(shop.getName());
System.out.println("名称查询结束, 开始查询Area属性");
System.out.println(shop.getArea());
}
测试积极懒加载

<!--积极懒加载启动-->
<setting name="aggressiveLazyLoading" value="true"/>

测试结果

在这里我们访问shop.name的时候, 就进行了shop.area的查询, 事实上我们不是很必要进行这一步, 所以说它是积极的
2018-12-29 19:24:42,837 - io.github.coinsjack.dao.ShopMapper.getShopById -1739 [main] DEBUG io.github.coinsjack.dao.ShopMapper.getShopById - ==> Preparing: select * from tb_shop where `shop_id` = ? 
...
懒加载查询结束

...io.github.coinsjack.dao.ProductMapper.getProductListByShopID - ==> Preparing: select * from tb_product WHERE `shop_id` = ?; 
2018-12-29 19:24:43,317 - io.github.coinsjack.dao.ProductMapper.getProductListByShopID -2219 [main] DEBUG io.github.coinsjack.dao.ProductMapper.getProductListByShopID - ==> Parameters: 29(Integer)
2018-12-29 19:24:43,327 - io.github.coinsjack.dao.ProductMapper.getProductListByShopID -2229 [main] DEBUG io.github.coinsjack.dao.ProductMapper.getProductListByShopID - <== Total: 6
...
暴漫奶茶店
名称查询结束, 开始查询Area属性
Area{id=3, name=‘西苑‘, priority=2, createTime=null, lastEditTime=null}

测试不积极懒加载

<setting name="aggressiveLazyLoading" value="false"/>

测试结果

知道访问shop.area的时候, 才进行数据库检索
...
2018-12-29 19:33:02,059 - org.apache.ibatis.datasource.pooled.PooledDataSource -4007 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 503938393 to pool.
懒加载查询结束
暴漫奶茶店
名称查询结束, 开始查询Area属性
2018-12-29 19:33:02,060 - io.github.coinsjack.dao.AreaMapper -4008 [main] DEBUG io.github.coinsjack.dao.AreaMapper - Cache Hit Ratio [io.github.coinsjack.dao.AreaMapper]: 0.0
2018-12-29 19:33:02,060 - org.apache.ibatis.transaction.jdbc.JdbcTransaction -4008 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Opening JDBC Connection
2018-12-29 19:33:02,060 - org.apache.ibatis.datasource.pooled.PooledDataSource -4008 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Checked out connection 503938393 from pool.
2018-12-29 19:33:02,060 - org.apache.ibatis.transaction.jdbc.JdbcTransaction -4008 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [[email protected]]
2018-12-29 19:33:02,061 - io.github.coinsjack.dao.AreaMapper.getAreaById -4009 [main] DEBUG io.github.coinsjack.dao.AreaMapper.getAreaById - ==> Preparing: select * from tb_area WHERE `area_id` = ?; 
2018-12-29 19:33:02,062 - io.github.coinsjack.dao.AreaMapper.getAreaById -4010 [main] DEBUG io.github.coinsjack.dao.AreaMapper.getAreaById - ==> Parameters: 3(Integer)
2018-12-29 19:33:02,066 - io.github.coinsjack.dao.AreaMapper.getAreaById -4014 [main] DEBUG io.github.coinsjack.dao.AreaMapper.getAreaById - <== Total: 1
2018-12-29 19:33:02,068 - org.apache.ibatis.transaction.jdbc.JdbcTransaction -4016 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [[email protected]]
2018-12-29 19:33:02,069 - org.apache.ibatis.transaction.jdbc.JdbcTransaction -4017 [main] DEBUG org.apache.ibatis.transaction.jdbc.JdbcTransaction - Closing JDBC Connection [[email protected]]
2018-12-29 19:33:02,069 - org.apache.ibatis.datasource.pooled.PooledDataSource -4017 [main] DEBUG org.apache.ibatis.datasource.pooled.PooledDataSource - Returned connection 503938393 to pool.
Area{id=3, name=‘西苑‘, priority=2, createTime=null, lastEditTime=null}

以上是关于Mybatis学习第25节 -- 懒加载 积极与不积极的主要内容,如果未能解决你的问题,请参考以下文章

Mybatis框架学习笔记 --- [懒加载配置 应用到嵌套查询]

Mybatis 学习笔记 — 开启日志启用懒加载

精通Mybatis之结果集处理流程与映射体系(无死角懒加载讲解)

MyBatis懒加载(延迟加载)

MyBatis 懒加载

mybatis懒加载