添加二级缓存_配置及使用

项目添加二级缓存
1、需要引入三个jar包
在hibernate下能找到
hibernate-distribution-3.5.6-Final\lib\optional\ehcache\ehcache-1.5.0.jar
在srping下能找到
..\lib\concurrent\backport-util-concurrent.jar
..\lib\jakarta-commons\commons-logging.jar
2、在hibernate.cfg.xml中配置
(1)开启二级缓存:
<!-- 开启二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!-- 配置二级缓存的供应商 -->
<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<!-- 启动二级缓存的查询缓存 -->
<property name="hibernate.cache.use_query_cache">true</property>
(2)添加类级别的二级缓存:
<!-- 配置类级别的二级缓存 -->
<class-cache class="cn.itcast.elec.domain.ElecSystemDDL" usage="read-write"/>
3、测试二级缓存:
在junit包下进行测试,TestHibernateCache.java进行测试:
public class TestHibernateCache {
@Test
public void testCache(){
Configuration configuration = new Configuration();
configuration.configure();
SessionFactory sf = configuration.buildSessionFactory();
Session s = sf.openSession();
Transaction tr = s.beginTransaction();

Query query = s.createQuery("from ElecSystemDDL");
//使用查询缓存
query.setCacheable(true);
query.list();//产生select语句

tr.commit();
s.close();
////////////////////////////////////////////////////////////////////
s = sf.openSession();
tr = s.beginTransaction();

Query query1 = s.createQuery("from ElecSystemDDL");
//使用查询缓存
query1.setCacheable(true);
query1.list();//?

tr.commit();
s.close();

}
}
4、在项目中添加二级缓存:
在CommonDaoImpl中添加方法
/**使用二级缓存,提高系统的检索性能*/
public List<T> findCollectionByConditionNoPageWithCache(String condition,
final Object[] params, LinkedHashMap<String, String> orderby) {
/**
* SELECT * FROM elec_text o WHERE 1=1 #DAO层封装
AND o.textName LIKE ? #Service层封装
AND o.textRemark LIKE ? #Service层封装
ORDER BY o.textDate ASC,o.textName DESC #Service层封装
*/
//定义Hql语句
String hql = "from " + entityClass.getSimpleName() + " o where 1=1";
String orderHql = this.orderByHql(orderby);
final String finalHql = hql + condition + orderHql;
//执行hql语句
//方法一:
//List<T> list = this.getHibernateTemplate().find(hql,params);
//方法二:
List<T> list = (List<T>) this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(finalHql);
for(int i=0;params!=null && i<params.length;i++){
query.setParameter(i, params[i]);
}
//启用二级缓存存储数据
query.setCacheable(true);
return query.list();
}
});
return list;
}

使用实例

/**使用二级缓存,提高系统的检索性能*/
public List<T> findCollectionByConditionNoPageWithCache(String condition,
final Object[] params, LinkedHashMap<String, String> orderby) {
/**
* SELECT * FROM elec_text o WHERE 1=1 #DAO层封装
AND o.textName LIKE ? #Service层封装
AND o.textRemark LIKE ? #Service层封装
ORDER BY o.textDate ASC,o.textName DESC #Service层封装
*/
//定义Hql语句
String hql = "from " + entityClass.getSimpleName() + " o where 1=1";
String orderHql = this.orderByHql(orderby);
final String finalHql = hql + condition + orderHql;
//执行hql语句
//方法一:
//List<T> list = this.getHibernateTemplate().find(hql,params);
//方法二:
List<T> list = (List<T>) this.getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session)
throws HibernateException, SQLException {
Query query = session.createQuery(finalHql);
for(int i=0;params!=null && i<params.length;i++){
query.setParameter(i, params[i]);
}
//启用二级缓存存储数据
query.setCacheable(true);
return query.list();
}
});
return list;
}

添加二级缓存_配置及使用

时间: 2024-08-03 18:38:46

添加二级缓存_配置及使用的相关文章

二级缓存和配置原理

二级缓存是进程或集群范围内的缓存,可以被所有的Session共享 二级缓存是可配置的插件 01.二级缓存的配置使用(ehcache缓存) *1.引入如下jar包. ehcache-1.2.3.jar  核心库 backport-util-concurrent.jar commons-logging.jar *2.配置Hibernate.cfg.xml开启二级缓存 <property name="hibernate.cache.use_second_level_cache">

Hibernate的一级二级缓存机制配置与测试

特别感谢http://www.cnblogs.com/xiaoluo501395377/p/3377604.html 在本篇随笔里将会分析一下hibernate的缓存机制,包括一级缓存(session级别).二级缓存(sessionFactory级别)以及查询缓存,当然还要讨论下我们的N+1的问题. 随笔虽长,但我相信看完的朋友绝对能对hibernate的 N+1问题以及缓存有更深的了解. 一.N+1问题 首先我们来探讨一下N+1的问题,我们先通过一个例子来看一下,什么是N+1问题: list(

mybatis(4)_二级缓存深入_使用第三方ehcache配置二级缓存

增删改对二级缓存的影响 1.增删改也会清空二级缓存 2.对于二级缓存的清空实质上是对value清空为null,key依然存在,并非将Entry<k,v>删除 3.从DB中进行select查询的条件是: 1.缓存中根本不存在这个key 2.存在key对应的Entry,但是value为null 二级缓存的配置 <cache eviction="FIFO" flushInterval="60000" size="512" readOn

配置Hibernate的二级缓存

1.在applicationContex.xml文件里面添加二级缓存配置: <!-- 配置hibernate的sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"><ref bea

Hibernate二级缓存以及ehcache的搭建配置

前言 这次主要复习Hibernate的二级缓存的相关知识,配置以及使用.二级缓存主要采用第三方的ehcache,也将介绍ehcache缓存的相关配置属性以及在项目中的搭建,具体的项目查看下一篇的 Maven搭建SpringMVC+Hibernate项目详解 的文章.(之前使用过Hibernate的二级缓存,但是没自己搭建和研究过,现在花了半天时间搭建了一下,写下来供大家参考) 1.Hibernate二级缓存 Hibernate包括两个级别的缓存: 1.一级缓存:默认总是启用的session级别的

二级缓存配置和原理

二级缓存是进程或集群范围内的缓存,可以被所有的Session共享 二级缓存是可配置的插件 01.二级缓存的配置使用(ehcache缓存) *1.引入如下jar包. ehcache-1.2.3.jar  核心库 backport-util-concurrent.jar commons-logging.jar *2.配置Hibernate.cfg.xml开启二级缓存 <property name="hibernate.cache.use_second_level_cache">

Hibernate 二级缓存配置

详见:https://www.cnblogs.com/Junsept/p/7324981.html Hibernate的cache管理: Cache就是缓存,它往往是提高系统性能的最重要手段,对数据起到一个蓄水池和缓冲的作用.Cache对于大量依赖数据读取操作的系统而言尤其重要.在大并发量的情况下,如果每次程序都需要向数据库直接做查询操作,它们所带来的性能开销是显而易见的,频繁的网络舆,数据库磁盘的读写操作都会大大降低系统的性能.此时如果能让数据库在本地内存中保留一个镜像,下次访问的时候只需要从

hibernate缓存机制(二级缓存)

一.why(为什么要用Hibernate缓存?) Hibernate是一个持久层框架,经常访问物理数据库. 为了降低应用程序对物理数据源访问的频次,从而提高应用程序的运行性能. 缓存内的数据是对物理数据源中的数据的复制,应用程序在运行时从缓存读写数据,在特定的时刻或事件会同步缓存和物理数据源的数据. 二.what(Hibernate缓存原理是怎样的?)Hibernate缓存包括两大类:Hibernate一级缓存和Hibernate二级缓存. 1.Hibernate一级缓存又称为“Session的

【SSH网上商城项目实战16】Hibernate的二级缓存处理首页的热门显示

网上商城首页都有热门商品,那么这些商品的点击率是很高的,当用户点击某个热门商品后需要进入商品的详细信息页面,就像淘宝里面那样.那么每次点击都要去后台查询一下该商品的详细信息,就会发送相应的sql语句,每次刷新一下详细页面也会发sql语句,这样的话,性能肯定会受到很大的影响.那么使用Hibernate的二级缓存就可以解决这个问题. 有些人可能会想,我们可以使用重定向,这样的话,在用户第一次访问的时候把信息查出来放到session中,以后每次用户刷新就可以去session中拿了,这样就不用去数据库中