Hibernate+spring缓存机制配置
来源:互联网 发布:java面向过程实现加法 编辑:程序博客网 时间:2024/06/07 00:54
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.nk.entity.DataAttr"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.SmContentCategory"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.SmProduct"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.SmDepartment"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.SmUser"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.DataAttrValue"
maxElementsInMemory="3000" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.DataAm"
maxElementsInMemory="800" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
<cache name="net.nk.entity.DataAttrGroup"
maxElementsInMemory="400" eternal="false" overflowToDisk="false"
timeToIdleSeconds="3600" timeToLiveSeconds="3600" />
</ehcache>
在设置了缓存机制的类的xml文件中添加一段代码<cache usage="read-write" region="net.nk.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 class="assigned" />
</id>
其中类的xml文件中的region设置要和ehcache.xml文件中的保持一致,此处是通过包名+类名的方式。
在调用数据库数据方法时,可采取以下设置:
protected List<POJO> getAll(Class<T> entityClass,boolean iscache) throws SSHException {
HibernateTemplate ht = getHibernateTemplate();
if(iscache){
ht.setCacheQueries(true);
}
return ht.find("from "+entityClass.getName());//getHibernateTemplate().loadAll(entityClass);
}
至此,可以通过放开hibernate的show_sql来查看是否缓存机制生效了
转自:https://www.cnblogs.com/xiangpiaopiao2011/archive/2011/12/15/2289222.html
阅读全文
0 0
- Hibernate+spring缓存机制配置
- Hibernate+spring缓存机制配置
- Hibernate+spring缓存机制配置
- spring+hibernate缓存配置
- Hibernate和Spring的缓存机制
- Hibernate+spring配置ehcache缓存的方式
- spring缓存机制(EhCache配置方式)
- Hibernate缓存机制介绍
- Hibernate:缓存机制解析
- Hibernate:缓存机制解析
- Hibernate的缓存机制
- Hibernate的缓存机制
- Hibernate的缓存机制
- HIbernate缓存机制
- Hibernate的缓存机制
- hibernate的缓存机制
- Hibernate缓存机制
- Hibernate缓存机制2
- Visual Studio 调试常用20个小技巧
- 彻底理解I/O架构
- YTU.3131: 进阶递归之简单的整数划分问题
- environment variable 'STAGING_DIR' not defined
- c++ 简单进程交互(获取双方进程句柄)
- Hibernate+spring缓存机制配置
- ASP.NET使用JQUERY AJAX文件上传DEMO_异步文件上传例子
- java--多线程练习题
- http://blog.csdn.net/dragonxiangfu/article/details/14120785
- Gulp的安装与配置
- 刚上线的“新睿云”到底如何达到高可用?IOPS高达200000?
- 【云隐】STM32F103C8T6实现串口IAP方式升级固件
- HTTP协议:概括性知识
- 一个Activity分别加载多个layout布局文件