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);)

你可能感兴趣的:(DAO,Hibernate,bean,xml,cache)