Mybatis学习第25节 -- 懒加载 积极与不积极
作者:互联网
积极懒加载是指如果你访问一个对象的属性,Mybatis就会帮你把需要进步一查询的该属性或者其他属性在数据库中查询出来.
不积极懒加载是指,有这种必要的时候,采取进行必要的数据库检索
我看的教程中的Mybatis版本默认是积极的lazy加载, 而我实际用的Mybatis3.4.6实际上默认不积极的lazy加载.
mybatis-config.xml文件
测试用例
测试积极懒加载
<settings> |
// 测试懒加载 |
|
测试结果
在这里我们访问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 [com.mysql.cj.jdbc.ConnectionImpl@1e097d59] 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 [com.mysql.cj.jdbc.ConnectionImpl@1e097d59] 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 [com.mysql.cj.jdbc.ConnectionImpl@1e097d59] 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} |
标签:25,coinsjack,github,dao,29,积极,io,Mybatis,main 来源: https://www.cnblogs.com/litran/p/10546262.html