mybatis lazy loading mechanism

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/caoxuecheng001/article/details/102586797

The so-called lazy loading is when two objects associated with the query, the query does not immediately come out objects associated with it, but when used in the query again. For example, we check the user information does not need to check account information, this time uses lazy loading.

1, enable lazy loading mechanism in the global configuration file

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

2、

Not immediately query the user query account

IAccountDao.xml

   <!-- 定义封装account和user的resultMap -->
    <resultMap id="accountUserMap" type="account">
        <id property="id" column="id"></id>
        <result property="uid" column="uid"></result>
        <result property="money" column="money"></result>
        <!-- 一对一的关系映射:配置封装user的内容
        select属性指定的内容:查询用户的唯一标识:
        column属性指定的内容:用户根据id查询时,所需要的参数的值
        -->
        <association property="user" column="uid" javaType="user" select="com.itheima.dao.IUserDao.findById"></association>
    </resultMap>

    <!-- 查询所有 -->
    <select id="findAll" resultMap="accountUserMap">
        select * from account
    </select>

IAccountDao.java

List<Account> findAll();

IUserDao.java

User findById(Integer userId);

IUserDao.xml

    <!-- 根据id查询用户 -->
    <select id="findById" parameterType="INT" resultType="user">
        select * from user where id = #{uid}
    </select>

3, does not query the user account immediately upon inquiry

IUserDao.xml


    <!-- 定义User的resultMap-->
    <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>
        <!-- 配置user对象中accounts集合的映射 -->
        <collection property="accounts" ofType="account" select="com.itheima.dao.IAccountDao.findAccountByUid" column="id"></collection>
    </resultMap>

    <!-- 查询所有 -->
    <select id="findAll" resultMap="userAccountMap">
        select * from user
    </select>

IUserDao.java

  List<User> findAll();

IAccountDao.java

   List<Account> findAccountByUid(Integer uid);

IAccountDao.xml

    <!-- 根据用户id查询账户列表 -->
    <select id="findAccountByUid" resultType="account">
        select * from account where uid = #{uid}
    </select>

This realization of lazy loading, is to configure and select the label in association collection, the label to another query properties associated with the object. The best one is loaded in practical applications at once, and you can open many lazy loading, specific also depends on the actual situation.

Guess you like

Origin blog.csdn.net/caoxuecheng001/article/details/102586797