最近项目中需要处理hibernate缓存:
网络上看了些文章 总结下:
大道理,原理就不说了,写点直接用的。
第一种方法:
Spring 中配置
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.MySQLDialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.max_fetch_depth">0</prop>
<prop key="hibernate.generate_statistics">true</prop>
<prop key="hibernate.cache.use_query_cache">true</prop>
<prop key="hibernate.cache.use_second_level_cache">true</prop>
<prop key="cglib.use_reflection_optimizer">false</prop>
<prop key="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</prop>
<prop key="hibernate.query.factory_class">org.hibernate.hql.ast.ASTQueryTranslatorFactory</prop>
</props>
</property>
<!-- 配置hibernate 模板代码 -->
<bean id="hibernateTemplate"
class="org.springframework.orm.hibernate3.HibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
<property name="cacheQueries" value="true"></property>
</bean>
Src 下 ehcache.xml
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<diskStore path="java.io.tmpdir" />
<defaultCache
maxElementsInMemory="10000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="300"
timeToLiveSeconds="180"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120" />
</ehcache>
实体配置:
@Entity
@Cache(usage=CacheConcurrencyStrategy.READ_WRITE)
(1) usage: 提供缓存对象的事务隔离机制,可选值有以下几种
(NONE, READ_ONLY, NONSTRICT_READ_WRITE, READ_WRITE, TRANSACTIONAL)
(2) region (optional): 指定缓存的区域,默认是类的全限定名。利用缓存区域,可以更精确的指定每个区域的缓存超前策略。如果指定了缓存区域前缀(在 hibernate.cfg.xml中设置cache.region_prefix属性为一个字符串),则所有的缓存区域名前将加上这个前缀。
(3) include (optional): all to include all properties, non-lazy to only include non lazy properties (default all).
如果不是使用annotation的话,则是在Hbm文件中添加cache usage="read-only"
此种方法制定类(对象)的缓存
第二种方法:
Spring 配置文件一样:
添加java方法:
public List findByHqlForCache(String queryString){
Session session = this.getSession();
Query query = session.createQuery(queryString);
query.setCacheable(true);
List list = query.list();
session.flush();
return list;
}
在使用的时候引用上面的方法,此种方式制定那条HQL 的缓存
分享到:
相关推荐
传播智客,为Spring集成的Hibernate配置二级缓存、cache.xml
memcache的客户端,用maven构建,里有用hibernate-memcached与hibernate,spring3.0进行配置,也有与spring3.0单独配置。
NULL 博文链接:https://jyao.iteye.com/blog/1299178
为Spring集成的Hibernate配置二级缓存
此配置和包,是springMVC4.3.3 +spring4+hibernate5.1.3+二级缓存ehcache(不用可以关闭)+fastjson。 是正常运行的项目里拷出来的,方便大家使用。
7:Hibernate查询缓存Query Cache及实现
基于springboot最新版本2.1.4.RELEASE,采用jpa为dao层框架,配置redis(redisson)为hibernate的二级缓存。
Spring4+Hibernate4二级缓存实例源码.
在Spring+Hibernate集成环境中使用EhCache缓存做的开发测试,在线程中打开数据库查询数据,及关闭数据库后查询数据的结果.
5.单数据源配置(兼容Tomcat和Weblogic)。 6.Hibernate继承 HibernateDaoSupport。 7.Spring+Junit4单元测试,优点:不会破坏数据库现场,等等。 2)Demo 导入说明: 1.Eclipse Encoding:GBK 2.Eclipse 导入后可能...
NULL 博文链接:https://microjava.iteye.com/blog/525981
Spring 整合 Hibernate 时启用二级缓存实例详解 写在前面: 1. 本例使用 Hibernate3 + Spring3; 2. 本例的查询使用了 HibernateTemplate; 1. 导入 ehcache-x.x.x.jar 包; 2. 在 applicationContext.xml ...
使用spring+mvc+hibernate集成redis缓存,如有需要详细使用阅读内容可以访问我的博客来查看http://blog.csdn.net/wohuozheng?ref=toolbar
完整的spring +springmvc+hibernate+shiro项目实例,详细的shiro配置介绍,通过redis管理用户session缓存。。。。。。。。
其中包含apache的log4j记录日志信息,spring管理组件,springmvc分层,springaop配置数据库事务控制,hibernate二级缓存配置,实现了查询,用户登录注册,请求验证是否登录等基础功能Demo,基于后台测试,使用前台...
struts2+spring+hibernate集成例子,包含所有jar包,ehcache二级缓存,mysql数据,需要自己创建
一个完整的Spring+Hibernate+FreeMarker环境的搭建过程。 包括了国际化配置、缓存配置、事物配置等功能
实例中有spring整合hibernate的实例,并且配置了hibernate的二级缓存,希望对大家有所帮助
本版本全面更新了jar包,全部使用了当前最新版本的jar包,struct2.1.8 spring3 hibernate3.5,全面使用注解取代xm的l配置。 另外增加了一个ant构建脚本,支持使用hudson完成每日构建,持续集成,自动测试,代码规范...
(2)运用struts1.2+hibernate+spring 框架,数据库连接池,事务管理; (3)Struts 应用国际化,Struts 标签库与Tiles框架, JSTL标签库,Spring IOC; (4)采用优化性能技术,采用oscache缓存,freemarker静态页面生成; (5)...