09.mybatis的延迟加载策略

mybatis的延迟加载策略


mybatis测试表结构.png

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

需要使用被延迟加载的属性时才去数据库查询相应的表,然后再封装!

一、延迟加载的有点以及缺点

1.优点

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

2.缺点

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

association、collection具备延迟加载功能


数据表描述

假设有两张表user account;是一对多的关系


公共pojo

  1. User
private Integer id;
private String username;
private Date birthday;
private String sex;
private String address;

List<Account> accounts;
  1. Account
private Integer id;
private Integer uid;
private Double money;
private User user;

SqlMapConfig.xml中开启延迟查询的配置

<configuration>
    <properties resource="JdbcConfig.properties"></properties>
    <settings>
        -- lazyLoadingEnabled 开启延迟加载
        -- aggressiveLazyLoading 当开启时,调用任何方法都会加载该对象的所有属性。否则,就会按需加载
        <setting name="lazyLoadingEnabled" value="true"/>
        <setting name="aggressiveLazyLoading" value="false"/>
    </settings>
    ...

二、使用association实现延迟加载

  1. AccountDao接口中的方法
/** 查询所有账户,同时获取账户的所属用户名名称
 * 以及地址信息 */
List<Account> findAll();
  1. AccountDao.xml配置
<mapper namespace="com.itheima.dao.AccountDao">
    <!-- 建立对应关系 -->
    <resultMap id="accountMap" type="Account">
        <id column="id" property="id"/>
        <result column="UID" property="uid"/>
        <result column="MONEY" property="money"/>

        <!-- 指定从表方的应用实体属性的
         select :填写需要调用的select 映射的id
         column: 填写需要传递给select 映射的参数
         -->
        <association property="user"
                     javaType="User"
                     select="com.itheima.dao.UserDao.findById"
                     column="uid">
        </association>
    </resultMap>

    <select id="findAll" resultMap="accountMap">
        select * from account
    </select>
    ...
  1. UserDao接口中对应的方法
/** 根据id查询用户数据 */
User findById(Integer userId);
  1. UserDao.xml对应的配置
<mapper namespace="com.itheima.dao.UserDao">
    <!-- 根据id查询用户信息 -->
    <select id="findById" resultType="User" parameterType="Integer">
        select * from user where id = #{uid}
    </select>
    ...
  1. AccountTest测试方法
private InputStream in;
private SqlSessionFactory factory;
private SqlSession session;
private AccountDao accountDao;

@Before
public void init() throws Exception {
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    factory = builder.build(in);
    session = factory.openSession();
    accountDao = session.getMapper(AccountDao.class);
}

@After
public void destory() throws Exception {
    session.commit();
    session.close();
}

@Test
public void testFindAll() {
    List<Account> accounts = accountDao.findAll();
}
  1. 运行测试结果:只进行了第一次查询
- ==>  Preparing: select * from account
- ==> Parameters:
- <==      Total: 6

三、使用Collection实现延迟加载

  1. UserDao接口中的方法
/** 查询所有用户,同时获取出每一个用户下的所有账户信息 */
List<User> findAll();
  1. UserDao.xml配置
<mapper namespace="com.itheima.dao.UserDao">

    <resultMap id="userMap" type="user">
        <id column="id" property="id"/>
        <result column="username" property="username"/>
        <result column="address" property="address"/>
        <result column="sex" property="sex"/>
        <result column="birthday" property="birthday"/>
        <!--
        collection: 是用于建议一对多集合属性的对应关系;主要用于加载关联的集合对象
        ofType: 用于指定集合元素的数据类型
        select: 【id】用于指定查询account列表的sql语句;是用于指定查询账户的唯一标识(账户的全限定类名加上方法名称)
        column: 【传递的参数】是用于指定使用哪个字段的值作为条件查询;指定select属性的sql语句的参数来源
        -->
        <collection property="accounts" ofType="Account"
                    select="com.itheima.dao.AccountDao.findByUid"
                    column="id">
        </collection>
    </resultMap>

    <!-- 配置查询所有操作 -->
    <select id="findAll" resultMap="userMap">
        select * from user
    </select>
</mapper>
  1. AccountDao接口中的方法
/** 根据用户id查询账户信息 */
List<Account> findByUid(Integer uid);
  1. AccountDao.xml配置
<mapper namespace="com.itheima.dao.AccountDao">

    <select id="findByUid" resultType="account" parameterType="INT">
        select * from account where uid = #{uid}
    </select>
</mapper>
  1. UserTest测试类
private InputStream in;
private SqlSessionFactory factory;
private SqlSession session;
private UserDao userDao;

@Before
public void init() throws Exception {
    in = Resources.getResourceAsStream("SqlMapConfig.xml");
    SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
    factory = builder.build(in);
    session = factory.openSession();
    userDao = session.getMapper(UserDao.class);
}

@After
public void destory() throws Exception {
    session.commit();
    session.close();
}

@Test
public void testFindAll() {
    List<User> all = userDao.findAll();
}
  1. 运行测试结果:只进行了第一次查询
- ==>  Preparing: select * from user
- ==> Parameters:
- <==      Total: 6
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容