阅读975 返回首页    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查看绑定源码