Hibernate+ehcache二级缓存技术

1、首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:
<? xml version="1.0" encoding="UTF-8" ?>  
< ehcache >
    
< diskStore  path ="java.io.tmpdir"   />
    
< defaultCache 
    
maxElementsInMemory ="10000"  
    eternal
="false"
    timeToIdleSeconds
="1"  
    timeToLiveSeconds
="1"  
    overflowToDisk
="true"   />
    
< cache  name ="com.prl.entity.SGpstraceinfo"  
        maxElementsInMemory
="10000"
        eternal
="false"  
        timeToIdleSeconds
="300"
        timeToLiveSeconds
="600"
        overflowToDisk
="true"   />
    
< cache  name ="com.prl.entity.SLbstraceinfo"  
        maxElementsInMemory
="10000"
        eternal
="false"  
        timeToIdleSeconds
="300"
        timeToLiveSeconds
="600"
        overflowToDisk
="true"   />
    
< cache  name ="org.hibernate.cache.UpdateTimestampsCache"
        maxElementsInMemory
="5000"
        eternal
="true"
        overflowToDisk
="true" />
    
< cache  name ="org.hibernate.cache.StandardQueryCache"
        maxElementsInMemory
="10000"
        eternal
="false"
        timeToLiveSeconds
="120"
        overflowToDisk
="true" />     
</ ehcache >
(注意里面的cache name与*.hbm.xml里面的region相对应)

2、在 applicationContext.xml 配置文件中加入一个bean:
     < bean  id ="hibernateTemplate"
            class
="org.springframework.orm.hibernate3.HibernateTemplate" >
            
< property  name ="sessionFactory" >
               
< ref  bean ="sessionFactory"   />
            
</ property >
            
< property  name ="cacheQueries" >
               
< value > true </ value >
            
</ property >
    
</ bean >  
         < property  name ="hibernateProperties" >
            
< props >
                
< prop  key ="hibernate.dialect" >
                    org.hibernate.dialect.Oracle9Dialect
                
</ prop >
                
< prop  key ="hibernate.c3p0.min_size" > 5 </ prop >  
                
< prop  key ="hibernate.c3p0.max_size" > 20 </ prop >  
                
< prop  key ="hibernate.c3p0.timeout" > 1800 </ prop >  
                
< prop  key ="hibernate.c3p0.max_statements" > 50 </ prop >                 
                
< prop  key ="hibernate.show_sql" > false </ prop >
                
< prop  key ="hibernate.cache.use_query_cache" > true </ prop >
                
< prop  key ="hibernate.cache.provider_class" > org.hibernate.cache.EhCacheProvider </ prop >             
                
            
</ props >
        
</ property >

(说明一下:如果不设置“查询缓存”,那么 hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置
hibernate.cache.use_query_cache true 才行 )

3. 在你想设置缓存的pojo的dao中修改如下:(去掉sessionFactoty属性,加入hibernateTemplate属性)
     < bean id = " SLbstraceinfoDAO "
        
class = " com.prl.entity.SLbstraceinfoDAO " >
        
<!--   
        
< property name = " sessionFactory " >
            
< ref  bean = " sessionFactory "   />
        
</ property >
        
-->
        
< property name = " hibernateTemplate " >
                   
< ref  bean = " hibernateTemplate "   />
        
</ property >         
    
</ bean >

4.在SLbstraceinfo.hbm.xml里加入:(注意必须在class节点下加入)

     < class  name ="com.prl.entity.SLbstraceinfo"  table ="S_LBSTRACEINFO"  schema ="HTGIS" >
    
< cache  usage ="read-write"  region ="com.prl.entity.SLbstraceinfo" />  

5.java代码里激活缓存:
     public  Pagination findPageByCriteria( final  DetachedCriteria detachedCriteria,  final   int  pageSize,   
            
final   int  pageNum) {   
        
return  (Pagination) getHibernateTemplate().execute( new  HibernateCallback() {   
            
public  Object doInHibernate(Session session)  throws  HibernateException { 
                
// PaginationSupport ps = new PaginationSupport(items, totalCount, pageSize, startIndex);
                Pagination < SLbstraceinfo >  page;
                
try  {
                    Criteria criteria 
=  detachedCriteria.getExecutableCriteria(session);  
                    criteria.setCacheable(
true );
                    
int  totalCount  =  ((Integer) criteria.setProjection(Projections.rowCount()).uniqueResult()).intValue();   
                    criteria.setProjection(
null );
                    System.out.println(
" firstResult= " + ((pageNum - 1 ) * pageSize + 1 ));
                    List items 
=  criteria.setFirstResult( (pageNum - 1 ) * pageSize + 1  ).setMaxResults(pageSize).list();   
                    page 
=   new  ListPagination < SLbstraceinfo > (
                            items, pageNum, pageSize);
                    page.setTotalElements(totalCount);
                } 
catch  (RuntimeException e) {                    
                    e.printStackTrace();
                    
return   null ;
                }
                
return  page;   
            }   
        }, 
true );   
    }  
(注意这里的criteria.setCacheable(true);)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值