Hibernate二级缓存

  1. 基本配置:
    pom.xml中引入与当前hibernate对应版本的hibernate-ehcache包
    <!-- hibernate缓存包 -->
    		<dependency>
    			<groupId>org.hibernate</groupId>
    			<artifactId>hibernate-ehcache</artifactId>
    			<version>${hibernate.version}</version>
    		</dependency>
  2. 在hibernate.cfg.xml中开启二级缓存

    <!-- 缓存设置 -->
      <property name="hibernate.cache.use_second_level_cache">true</property>
    <!-- 缓存注入类配置 -->
    <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>
    注:查看该 类的方法一般是到引入的包中找到EhcacheRegionFactory.class这个类

  3. 根目录中创建ehcache.xml



     
    <?xml version="1.0" encoding="UTF-8"?>  
    <ehcache>  
      <!--如果缓存中的对象存储超过指定的缓存数量的对象存储的磁盘地址-->  
      <diskStore path="G:/ehcache/"/>  
      
      <!-- 默认cache:如果没有对应的特定区域的缓存,就使用默认缓存 --> 
      <!-- maxElementsInMemory为内存中缓存的最大对象数量,
      如果overflowToDisk为 true,缓存的内容有外溢,那么就会将外溢部份的数据写到指定的磁盘中 ,
      maxElementsOnDisk为磁盘上缓存的对象数量,默认为0表示不限制
      eternal为设定对象保存的永久属性,为true时表示永久,timeToLiveSeconds、timeToIdleSeconds失效
      timeToIdleSeconds对象多久没有被访问就会失效,单位秒
      timeToLiveSeconds为对象从创建到失效一共多长时间,单位秒
       diskSpoolBufferSizeMB 为diskStore使用的磁盘空间大小,默认30 mb-->
      
        <defaultCache maxElementsInMemory="10000"   
                    eternal="false"  
                    timeToIdleSeconds="300"   
                    timeToLiveSeconds="600" 
                    overflowToDisk="true"/>  
      <!-- 在类的映射文件指定区域cache:通过region指定某个类或集合二级缓存使用这个区块-->  
       <!-- 如:<cache usage="read-write" region="testCache"/> -->
       <!--如: <collection-cache region="testCache" usage="read-write" collection="com.restfullDemo.model.Department.users"/> -->
       <cache name="testCache"  
                    eternal="false"  
                    maxElementsInMemory="100" 
                    timeToIdleSeconds="1200"  
                    timeToLiveSeconds="1200"  
                    overflowToDisk="false">  
      </cache>   
    </ehcache> 

  4. 配置指定要使用二级缓存的方式有两种
    a) 在hibernate.cfg.xml中指定要使用二级缓存的类
    <!-- 类级二级缓存:指定哪些类要使用二级缓存 -->
      <class-cache usage="read-write" class="com.restfullDemo.model.User"/>
      <class-cache usage="read-write" class="com.restfullDemo.model.Department"/>
      <!-- 集合级二级缓存,指定哪个类中的集合属性使用二级缓级,前提是该属性的类必须已设置二级缓存中 -->
      <collection-cache usage="read-write" collection="com.restfullDemo.model.Department.users"/>

    b) 也可以在类的对应映射文件中时行配置
     

    类的二级缓存配置
    <class name="com.restfullDemo.model.User" table="t_user">
        <cache usage="read-write"/>
    ......
    </class>
    集合级的二级缓存配置
    <hibernate-mapping>
        <class name="com.restfullDemo.model.Department" table="t_department">
            <id name="id" type="int">
                <column name="id" />
                <generator class="assigned" />
            </id>
            <property name="name" type="java.lang.String">
                <column name="name" />
            </property>
            <set name="users" table="t_user" inverse="false" lazy="false" >
            <cache usage="read-write"/>
                <key>
                    <column name="dpt_id" />
                </key>
                <one-to-many class="com.restfullDemo.model.User" />
            </set>
        </class>
    </hibernate-mapping>


    实例
     

    public class User {
    	private int id;
    	private String name;
    	private int age;
    	private Department dpt;
        setter、getter.....
    }
    public class Department {
    	private int id;
    	private String name;
    	private Set<User> users = new HashSet<User>();
    setter、getter......
    }

    hibernet.cfg.xml
     

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
                                             "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
    <hibernate-configuration>
     <session-factory name="">
      <property name="hibernate.dialect">xxxxx</property>
      <property name="hibernate.connection.driver_class">xxxx</property>
      <property name="hibernate.connection.url">xxxx</property>
      <property name="hibernate.connection.username">xxxx</property>
      <property name="hibernate.connection.password">123456</property>
      <property name="hibernate.show_sql">true</property>
      <property name="hibernate.format_sql">true</property>
      <property name="hibernate.hbm2ddl.auto">update</property>
      <!-- 缓存设置 -->
      <property name="hibernate.cache.use_second_level_cache">true</property>
      <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>
      <!-- 映射文件配置 -->
      <mapping resource="com/restfullDemo/mappingResources/User.hbm.xml"/>
      <mapping resource="com/restfullDemo/mappingResources/Department.hbm.xml"/>
      <!-- 类级二级缓存:指定哪些类要使用二级缓存 -->
      <class-cache usage="read-write" class="com.restfullDemo.model.User"/>
      <class-cache usage="read-write" class="com.restfullDemo.model.Department"/>
      <!-- 集合级二级缓存,指定哪个类中的集合属性使用二级缓级,前提是该属性的类必须已设置二级缓存中 -->
      <collection-cache usage="read-write" collection="com.restfullDemo.model.Department.users"/>
     </session-factory>
    </hibernate-configuration>

查询缓存

查询缓存依赖于二级缓存,二级缓存没有开,查询缓存会失效,同理当设置完类级、集合集的二级缓存后,二级缓存没有开,在进行HQL的Query查询时,缓存也会失效,配置如下:
在hibernet.cfg.xml中添加

<!-- 缓存设置 -->
  <property name="hibernate.cache.use_second_level_cache">true</property>
  <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactory</property>
 <!-- 开启查询二级缓存 -->
  <property name="hibernate.cache.use_query_cache">true</property>

在查询语句中开启查询缓存
 

public void getallUser() {
		String hql="from User";
		Query query=session.createQuery(hql);
		//开启查询缓存
        query.setCacheable(true);
		
		List<User> users1=query.list();
		System.out.println(Arrays.toString(users1.toArray()));
		List<User> users2=query.list();
		System.out.println(Arrays.toString(users2.toArray()));
	}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值