【MyBatis】基础篇(十二) MyBatis延迟加载策略

MyBatis·基础篇(十二) MyBatis延迟加载策略


1. 什么是延迟加载?

  • 延迟加载:

就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载.

  • 好处:

先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。

  • 坏处:

因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗时间,所以可能造成用户等待时间变长,造成用户体验下降。

2. 延迟加载场景需求分析

需求:

查询账户(Account)信息并且关联查询用户(User)信息。如果先查询账户(Account)信息即可满足要求,当我们需要查询用户(User)信息时再查询用户(User)信息。把对用户(User)信息的按需去查询就是延迟加载。

主要是通过 association、collection 实现一对一及一对多映射。association、collection 具备延迟加载功能。

3. 使用 assocation 实现延迟加载

查询账户信息同时查询用户信息

3.1 账户类的持久层接口

/**
 * @author: LzCc
 * @blog: https://blog.csdn.net/qq_41744145
 * @description: 账户类的持久层接口
 */
public interface AccountMapper {
    /**
     * 查询所有账户,同时获取账户的所属用户名称以及它的地址信息
     * @return
     */
    List<Account> findAll();
}

3.2 用户类的持久层接口和映射文件


/**
 * @author: LzCc
 * @blog: https://blog.csdn.net/qq_41744145
 * @description: 用户类的持久层接口
 */
public interface UserMapper {
    /**
     * 根据 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="top.lzchao.mybatisquickstart.mapper.UserMapper">
    <!-- 根据 id 查询 -->
    <select id="findById" resultType="top.lzchao.mybatisquickstart.entity.user" parameterType="int" >
        select * from user where id = #{uid}
    </select>
</mapper>

3.3 账户的持久层映射文件

<?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="top.lzchao.mybatisquickstart.mapper.AccountMapper">
    <!-- 建立对应关系 -->
    <resultMap type="top.lzchao.mybatisquickstart.entity.Account" id="accountMap">
        <id column="aid" property="id"/>
        <result column="uid" property="uid"/>
        <result column="money" property="money"/>
        <!-- 它是用于指定从表方的引用实体属性的 -->
        <association property="user" javaType="top.lzchao.mybatisquickstart.entity.User"
                     select="top.lzchao.mybatisquickstart.mapper.UserMapper.findById"
                     column="uid">
        </association>
    </resultMap>
    <select id="findAll" resultMap="accountMap">
        select
            a.*,
            u.username,
            u.address
        from
            account a,
            user u
        where a.uid =u.id;
    </select>
</mapper>
  • association :有一个的关系映射
    • select : 填写我们要调用的 select 映射的 id
    • column : 填写我们要传递给 select 映射的参数

3.4 开启 Mybatis 的延迟加载策略

延迟加载策略默认是关闭的,所以在编写好上面代码后并没有出现延迟加载效果。
我们需要手动设置开启延迟加载策略

进入 Mybaits 的官方文档,找到 settings 的说明信息:
在这里插入图片描述
我们需要在 Mybatis 的配置文件 SqlMapConfig.xml 文件中添加延迟加载的配置。

<!-- 开启延迟加载的支持 -->
<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

aggressiveLazyLoading:在 3.4.1 及之前的版本默认值为 true,之后的版本可以省略配置

3.5 编写测试只查账户信息不查用户信息

@Test
public void testFindAll() {
    //6.执行操作
    List<Account> accounts = accountMapper.findAll();
}

控制台输出:
在这里插入图片描述

我们发现,因为本次只是将 Account对象查询出来放入 List 集合中,并没有涉及到 User对象,所以就没有发出 SQL 语句查询账户所关联的 User 对象的查询

3.5 编写测试查账户信息和查用户信息

@Test
public void testFindAll() {
    //6.执行操作
    List<Account> accounts = accountMapper.findAll();
    for (Account account : accounts) {
        System.out.println(account.getUser());
    }
}

控制台输出:
在这里插入图片描述

当我们去调用被延迟加载的对象时,他就会去查询sql语句并加载出对象来

4. 使用 Collection 实现延迟加载

需求:完成加载用户对象时,查询该用户所拥有的账户信息。

4.1 在 User 实体类中加入 List属性

private List<Account> accounts;
public List<Account> getAccounts() {
	return accounts;
}
public void setAccounts(List<Account> accounts) {
	this.accounts = accounts;
}

4.2 编写账户持久层接口的方法

/**
* 根据用户 id 查询账户信息
* @param uid
* @return
*/
List<Account> findByUid(Integer uid);

4.3 编写用户持久层映射配置

<resultMap type="top.lzchao.mybatisquickstart.entity.User" id="userMap">
    <id column="id" property="id"></id>
    <result column="username" property="username"/>
    <result column="address" property="address"/>
    <result column="sex" property="sex"/>
    <result column="birthday" property="birthday"/>
    <!-- collection 是用于建立一对多中集合属性的对应关系
    ofType 用于指定集合元素的数据类型
    select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
    column 是用于指定使用哪个字段的值作为条件查询
    -->
    <collection property="accounts" ofType="top.lzchao.mybatisquickstart.entity.Account"
                select="top.lzchao.mybatisquickstart.mapper.AccountMapper.findByUid"
                column="id">
    </collection>
</resultMap>
<!-- 配置查询所有操作 -->
<select id="findAll" resultMap="userMap">
 select * from user
</select>
  • 标签:

主要用于加载关联的集合对象

  • select 属性:

用于指定查询 account 列表的 sql 语句,所以填写的是该 sql 映射的 id

  • column 属性:

用于指定 select 属性的 sql 语句的参数来源,上面的参数来自于 user 的 id 列,所以就写成 id 这一个字段名了

4.4 编写账户持久层映射配置

<!-- 根据用户 id 查询账户信息 -->
<select id="findByUid" resultType="top.lzchao.mybatisquickstart.entity.Account" parameterType="int">
select * from account where uid = #{uid}
</select>

4.5 测试只加载用户信息

@Test
public void testFindAll() {
//6.执行操作
List<User> users = userDao.findAll();
}

控制台输出:
在这里插入图片描述
我们发现并没有查询 Account 账户信息

4.5 测试加载用户信息,获取账户信息

@Test
public void testFindAll() {
//6.执行操作
List<User> users = userDao.findAll();
	for(User user : users) {
       user.getAccounts()
    }
}

控制台输出:
在这里插入图片描述
当获取账户信息时,才发送SQL语句查询

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值