Mybatis延遲加載策略
- 延遲加載
- 專案目錄
- 使用 assocation 實作延遲加載(一對一)
- 需求
- Account物體類
- 賬戶的持久層 DAO 介面
- 賬戶的持久層映射檔案
- 用戶的持久層DAO介面
- 用戶的持久層映射檔案
- 開啟 Mybatis 的延遲加載策略
- 撰寫測驗代碼
- 使用 Collection 實作延遲加載(一對多)
- 需求
- User 物體類
- 用戶的持久層DAO介面
- 用戶的持久層映射檔案
- 賬戶的持久層 DAO 介面
- 賬戶的持久層映射檔案
- 測驗代碼
在對應的四種表關系中:一對多,多對一,一對一,多對多
一對多,多對多:通常情況下我們都是采用延遲加載,
多對一,一對一:通常情況下我們都是采用立即加載,
延遲加載
延遲加載:
就是在需要用到資料時才進行加載,不需要用到資料時就不加載資料,延遲加載也稱懶加載,
在實際的開發程序中很多時候我們并不需要總是在加載用戶資訊時就一定要加載他的賬戶資訊,這就是我們所說的延遲加載,
在真正使用資料時才發起查詢,不用的時候不查詢,按需加載(懶加載),
好處:
先從單表查詢,需要時再從關聯表去關聯查詢,大大提高資料庫性能,因為查詢單表要比關聯查詢多張表速度要快,
壞處:
因為只有當需要用到資料時,才會進行資料庫查詢,這樣在大批量資料查詢時,因為查詢作業也要消耗時間,所以可能造成用戶等待時間變長,造成用戶體驗下降,
專案目錄

使用 assocation 實作延遲加載(一對一)
需求
查詢賬戶資訊同時查詢用戶資訊,一對一實作延遲加載,
Account物體類
Account:
package com.keafmd.domain;
import java.io.Serializable;
/**
* Keafmd
*
* @ClassName: Account
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-10 21:43
*/
public class Account implements Serializable {
private Integer id;
private Integer uid;
private Double money;
//從表物體應該包含一個主表物體的物件參考
private User user;
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Double getMoney() {
return money;
}
public void setMoney(Double money) {
this.money = money;
}
@Override
public String toString() {
return "Account{" +
"id=" + id +
", uid=" + uid +
", money=" + money +
'}';
}
}
賬戶的持久層 DAO 介面
IAccountDao:
package com.keafmd.dao;
import com.keafmd.domain.Account;
import java.util.List;
/**
* Keafmd
*
* @ClassName: IAccountDao
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-10 21:46
*/
public interface IAccountDao {
/**
* 查詢所有賬戶,同時還要獲取當前賬戶的所屬用戶資訊
* @return
*/
List<Account> findAll();
/**
* 根據用戶id查詢賬戶資訊
* @return
*/
List<Account> findAccountByUid(Integer uid);
}
賬戶的持久層映射檔案
IAccountDao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.keafmd.dao.IAccountDao">
<!--定義封裝account和user的resultmap-->
<resultMap id="accountUserMap" type="account">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!--一對一的關系映射,配置封裝user的內容
select屬性指定的內容:查詢用戶的唯一標志
column屬性指定的內容:用戶根據id查詢是,所需要的引數的值
-->
<association property="user" column="uid" javaType="user" select="com.keafmd.dao.IUserDao.findById">
</association>
</resultMap>
<!--配置查詢所有-->
<select id="findAll" resultMap="accountUserMap">
select * from account
</select>
<!--根據用戶id查詢賬戶串列-->
<select id="findAccountByUid" resultType="account">
select * from account where uid = #{uid}
</select>
</mapper>
用戶的持久層DAO介面
IUserDao:
package com.keafmd.dao;
import com.keafmd.domain.User;
import java.util.List;
/**
* Keafmd
*
* @ClassName: IUserDao
* @Description: 用戶的持久層介面
* @author: 牛哄哄的柯南
* @date: 2021-02-06 19:29
*/
public interface IUserDao {
/**
* 查詢所有用戶,同時獲取到用戶下所有賬戶的資訊
* @return
*/
List<User> findAll();
/**
* 根據id查新用戶資訊
* @param id
* @return
*/
User findById(Integer id);
}
用戶的持久層映射檔案
IUserDao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.keafmd.dao.IUserDao">
<!--根據id查詢用戶-->
<select id="findById" parameterType="Integer" resultType="user">
select * from user where id = #{id}
</select>
</mapper>
開啟 Mybatis 的延遲加載策略

在 Mybatis 的組態檔 SqlMapConfig.xml 檔案中添加延遲加載的配置,
<!--配置引數-->
<settings>
<!--開啟Mybatis支持延遲加載-->
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
撰寫測驗代碼
AccountTest:
package com.keafmd.test;
import com.keafmd.dao.IAccountDao;
import com.keafmd.domain.Account;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
/**
* Keafmd
*
* @ClassName: MybatisTest
* @Description: 測驗類
* @author: 牛哄哄的柯南
* @date: 2021-02-08 15:24
*/
public class AccountTest {
private InputStream in;
private SqlSession sqlsession;
private IAccountDao accountDao;
@Before // 用于在測驗方法執行前執行
public void init()throws Exception{
//1.讀取組態檔,生成位元組輸入流
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.創建SqlSessionFactory工廠
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工廠生產SqlSession物件
sqlsession = factory.openSession(); //里面寫個true,下面每次就不用了寫 sqlsession.commit(); 了
//4.使用SqlSession創建Dao介面的代理物件
accountDao = sqlsession.getMapper(IAccountDao.class);
}
@After // 用于在測驗方法執行后執行
public void destory() throws Exception{
//提交事務
sqlsession.commit();
//6.釋放資源
sqlsession.close();
in.close();
}
/**
* 查詢所有 一對一實作延遲加載測驗
* @throws Exception
*/
@Test
public void testFindAll() {
List<Account> accounts = accountDao.findAll();
for (Account account : accounts) {
System.out.println("------------每個account的資訊-------");
System.out.println(account);
System.out.println(account.getUser());
}
}
}
運行結果:
2021-02-14 16:15:59,771 620 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-02-14 16:16:00,072 921 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1783047508.
2021-02-14 16:16:00,072 921 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a472554]
2021-02-14 16:16:00,076 925 [ main] DEBUG keafmd.dao.IAccountDao.findAll - ==> Preparing: select * from account
2021-02-14 16:16:00,099 948 [ main] DEBUG keafmd.dao.IAccountDao.findAll - ==> Parameters:
2021-02-14 16:16:00,156 1005 [ main] DEBUG keafmd.dao.IAccountDao.findAll - <== Total: 3
------------每個account的資訊-------
2021-02-14 16:16:00,156 1005 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Preparing: select * from user where id = ?
2021-02-14 16:16:00,156 1005 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Parameters: 41(Integer)
2021-02-14 16:16:00,159 1008 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <== Total: 1
Account{id=1, uid=41, money=1000.0}
User{id=41, username='老王', sex='男', address='北京', birthday=Tue Feb 27 17:47:08 CST 2018}
------------每個account的資訊-------
2021-02-14 16:16:00,160 1009 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Preparing: select * from user where id = ?
2021-02-14 16:16:00,160 1009 [ main] DEBUG m.keafmd.dao.IUserDao.findById - ==> Parameters: 45(Integer)
2021-02-14 16:16:00,161 1010 [ main] DEBUG m.keafmd.dao.IUserDao.findById - <== Total: 1
Account{id=2, uid=45, money=1000.0}
User{id=45, username='新一', sex='男', address='北京', birthday=Sun Mar 04 12:04:06 CST 2018}
------------每個account的資訊-------
Account{id=3, uid=41, money=2000.0}
User{id=41, username='老王', sex='男', address='北京', birthday=Tue Feb 27 17:47:08 CST 2018}
2021-02-14 16:16:00,161 1010 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a472554]
2021-02-14 16:16:00,161 1010 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@6a472554]
2021-02-14 16:16:00,162 1011 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1783047508 to pool.
Process finished with exit code 0
當我們把測驗代碼改寫成下面這樣,只查賬戶資訊不查用戶資訊,
/**
* 查詢所有 一對一實作延遲加載測驗
* @throws Exception
*/
@Test
public void testFindAll() {
List<Account> accounts = accountDao.findAll();
/*for (Account account : accounts) {
System.out.println("------------每個account的資訊-------");
System.out.println(account);
System.out.println(account.getUser());
}*/
}
運行結果:
2021-02-14 16:17:27,554 185 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-02-14 16:17:27,811 442 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1641313620.
2021-02-14 16:17:27,811 442 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:17:27,815 446 [ main] DEBUG keafmd.dao.IAccountDao.findAll - ==> Preparing: select * from account
2021-02-14 16:17:27,845 476 [ main] DEBUG keafmd.dao.IAccountDao.findAll - ==> Parameters:
2021-02-14 16:17:27,901 532 [ main] DEBUG keafmd.dao.IAccountDao.findAll - <== Total: 3
2021-02-14 16:17:27,901 532 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:17:27,902 533 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:17:27,902 533 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1641313620 to pool.
Process finished with exit code 0
對比上面的測驗輸出可以很明顯的看出來:因為本次只是將 Account物件查詢出來放入 List 集合中,并沒有涉及到 User物件,所以就沒有發出 SQL 陳述句查詢賬戶所關聯的 User 物件的查詢,
使用 Collection 實作延遲加載(一對多)
我們也可以在一對多關系配置的
<collection>結點中配置延遲加載策略,
<collection>結點中也有 select 屬性,column 屬性,
需求
完成加載用戶物件時,查詢該用戶所擁有的賬戶資訊,
User 物體類
User:
package com.keafmd.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
/**
* Keafmd
*
* @ClassName: User
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-08 15:16
*/
public class User implements Serializable {
private Integer id;
private String username;
private String sex;
private String address;
private Date birthday;
//一對多關系映射,主表物體應該包含從表物體的集合參考
private List<Account> accounts;
public List<Account> getAccounts() {
return accounts;
}
public void setAccounts(List<Account> accounts) {
this.accounts = accounts;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", sex='" + sex + '\'' +
", address='" + address + '\'' +
", birthday=" + birthday +
'}';
}
}
用戶的持久層DAO介面
IUserDao:
package com.keafmd.dao;
import com.keafmd.domain.User;
import java.util.List;
/**
* Keafmd
*
* @ClassName: IUserDao
* @Description: 用戶的持久層介面
* @author: 牛哄哄的柯南
* @date: 2021-02-06 19:29
*/
public interface IUserDao {
/**
* 查詢所有用戶,同時獲取到用戶下所有賬戶的資訊
* @return
*/
List<User> findAll();
/**
* 根據id查新用戶資訊
* @param id
* @return
*/
User findById(Integer id);
}
用戶的持久層映射檔案
IUserDao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.keafmd.dao.IUserDao">
<!--定義User的resultMap-->
<resultMap id="userAccountMap" type="user">
<id property="id" column="id"></id>
<result property="username" column="username"></result>
<result property="address" column="address"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<!--配置user物件中account集合的映射-->
<collection property="accounts" ofType="account" select="com.keafmd.dao.IAccountDao.findAccountByUid" column="id"></collection>
</resultMap>
<!--配置查詢所有-->
<select id="findAll" resultMap="userAccountMap">
select * from user
</select>
<!--根據id查詢用戶-->
<select id="findById" parameterType="Integer" resultType="user">
select * from user where id = #{id}
</select>
</mapper>
賬戶的持久層 DAO 介面
IAccountDao:
package com.keafmd.dao;
import com.keafmd.domain.Account;
import java.util.List;
/**
* Keafmd
*
* @ClassName: IAccountDao
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-10 21:46
*/
public interface IAccountDao {
/**
* 查詢所有賬戶,同時還要獲取當前賬戶的所屬用戶資訊
* @return
*/
List<Account> findAll();
/**
* 根據用戶id查詢賬戶資訊
* @return
*/
List<Account> findAccountByUid(Integer uid);
}
賬戶的持久層映射檔案
IAccountDao.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.keafmd.dao.IAccountDao">
<!--根據用戶id查詢賬戶串列-->
<select id="findAccountByUid" resultType="account">
select * from account where uid = #{uid}
</select>
</mapper>
測驗代碼
package com.keafmd.test;
import com.keafmd.dao.IUserDao;
import com.keafmd.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.InputStream;
import java.util.List;
/**
* Keafmd
*
* @ClassName: MybatisTest
* @Description: 測驗類,測驗crud操作
* @author: 牛哄哄的柯南
* @date: 2021-02-08 15:24
*/
public class UserTest {
private InputStream in;
private SqlSession sqlsession;
private IUserDao userDao;
@Before // 用于在測驗方法執行前執行
public void init()throws Exception{
//1.讀取組態檔,生成位元組輸入流
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//2.創建SqlSessionFactory工廠
SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
SqlSessionFactory factory = builder.build(in);
//3.使用工廠生產SqlSession物件
sqlsession = factory.openSession(); //里面寫個true,下面每次就不用了寫 sqlsession.commit(); 了
//4.使用SqlSession創建Dao介面的代理物件
userDao = sqlsession.getMapper(IUserDao.class);
}
@After // 用于在測驗方法執行后執行
public void destory() throws Exception{
//提交事務
sqlsession.commit();
//6.釋放資源
sqlsession.close();
in.close();
}
/**
* 查詢所有 ,一對多實作延遲加載
* @throws Exception
*/
@Test
public void testFindAll() {
List<User> users = userDao.findAll();
for (User user : users) {
System.out.println("--------每個用戶的資訊---------");
System.out.println(user);
System.out.println(user.getAccounts());
}
}
}
運行結果:
2021-02-14 16:29:07,023 208 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-02-14 16:29:07,272 457 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1641313620.
2021-02-14 16:29:07,272 457 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:29:07,276 461 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Preparing: select * from user
2021-02-14 16:29:07,303 488 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Parameters:
2021-02-14 16:29:07,371 556 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - <== Total: 9
--------每個用戶的資訊---------
2021-02-14 16:29:07,372 557 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,372 557 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 41(Integer)
2021-02-14 16:29:07,375 560 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 2
User{id=41, username='老王', sex='男', address='北京', birthday=Tue Feb 27 17:47:08 CST 2018}
[Account{id=1, uid=41, money=1000.0}, Account{id=3, uid=41, money=2000.0}]
--------每個用戶的資訊---------
2021-02-14 16:29:07,376 561 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,376 561 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 42(Integer)
2021-02-14 16:29:07,377 562 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{id=42, username='update', sex='男', address='XXXXXXX', birthday=Mon Feb 08 19:37:31 CST 2021}
[]
--------每個用戶的資訊---------
2021-02-14 16:29:07,377 562 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,377 562 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 43(Integer)
2021-02-14 16:29:07,378 563 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{id=43, username='小二王', sex='女', address='北京', birthday=Sun Mar 04 11:34:34 CST 2018}
[]
--------每個用戶的資訊---------
2021-02-14 16:29:07,379 564 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,383 568 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 45(Integer)
2021-02-14 16:29:07,385 570 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 1
User{id=45, username='新一', sex='男', address='北京', birthday=Sun Mar 04 12:04:06 CST 2018}
[Account{id=2, uid=45, money=1000.0}]
--------每個用戶的資訊---------
2021-02-14 16:29:07,386 571 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,386 571 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 50(Integer)
2021-02-14 16:29:07,387 572 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{id=50, username='Keafmd', sex='男', address='XXXXXXX', birthday=Mon Feb 08 15:44:01 CST 2021}
[]
--------每個用戶的資訊---------
2021-02-14 16:29:07,387 572 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,387 572 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 51(Integer)
2021-02-14 16:29:07,389 574 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{id=51, username='update DAO', sex='男', address='XXXXXXX', birthday=Tue Feb 09 11:31:38 CST 2021}
[]
--------每個用戶的資訊---------
2021-02-14 16:29:07,389 574 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,390 575 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 52(Integer)
2021-02-14 16:29:07,391 576 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{id=52, username='Keafmd DAO', sex='男', address='XXXXXXX', birthday=Tue Feb 09 11:29:41 CST 2021}
[]
--------每個用戶的資訊---------
2021-02-14 16:29:07,391 576 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,391 576 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 53(Integer)
2021-02-14 16:29:07,392 577 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{id=53, username='Keafmd laset insertid 1', sex='男', address='XXXXXXX', birthday=Fri Feb 12 20:53:46 CST 2021}
[]
--------每個用戶的資訊---------
2021-02-14 16:29:07,392 577 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Preparing: select * from account where uid = ?
2021-02-14 16:29:07,392 577 [ main] DEBUG o.IAccountDao.findAccountByUid - ==> Parameters: 54(Integer)
2021-02-14 16:29:07,392 577 [ main] DEBUG o.IAccountDao.findAccountByUid - <== Total: 0
User{id=54, username='Keafmd laset insertid 2 auto', sex='男', address='XXXXXXX', birthday=Fri Feb 12 21:02:12 CST 2021}
[]
2021-02-14 16:29:07,392 577 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:29:07,393 578 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:29:07,393 578 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1641313620 to pool.
Process finished with exit code 0
同理我們直接注釋掉輸出陳述句就不會進行賬戶資訊的查詢了,
/**
* 查詢所有 ,一對多實作延遲加載
* @throws Exception
*/
@Test
public void testFindAll() {
List<User> users = userDao.findAll();
/*for (User user : users) {
System.out.println("--------每個用戶的資訊---------");
System.out.println(user);
System.out.println(user.getAccounts());
}*/
}
運行結果:
2021-02-14 16:30:33,239 191 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Opening JDBC Connection
2021-02-14 16:30:33,493 445 [ main] DEBUG source.pooled.PooledDataSource - Created connection 1641313620.
2021-02-14 16:30:33,494 446 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Setting autocommit to false on JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:30:33,498 450 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Preparing: select * from user
2021-02-14 16:30:33,521 473 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - ==> Parameters:
2021-02-14 16:30:33,583 535 [ main] DEBUG om.keafmd.dao.IUserDao.findAll - <== Total: 9
2021-02-14 16:30:33,584 536 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Resetting autocommit to true on JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:30:33,584 536 [ main] DEBUG ansaction.jdbc.JdbcTransaction - Closing JDBC Connection [com.mysql.jdbc.JDBC4Connection@61d47554]
2021-02-14 16:30:33,585 537 [ main] DEBUG source.pooled.PooledDataSource - Returned connection 1641313620 to pool.
Process finished with exit code 0
我們這時就可以發現并沒有加載 Account 賬戶資訊,沒有執行查詢賬戶資訊的sql陳述句,
以上就是Mybatis延遲加載策略的全部內容,
看完如果對你有幫助,感謝點贊支持!
如果你是電腦端的話,看到右下角的 “一鍵三連” 了嗎,沒錯點它[哈哈]

加油!
共同努力!
Keafmd
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/259778.html
標籤:其他
