Java框架:Mybatis:延時加載 + 緩存

延遲加載==懶加載==按需加載:

  • mybatis中,resultMap 標籤的association標籤(模型)和collection標籤(集合)都具有延遲加載功能

配置:在全局配置文件中

  •     <!--配置允許懶加載-->
        <settings>
            <setting name="lazyLoadingEnabled" value="true"/>
    
            //積極的懶加載,默認是true,設置爲false時,懶加載生效
    5       <setting name="aggressiveLazyLoading" value="false"/>
        </settings>
<!--  懶加載 -->
<resultMap id="orderLazyloadingRslMap" type="orders">
	<id column="id" property="id"/>
	<result column="name" property="name"/>
	<!--懶加載配置查詢  association針對模型  + 執行方法 -->
	<!--此處會進行懶加載,如果不用user則不會執行findUserById方法,
       如果用到,則執行,並將column對應的列值作爲user的屬性傳入findUserById方法參數 -->
	<association property="user" select="com.zjc.mapper.UserMapper.findUserById" column="user_id"/>
</resultMap>	

<select id="findOrderAndUserByLazyloading" resultMap="orderLazyloadingRslMap">
	SELECT * FROM orders
</select>

緩存關係

一級緩存:默認開啓    session級別

  • 一級緩存指的是sqlsession,在sqlsession中有一個數據區域,是map結構,這個區域就是一級緩存區域
  • key:sql語句、條件、statement等信息組成一個  唯一值
  • value:查詢出的結果對象
  • 只有執行 增、 刪、 改 操作纔會清空緩存

二級緩存:手動配置   session工廠級別

配置:全局配置文件

  • <!--允許開啓二級緩存-->
    <setting name="cacheEnabled" value="true"/>

 

  • mapper中配置緩存開啓,默認是perpetualCache實現Cache接口
    <cache></cache>    
    <select id="findUserById" parameterType="int" resultType="user">
          SELECT * FROM user WHERE id = #{id}
    </select>
  • 二級緩存指的是同一個namespace下的mapper,map結構
  • 模型model要實現序列化接口 Serializable
  • 執行 增、 刪、 改 操作纔會清空緩存

指定方法禁用二級緩存   useCache="false"

  •     <select id="findUserById" parameterType="int" resultType="user" useCache="false">
            SELECT * FROM user WHERE id = #{id}
        </select>

刷新緩存   flushCache

  • 默認flushCache="true",插入,更新,刪除,會清空二級緩存;
  • 設置flushCache="false",插入,更新,刪除,不會清空二級緩存 
       <insert id="save" parameterType="user" flushCache="false">
          INSERT INTO user (username,sex,birthday,address)
            VALUE (#{username},#{sex},#{birthday},#{address})
       </insert>

二級緩存應用場景:訪問響應速度要求高,但實時性不高的查詢

  • cache標籤中有一個flashInterval屬性)來定時刷新二級緩存,單位爲毫秒


整合 ehcache :分佈式緩存框架

  • 分佈式緩存框架:redis 、menmcached、  ehcache
  • 應用:對緩存數據集中管理,集羣模式

原理:

  • Cache緩存是一個接口,mybatis默認實現類是PerpetualCache。整合mybatis的二級緩存,那麼用其他實現類實現Cache接口即可

步驟:

  • 1.導入jar包   mybatis-ehcache-1.0.2.jar    ehcache-core-2.6.5.jar
  • 2.src下添加ehcache.xml配置文件
  • <ehcache>
        <diskStore path="java.io.tmpdir"/>
        <defaultCache
                maxElementsInMemory="10000"
                eternal="false"
                timeToIdleSeconds="120"
                timeToLiveSeconds="120"
                maxElementsOnDisk="10000000"
                diskExpiryThreadIntervalSeconds="120"
                memoryStoreEvictionPolicy="LRU">
            <persistence strategy="localTempSwap"/>
        </defaultCache>
    </ehcache>
  • 3.指定mapper下配置
  •     <!-- 配置Ehcached緩存,type不寫,默認使用mybaits自帶緩存:perpetualCache,此處改爲ehcache緩存 -->
        <cache type="org.mybatis.caches.ehcache.EhcacheCache"></cache>

附錄:ehcache配置說明

配置項 說明
maxElementsInMemory 設置基於內存的緩存中可存放的對象最大數目
eternal 設置對象是否爲永久的,true表示永不過期
timeToIdleSeconds 設置對象空閒最長時間,單位:秒;超過則對象過期,並從緩存中清除;值爲0,表示對象可以無限期處於空閒狀態
timeToLiveSeconds 設置對象生存最長時間,超過則對象過期;值爲0,表示對象可以無限期地存在於緩存中。該屬性值必須大於或等於 timeToIdleSeconds 屬性值
overflowToDisk 設置基於內在的緩存中的對象數目達到上限後,是否把溢出的對象寫到基於硬盤的緩存中
diskPersistent 當jvm結束時是否持久化對象 true false 默認是false
diskExpiryThreadIntervalSeconds  指定專門用於清除過期對象的監聽線程的輪詢時間
memoryStoreEvictionPolicy 當內存緩存達到最大,有新的element加入的時候, 移除緩存中element的策略。默認是LRU(最近最少使用),可選的有LFU(最不常使用)和FIFO(先進先出)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章