mybatis 延迟加载策略

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

  需求:
    查询账户信息同时查询用户信息。

  1.账户的持久层 DAO 接口

public interface UserDao {
    List<User> findAll();
}

  2.账户的持久层映射文件

<mapper namespace="com.fgy.dao.AccountDao">
    <resultMap id="accountMap" type="account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!-- 指定从表方的引用实体属性
            select:填写要调用的 select 映射的 id
            column:填写我们要传递给 select 映射的参数
         -->
        <association property="user" column="uid" select="com.fgy.dao.UserDao.findById" javaType="user">
        </association>
    </resultMap>

    <select id="findAll" resultMap="accountMap">
        select * from account
    </select>
</mapper>

  3.用户的持久层接口和映射文件

public interface UserDao {
    User findById(Integer id);
}
<mapper namespace="com.fgy.dao.UserDao">
<select id="findById" parameterType="int" resultType="user">
        SELECT * FROM `user` WHERE id = #{id}
    </select>
</mapper>

  4.开启 Mybatis 的延迟加载策略

    进入 Mybaits 的官方文档,找到 settings 的说明信息:

  

    在 Mybatis 的主配置文件 SqlMapConfig.xml 文件中添加延迟加载的配置。
      <!-- 开启延迟加载的支持 -->

<settings>
    <setting name="lazyLoadingEnabled" value="true"/>
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

  5.测试:

@Test
public void testFindAll() {
    List<Account> list = accountDao.findAll();
}

    测试结果如下:

      

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

使用 collection 实现延迟加载:

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

  1.编写用户和账户持久层接口的方法

public interface UserDao {
    List<User> findAll();
}
public interface AccountDao {
    List<Account> findByUid(Integer uid);
}

  2.编写用户持久层映射配置

<mapper namespace="com.fgy.dao.UserDao">
    <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>
        <!-- collection 用于建立一对多中集合属性的对应关系
            ofType 用于指定集合元素的数据类型
            select 是用于指定查询账户的唯一标识(账户的 dao 全限定类名加上方法名称)
            column 是用于指定使用哪个字段的值作为条件查询
        -->
        <collection property="accounts" column="id" select="com.fgy.dao.AccountDao.findByUid" ofType="account"></collection>
    </resultMap>

    <select id="findAll" resultMap="userAccountMap">
        select * from user
    </select>
</mapper>

  3.编写账户持久层映射配置

<mapper namespace="com.fgy.dao.AccountDao">
    <select id="findByUid" parameterType="int" resultType="account">
        select * from account where uid = #{uid}
    </select>
</mapper>

  4.测试只加载用户信息

@Test
public void testFindAll() {
    List<User> users = userDao.findAll();
    /*for (User user : users) {
        System.out.println(user);
        for (Account account : user.getAccounts()) {
            System.out.println(account);
        }
    }*/
}

    

     发现并没有加载 Account 账户信息。

在主配置文件 settings 标签中开启延迟加载,所有关联对象都会延迟加载,有时有些特定关联对象并不需要延迟加载,可以在 assocation 或 collection 标签中通过设置 fetchType 属性来覆盖全局设置,取值 eager | lazy

原文地址:https://www.cnblogs.com/roadlandscape/p/12295219.html