延时加载

一对一实现延时加载

通过account寻找唯一的user

  • 首先在user中添加findById的接口.
<select id="findById" parameterType="INT" resultType="com.oylong.domain.User">
    select * from user where id = #{id}
</select>
  • 然后更改Accound映射文件的配置,将resultMap标签改为如下配置
<resultMap id="accountUserMap" type="com.oylong.domain.Account">
    <id property="id" column="id"></id>
    <result property="uid" column="uid"></result>
    <result property="money" column="money"></result>

    <association property="user" column="uid" javaType="com.oylong.domain.User" select="com.oylong.dao.UserDao.findById"></association>
</resultMap>

我们可以看到,association标签有了变化,加了个select属性,它直接对应到了UserDao中的findById属性,而这个id的值就是我们制定的,cloumn的值,即为uid,封装成的类型为javaType中的类型,即为User

  • 运行的代码如下
        ...
        AccountDao accountDao = session.getMapper(AccountDao.class);
  
        List<Account> accountUser = accountDao.findAll();

        session.commit();
        ...

运行结果:

2020-03-24 09:21:50,951 307 [ main] DEBUG .oylong.dao.AccountDao.findAll - ==> Preparing: select * from account
2020-03-24 09:21:50,988 344 [ main] DEBUG .oylong.dao.AccountDao.findAll - ==> Parameters:
2020-03-24 09:21:51,020 376 [ main] DEBUG om.oylong.dao.UserDao.findById - ====> Preparing: select * from user where id = ?
2020-03-24 09:21:51,021 377 [ main] DEBUG om.oylong.dao.UserDao.findById - ====> Parameters: 46(Integer)
2020-03-24 09:21:51,024 380 [ main] DEBUG om.oylong.dao.UserDao.findById - <==== Total: 1
2020-03-24 09:21:51,025 381 [ main] DEBUG om.oylong.dao.UserDao.findById - ====> Preparing: select * from user where id = ?
2020-03-24 09:21:51,025 381 [ main] DEBUG om.oylong.dao.UserDao.findById - ====> Parameters: 41(Integer)
2020-03-24 09:21:51,026 382 [ main] DEBUG om.oylong.dao.UserDao.findById - <==== Total: 1
2020-03-24 09:21:51,027 383 [ main] DEBUG .oylong.dao.AccountDao.findAll - <== Total: 4

我们可以看到,执行了3条sql语句。

  • 延时加载配置
    只需要在Mybatis的全局配置文件中添加如下配置即可实现延时加载:
<configuration>
    <settings>
        <setting name="lazyLoadingEnabled" value="true" />
        <setting name="aggressiveLazyLoading" value="false" />
    </settings>
...
</configuration>

添加完之后为们在看下执行结果

2020-03-24 09:29:57,524 273 [ main] DEBUG .oylong.dao.AccountDao.findAll - ==> Preparing: select * from account
2020-03-24 09:29:57,554 303 [ main] DEBUG .oylong.dao.AccountDao.findAll - ==> Parameters:
2020-03-24 09:29:57,608 357 [ main] DEBUG .oylong.dao.AccountDao.findAll - <== Total: 4

可以看到,只执行了一条sql语句,这样为们就实现了延时加载,并且增加了我们的执行效率。

一对多实现延时加载

同样的更改方法,将resultMapsql语句修改后,即可实现。
如下:

<resultMap id="resultmap" type="com.oylong.domain.User">
    <id property="id" column="id"></id>
    <result property="username" column="username"></result>
    <result property="birthday" column="birthday"></result>
    <result property="sex" column="sex"></result>
    <result property="address" column="address"></result>

    <collection property="accounts" ofType="com.oylong.domain.Account" column="id" select="com.oylong.dao.AccountDao.findByUid"> </collection>
</resultMap>
<select id="findAccounts" resultMap="resultmap" parameterType="com.oylong.domain.User">
    select * from user
</select>

AccountDao的findByUid

<select id="findByUid" resultType="com.oylong.domain.Account">
        select * from account where id = #{uid}
</select>
Last modification:April 4, 2021
If you think my article is useful to you, please feel free to appreciate