博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
spring中配置二级缓存
阅读量:4067 次
发布时间:2019-05-25

本文共 1893 字,大约阅读时间需要 6 分钟。

1.首先,在spring的hibernate配置里(我的是applicationContext-hibernate.xml) 加上如下属性:

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">         

  <property name="dataSource">
   <ref bean="dataSource"/>
  </property>
  <property name="mappingResources">
   <list>
    <value>org/appfteaching/model/TArticleclass.hbm.xml</value>
   </list>
  </property>
  <property name="hibernateProperties">
   <props>
    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
          <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
          <prop key="hibernate.jdbc.fetch_size">${hibernate.jdbc.fetch_size}</prop>
          <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
          <prop key="hibernate.cache.use_query_cache">true</prop>
          <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
   </props>   
  </property>  
 </bean> 

2.其次,在src目录下的ehcache.xml中配置如下信息(如果是默认ehcache.xml则会有<cache name="sampleCache1">和<cache name="sampleCache2>",去掉)

<cache name="org.hibernate.cache.StandardQueryCache"

        maxElementsInMemory="10000"
        eternal="false"
        timeToIdleSeconds="300"
        timeToLiveSeconds="4200"
        overflowToDisk="true"
        />

    <!-- Sample cache named sampleCache2

        This cache contains 1000 elements. Elements will always be held in memory.
        They are not expired. -->

    <cache name="org.hibernate.cache.UpdateTimestampsCache"

        maxElementsInMemory="5000"
        eternal="true"
        timeToIdleSeconds="0"
        timeToLiveSeconds="0"
        overflowToDisk="false"
        />

3.将你要缓存的model加进ehcache.xml里

<cache name="org.appfteaching.model.TArticleclass"

     maxElementsInMemory="1000"
        eternal="false"
        timeToIdleSeconds="100"
        timeToLiveSeconds="4200"
        overflowToDisk="true"
     />  

4.最后一步,在TArticleclass.hbm.xml里加上

<cache usage="read-write"/>

启动Tomcat,如发现如下错误

Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.

Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.

则是第二步没有做,加上即可.配置完毕

转载地址:http://ywaji.baihongyu.com/

你可能感兴趣的文章
出现( linker command failed with exit code 1)错误总结
查看>>
iOS开发中一些常见的并行处理
查看>>
iOS获取手机的Mac地址
查看>>
ios7.1发布企业证书测试包的问题
查看>>
如何自定义iOS中的控件
查看>>
iOS 开发百问
查看>>
Mac环境下svn的使用
查看>>
github简单使用教程
查看>>
如何高效利用GitHub
查看>>
环境分支-git版本管理
查看>>
uni-app 全局变量
查看>>
js判断空对象的几种方法
查看>>
java 不用递归写tree
查看>>
springboot2 集成Hibernate JPA 用 声明式事物
查看>>
fhs-framework jetcache 缓存维护之自动清除缓存
查看>>
SpringBoot 动态编译 JAVA class 解决 jar in jar 的依赖问题
查看>>
fhs-framework springboot mybatis 解决表关联查询问题的关键方案-翻译服务
查看>>
ZUUL2 使用场景
查看>>
Spring AOP + Redis + 注解实现redis 分布式锁
查看>>
elastic-job 和springboot 集成干货
查看>>