1. 程式人生 > >Mybatis學習第25節 -- 懶加載 積極與不積極

Mybatis學習第25節 -- 懶加載 積極與不積極

true cti system bat open p s 積極 batis pan

積極懶加載是指如果你訪問一個對象的屬性,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 [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}

Mybatis學習第25節 -- 懶加載 積極與不積極