Hibernate中的缓存机制
2017-09-24 10:34
281 查看
表现层:struts2
业务逻辑层:
持久层:Hibernate
Hibernate中的缓存机制
一:一级缓存:默认是开启的,不需要做任何的配置
session.evict(news); 清除单个缓存
session.clear(); 清除所有缓存
true
2. 根据需要配置二级缓存的提供商 常用ehcache oscache
需要引入ehcache依赖的jar包
在Hibernate.cfg.xml中进行配置 指定二级缓存的提供商
org.hibernate.cache.internal.EhCacheRegionFactory
3. 引入ehcache.xml文件
4.在需要放入二级缓存的类的映射文件中加上
true
2. 在查询方法中Query语句执行,返回值前调用.setCacheable(true)
eg:通过id查询
public News getNewsByIdQuery(long newsId){
News n=null;
Session session=null;
try{
session=SessionFactoryUtils.openSession();
String hql=”from News where id=”+newsId;
n=(News)session.createQuery(hql)
.setCacheable(true) //返回值前调用.setCacheable(true)
.uniqueResult();
}catch(Exception e){
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
return n;
}
业务逻辑层:
持久层:Hibernate
Hibernate中的缓存机制
一:一级缓存:默认是开启的,不需要做任何的配置
session.evict(news); 清除单个缓存
session.clear(); 清除所有缓存
二:二级缓存:默认是没有开启的,需要手工配置 1. 需要在Hibernate.cfg.xml中进行配置
true
2. 根据需要配置二级缓存的提供商 常用ehcache oscache
需要引入ehcache依赖的jar包
在Hibernate.cfg.xml中进行配置 指定二级缓存的提供商
org.hibernate.cache.internal.EhCacheRegionFactory
3. 引入ehcache.xml文件
4.在需要放入二级缓存的类的映射文件中加上
三:查询缓存: 1.在hibernate.cfg.xml配置文件中配置以下代码
true
2. 在查询方法中Query语句执行,返回值前调用.setCacheable(true)
eg:通过id查询
public News getNewsByIdQuery(long newsId){
News n=null;
Session session=null;
try{
session=SessionFactoryUtils.openSession();
String hql=”from News where id=”+newsId;
n=(News)session.createQuery(hql)
.setCacheable(true) //返回值前调用.setCacheable(true)
.uniqueResult();
}catch(Exception e){
e.printStackTrace();
}finally{
if(session!=null){
session.close();
}
}
return n;
}
相关文章推荐
- Hibernate的缓存机制介绍
- Hibernate 所有缓存机制详解
- Hibernate+spring缓存机制配置
- Hibernate——脏检查和缓存清理机制
- Hibernate——脏检查和缓存清理机制
- Hibernate的缓存机制
- Hibernate的缓存机制
- Hibernate的缓存机制
- 关于hibernate一级,二级缓存以及事务隔离机制。
- Hibernate 缓存机制续 - 查询缓存
- Hibernate 缓存机制
- Hibernate 缓存机制
- Hibernate中的缓存机制
- Hibernate 所有缓存机制详解
- Hibernate-缓存机制详解
- Hibernate学习笔记之----理解对象的持久性与一级缓存机制
- Hibernate的缓存机制
- hibernate的缓存机制
- hibernate的缓存机制
- [JavaEE]Hibernate 所有缓存机制详解