二级缓存

使用Hibernate 二级缓存的步骤:
加入二级缓存插件的jar包及配置文件:
在hibernate.cfg.xml文件中添加
<!-- 启用二级缓存 -->
<property name="cache.use_second_level_cache">true</property>
<!-- 配置使用的二级缓存的产品 -->
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!--配置对哪些类使用hibernate的二级缓存-->
<class-cache usage="read-write" class="com.city.oa.model.CourseModel"/>
实际上也可以在.hbm.xml文件中配置对哪些类使用二级缓存,及二级缓存的策略是什么

 set方法中添加:

 <cache usage="read-writer"/>   
类级别二级缓存
<class-cache usage="read-write" class="com.city.oa.model.CourseModel"/>
集合级别二级缓存:不仅要缓存集合,还要缓存集合中的类
<class-cache usage="read-write" class="com.city.oa.model.CourseModel"/>
<class-cache usage="read-write" class="com.city.oa.model.StudentModel"/>
<collection-cache usage="read-write" collection="com.city.oa.model.StudentModel.courses"/>
	
也可以再.hbm.xml文件中进行配置:
<set name="courses" table="OA_Course">
 		<cache usage="read-write"/>
 		<key column="sid" />
 		<many-to-many class="CourseModel" column="cno" />
</set>
ehcache.xml文件可以进行配置

查询缓存:
在hibernate.cfg.xml文件中添加
<!-- 配置启用查询缓存 -->
<property name="cache.use_query_cache">true</property>
调用Query的setCacheable(true)方法或Criteria的setCacheable(true)方法;

查询缓存依赖于二级缓存
Query接口的iterate()方法:同list()一样执行查询操作,执行的SQL语句中仅包含实体类对应的数据表的ID字段
为了提高性能:
要查询的数据表中包含大量字段

启用了二级缓存,且二级缓存中可能已经包含了待查询的对象


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值