通过前面的学习,我们已经掌握了 Mybatis 中一对一,一对多,多对多关系的配置及实现,可以实现对象的关联查询。实际开发过程中很多时候我们并不需要总是在加载用户信息时就一定要加载他的账户信息。此时就是我们所说的延迟加载.
延迟加载:
就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载.
好处:
先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。
坏处:
因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗时间,所以可能造成用户等待时间变长,造成用户体验下降。
需求:
查询账户(Account)信息并且关联查询用户(User)信息。如果先查询账户(Account)信息即可满足要求,当我们需要查询用户(User)信息时再查询用户(User)信息。把对用户(User)信息的按需去查询就是延迟加载。
mybatis 第三天实现多表操作时,我们使用了 resultMap 来实现一对一,一对多,多对多关系的操作。主要是通过 association、collection 实现一对一及一对多映射。association、collection 具备延迟加载功能。
需求:
查询账户信息同时查询用户信息。
/**
* 账户的持久层接口
*/
public interface IAccountDao {
/**
* 查询所有账户
* @return
*/
List<Account> findAll();
}
<?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.zjw.dao.IAccountDao">
<resultMap id="accountUserMap" type="Account">
<id property="id" column="id"></id>
<result property="uid" column="uid"></result>
<result property="money" column="money"></result>
<!-- <association property="user" javaType="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>
</association>-->
<!--
一对一的关系映射,配置封装User的内容
select属性指定的内容,查询用户的唯一标识
column属性指定的内容,用户根据id查询时,所需参数的值
-->
<association property="user" column="uid" javaType="User" select="com.zjw.dao.IUserDao.findById">
</association>
</resultMap>
<!--查询所有账户-->
<select id="findAll" resultMap="accountUserMap">
select * from account ;
</select>
</mapper>
/**
* 用户的持久层接口
*/
public interface IUserDao {
/**
* 根据id查询用户
* @param userId
* @return
*/
User findById(Integer userId);
}
<?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.zjw.dao.IUserDao">
<resultMap id="UserAccountMap" type="User">
<id property="id" column="id"></id>
<result property="address" column="address"></result>
<result property="username" column="username"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
</resultMap>
<!--查询用户-->
<select id="findById" parameterType="Integer" resultType="User">
select * from user where id = #{userId} ;
</select>
</mapper>
我们需要在 Mybatis 的配置文件 SqlMapConfig.xml 文件中添加延迟加载的配置。
<settings>
<setting name="lazyLoadingEnabled" value="true"/>
<setting name="aggressiveLazyLoading" value="false"/>
</settings>
public class AccountTest {
private InputStream in ;
private SqlSessionFactoryBuilder builder;
private SqlSessionFactory factory ;
private SqlSession session ;
private IUserDao userDao ;
private IAccountDao accountDao;
@Before
public void init() throws Exception{
//读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory工厂
builder = new SqlSessionFactoryBuilder();
factory = builder.build(in);
//使用工厂生成SqlSession对象
session = factory.openSession();
//使用SqlSession穿过将Dao接口的代理对象
accountDao = session.getMapper(IAccountDao.class);
}
@After
public void destory() throws Exception{
session.commit();
session.close();
in.close();
}
/**
* 测试查询操作
*/
@Test
public void testFindAll(){
//使用代理对象执行方法
List<Account> accounts = accountDao.findAll();
}
}
测试结果如下:
我们发现,因为本次只是将 Account 对象查询出来放入 List 集合中,并没有涉及到 User 对象,所以就没有发出 SQL 语句查询账户所关联的 User 对象的查询。
同样我们也可以在一对多关系配置的
结点中配置延迟加载策略。
<collection>结点中也有 select 属性,column 属性。
需求:
完成加载用户对象时,查询该用户所拥有的账户信息。
package com.zjw.domain;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
public class User implements Serializable {
private Integer id;
private String username;
private String address;
private String sex;
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 getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username=‘" + username + ‘\‘‘ +
", address=‘" + address + ‘\‘‘ +
", sex=‘" + sex + ‘\‘‘ +
", birthday=" + birthday +
‘}‘;
}
}
/**
* 查询所有用户
* @return
*/
List<User> findAll();
/**
* 根据用户id查询用户的所有账户
* @param uid
* @return
*/
List<Account> findAccountByUid(Integer uid);
<?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.zjw.dao.IUserDao">
<resultMap id="UserAccountMap" type="User">
<id property="id" column="id"></id>
<result property="address" column="address"></result>
<result property="username" column="username"></result>
<result property="sex" column="sex"></result>
<result property="birthday" column="birthday"></result>
<!-- collection 是用于建立一对多中集合属性的对应关系
ofType 用于指定集合元素的数据类型
select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
column 是用于指定使用哪个字段的值作为条件查询
-->
<collection property="accounts" ofType="Account" column="id" select="com.zjw.dao.IAccountDao.findAccountByUid">
</collection>
</resultMap>
<!--查询所有-->
<select id="findAll" resultMap="UserAccountMap">
select * from user;
</select>
</mapper>
<select id="findAccountByUid" resultType="Account" parameterType="Integer">
select ac.* from account ac where ac.uid = #{uid};
</select>
public class UserTest {
private InputStream in ;
private SqlSessionFactoryBuilder builder;
private SqlSessionFactory factory ;
private SqlSession session ;
private IUserDao userDao ;
@Before
public void init() throws Exception{
//读取配置文件
in = Resources.getResourceAsStream("SqlMapConfig.xml");
//创建SqlSessionFactory工厂
builder = new SqlSessionFactoryBuilder();
factory = builder.build(in);
//使用工厂生成SqlSession对象
session = factory.openSession();
//使用SqlSession穿过将Dao接口的代理对象
userDao = session.getMapper(IUserDao.class);
}
@After
public void destory() throws Exception{
session.commit();
session.close();
in.close();
}
/**
* 测试查询操作
*/
@Test
public void testFindAll(){
//使用代理对象执行方法
List<User> users = userDao.findAll();
// for (User user : users) {
// System.out.println(user);
// List<Account> accounts = user.getAccounts();
// for (Account account : accounts) {
// System.out.println(account);
// }
// }
}
我们发现并没有加载 Account 账户信息.
原文:https://www.cnblogs.com/zjw-blog/p/13934686.html