/**
-
根据用户id查询账户信息
-
@return
*/
List findAccountByUid(Integer uid);
}
账户的持久层映射文件
IAccountDao.xml:
<?xml version="1.0" encoding="UTF-8"?>select * from account
select * from account where uid = #{uid}
用户的持久层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 findAll();
/**
-
根据id查新用户信息
-
@param id
-
@return
*/
User findById(Integer id);
}
用户的持久层映射文件
IUserDao.xml:
<?xml version="1.0" encoding="UTF-8"?>select * from user where id = #{id}
开启 Mybatis 的延迟加载策略
在 Mybatis 的配置文件 SqlMapConfig.xml 文件中添加延迟加载的配置。
编写测试代码
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 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 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>
结点中也有 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 accounts;
public List getAccounts() {
return accounts;
}
public void setAccounts(List 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 findAll();
/**
-
根据id查新用户信息
-
@param id
-
@return
*/
User findById(Integer id);
}
用户的持久层映射文件
IUserDao.xml:
<?xml version="1.0" encoding="UTF-8"?>select * from user
select * from user where id = #{id}
账户的持久层 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 findAll();
/**
-
根据用户id查询账户信息
-
@return
*/
List findAccountByUid(Integer uid);
}
账户的持久层映射文件
IAccountDao.xml:
<?xml version="1.0" encoding="UTF-8"?>select * from account where uid = #{uid}
测试代码
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 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}