閱讀975 返回首頁    go 阿裏雲 go 技術社區[雲棲]


Hibernate整合spring配置二級緩存

applicationContext.xml 文件配置:

<prop key="hibernate.cache.use_second_level_cache">true</prop>   <!--設置緩存機製為二級緩存 -->
<prop key="hibernate.cache.use_query_cache">true</prop>          <!--啟動查詢緩存 -->
<prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>  <!--設置二級緩存的Provider類 -->
<prop key="hibernate.cache.provider_configuration_file_resource_path">WEB-INF/classes/ehcache.xml</prop>   <!--設置緩存的配置文件路徑 -->

將ehcache.xml文件放到src下麵,並配置ehcache.xml文件如下:

<ehcache>
    <diskStore path="D:\cache" />

    <defaultCache maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="3600" timeToLiveSeconds="3600"
        overflowToDisk="true" diskPersistent="false"
        diskExpiryThreadIntervalSeconds="120"
        memoryStoreEvictionPolicy="LRU" />
    <cache name="net.52itstyle.entity.DataAttr"  
        maxElementsInMemory="800" eternal="false" overflowToDisk="false"  
        timeToIdleSeconds="3600" timeToLiveSeconds="3600"   /> 
</ehcache>

在設置了緩存機製的類的xml文件中添加一段代碼<cache usage="read-write" region="net.52itstyle.entity.SmProduct/>"代碼如下:

<hibernate-mapping>
    <!--SM_PRODUCT表的hibernate映射描述文件 -->
    <class name="net.nk.entity.SmProduct" table="SM_PRODUCT" >
        <cache usage="read-write" region="net.nk.entity.SmProduct"/>
         <!-- ID -->
        <id name="id" type="string">
            <column name="ID" />
            <generator  />
        </id>
最後可以 通過放開hibernate的show_sql來查看是否緩存機製生效了

可能出現錯誤:

Java代碼 Exception?in?thread?"main"?java.lang.NoClassDefFoundError:?edu/emory/mathcs/backport/java/util/concurrent/BlockingQueue 

下載jar包:點擊打開鏈接


最後更新:2017-04-03 08:26:28

  上一篇:go 【已解決】Win7中無法和VirtualBox中的Ubuntu 13.04共享文件夾
  下一篇:go android查看綁定源碼