Java_myBatis_延迟加载

Posted 张啊咩

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java_myBatis_延迟加载相关的知识,希望对你有一定的参考价值。

使用mybatis的延迟加载,需要两个步骤:

1.在全局配置文件中添加一下语句(lazyLoadingEnabled默认为false,aggressiveLazyLoading默认为true)

<settings>
    <!-- 延迟加载总开关 -->
    <setting name="lazyLoadingEnabled" value="true"/>
    <!-- 侵入式延迟 -->
    <setting name="aggressiveLazyLoading" value="false"/>
</settings>

2.写好映射文件

<resultMap type="User" id="CacheTest">
    <id column="id" property="id" />
    <result column="username" property="username" />
    <result column="birthday" property="birthday" />
    <result column="address" property="address" />
    <!-- column相当于传入parameter  ofType相当于resultType -->
    <collection property="orderList" column="id" ofType="Order"
        select="resultMap.resultMapMapper.CacheTestSelect">
        <result column="number" property="number" />
        <result column="createtime" property="createtime" />
    </collection>
</resultMap>
<select id="CacheTest" resultMap="CacheTest">
    select * from user
</select>
<select id="CacheTestSelect" parameterType="int" resultType="Order">
    select * from orders where user_id = #{id}
</select>

这样查询一开始会执行select * from user

然后当读取到orderList时才会执行select * from orders where user_id = #{id}

以上是关于Java_myBatis_延迟加载的主要内容,如果未能解决你的问题,请参考以下文章

片段布局加载延迟

Java_myBatis_入门写法

Java_myBatis_全局配置文件

Java_myBatis_一对多映射

Java_MyBatis

延迟弹出对话框片段,直到其中的 webview 完成加载