Mybatis框架学习(九)延迟加载

延迟加载

  1. 就是在需要用到数据时才进行加载,不需要用到数据时就不加载数据。延迟加载也称懒加载.
  2. 优点:先从单表查询,需要时再从关联表去关联查询,大大提高数据库性能,因为查询单表要比关联查询多张表速度要快。
  3. 缺点:因为只有当需要用到数据时,才会进行数据库查询,这样在大批量数据查询时,因为查询工作也要消耗时间,所以可能造成用户等待时间变长,造成用户体验下降。

实现延迟加载

  • association、collection 具备延迟加载功能。

使用 assocation 实现延迟加载

  1. 账户的持久层 DAO 接口
    在这里插入图片描述
  2. 账户的持久层 映射文件
  • select : 填写我们要调用的 select 映射的 id
  • column : 填写我们要传递给 select 映射的参数
<resultMap id="AccountU" type="com.lwb.domain.Account">
        <!--建立对应关系-->
        <id property="id" column="aid"></id>
        <result column="uid" property="uid"/>
        <result column="money" property="money"/>
        <!--它是用于指定从表方的引用实体属性的-->
        <association property="user" javaType="com.lwb.domain.User" select="com.lwb.dao.IUserDao.findById"  column="uid">
        </association>
    </resultMap>
  1. 用户的持久层 接口和 映射文件
    在这里插入图片描述
    在这里插入图片描述
  2. 开启 Mybatis 的延迟加载策略
    SqlMapConifg.xml
    在这里插入图片描述
  3. 测试
  • 使用懒加载,没有用到数据的时候
    @Test
    public void test11()throws Exception {

        // 第五步:执行dao中的方法
        List<Account> accounts = accountDao.findAll();

    }

在这里插入图片描述

  • 使用到数据的时候
    @Test
    public void test11()throws Exception {

        // 第五步:执行dao中的方法
        List<Account> accounts = accountDao.findAll();
        for (Account account:accounts) {
            System.out.println(account.getUser());
        }
    }

在这里插入图片描述

使用 Collection 实现延迟加载

  • 同样我们也可以在一对多关系配置的<collection>结点中配置延迟加载策略。
    <collection>结点中也有 select 属性,column 属性。
  1. 在 在 User 实体类中加入 List<Account> 属性
    在这里插入图片描述
  2. 编写用户和账户持久层接口的方法
  • IUserDao接口
List<User> findAll();
  • IAccountDao接口
/**
* 根据用户 id 查询账户信息
* @param uid
* @return
*/
List<Account> findByUid(Integer uid);
  1. 编写用户持久层映射 配置
  • collection 是用于建立一对多中集合属性的对应关系
  • ofType 用于指定集合元素的数据类型
  • select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
  • column 是用于指定使用哪个字段的值作为条件查询
    <resultMap id="UserA" type="com.lwb.domain.User">
        <id property="id" column="id"></id>
        <result column="username" property="username"/>
        <result column="sex" property="sex"/>
        <result column="birthday" property="birthday"/>
        <result column="address" property="address"/>
        <collection property="accounts" ofType="com.lwb.domain.Account" select="com.lwb.dao.IAccountDao.findByUid" column="id">
        </collection>
    </resultMap>
    <select id="findAll" resultMap="UserA">
        select * from USER
    </select>
  1. 编写账户持久层映射配置
    在这里插入图片描述
  2. 测试
  • 延迟加载
    @Test
    public void test12()throws Exception {

        // 第五步:执行dao中的方法
        List<User> users = dao.findAll();
    }

在这里插入图片描述

  • 非延迟加载
    @Test
    public void test12()throws Exception {

        // 第五步:执行dao中的方法
        List<User> users = dao.findAll();
        for (User user:users){
            System.out.println(user+" "+user.getAccounts());
        }
    }

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值