0
点赞
收藏
分享

微信扫一扫

【MyBaits】4、延迟加载、MyBatis 的缓存

杏花疏影1 2023-06-16 阅读 39

目录

一、延迟加载

🍃 关联对象(associationcollection)可以实现延迟加载

🍃 实现延迟加载需设置如下属性

  • fetchType:设置为 lazy
  • select:指定一个 select 标签的 id(用于查询需要延迟加载的记录)
  • column:指定一个列的值(作为 select 查询时传入的参数值)

🍃 一般会给 collection设置延迟加载

<mapper namespace="person">

    <resultMap id="rmGet" type="Person">
        <id property="id" column="id"/>
        <result property="name" column="name"/>
        <!-- 身份证 -->
        <association property="idCard" javaType="IdCard">
            <id property="id" column="id"/>
            <result property="no" column="id_card_no"/>
            <result property="address" column="id_card_address"/>
        </association>
        <!--银行卡-->
        <collection property="bankCards"
                    fetchType="lazy"
                    column="id"
                    select="bankCard.getByPersonId"
                    ofType="BankCard"/>
        <!--职业-->
        <collection property="jobs"
                    fetchType="lazy"
                    column="id"
                    select="job.getByPersonId"
                    ofType="Job"/>
    </resultMap>
    <select id="get" resultMap="rmGet" parameterType="long">
        SELECT
        p.*,
        ic.id id_card_id,
        ic.`no` id_card_no,
        ic.address id_card_address
        FROM
        person p
        JOIN id_card ic ON p.id = ic.person_id
        WHERE
        p.id = #{id}
    </select>

</mapper>
<mapper namespace="bankCard">

    <select id="getByPersonId" resultType="BankCard">
        SELECT
        *
        FROM
        bank_card
        WHERE person_id = #{personId}
    </select>

</mapper>
<mapper namespace="job">

    <select id="getByPersonId" resultType="Job">
        SELECT
        j.*
        FROM
        person p
        JOIN person_job pj ON pj.person_id = p.id
        JOIN job j ON pj.job_id = j.id
        AND p.id = #{personId}
    </select>

</mapper>

二、全局延迟加载开关

<configuration>
    <settings>
        <!--所有设置了select属性的关联对象都延迟加载-->
        <setting name="lazyLoadingEnabled" value="true"/>
    </settings>
</configuration>

🍀全局延迟加载开关在 mybatis-config.xml 的 setting 标签中配置
🍀 <setting name="lazyLoadingEnabled" value="true"/>

🍀 <setting name="aggressiveLazyLoading" value="false"/>

三、缓存

🌼 缓存是为了减少数据库直接访问次数,提高访问效率而临时存储在内存中的数据

🌼 适合存放到缓存中的数据:经常查询、不经常改变、数据的正确性对最终结果影响不大的数据

🌼不适合放在缓存中的数据:商品库存、股票/黄金的价格、汇率


🌼 MyBatis 的缓存分为【用于缓存 select 的结果】:
① 一级缓存
② 二级缓存

(1) 一级缓存【自动开启】

🌿 一级缓存存放在 SqlSession 对象中

  • 同一个 SqlSession 的 select 共享缓存

  • 当 SqlSession 关闭的时候,缓存失效

  • 执行 insert、update、delete、commit 等方法的时候会自动清理一级缓存

  • 一般情况下,每次查询用的都是不同的 SqlSession,所以 一级缓存的命中率并不高

(2) 二级缓存

☀️ 为了提高缓存的命中率,可以考虑手动开启 MyBatis 的二级缓存
☀️ 它是 namespace(mapper)级别的缓存

  • 同一个 namespace 下的 select 共享缓存
  • 默认情况下:namespace 下的 update、insert、delete 若执行成功,会自动清理二级缓存

🌺 开启二级缓存

  • 在 mybatis-config.xml 文件中配置
<configuration>
    <settings>
        <!--开启二级缓存-->
        <setting name="cacheEnabled" value="true"/>
    </settings>
</configuration>
  • 在映射文件的 mapper 中添加 cache 标签(默认会缓存映射文件中的所有 select 的结果)
<mapper namespace="job">
    <cache readOnly="true"/>
</mapper>

(3) useCache 和 flushCache

可通过设置 useCache 属性来决定某个 select 是否需要开启二级缓存

可通过设置 flushCache 属性来决定某个操作后是否需要清除缓存

举报

相关推荐

0 条评论