hibernate配置二级缓存(二级缓存配置未起作用)

本文介绍如何在Hibernate中配置并使用EHCache作为二级缓存插件。具体步骤包括添加必要的jar包、配置ehcache.xml文件、在hibernate.cfg.xml中启用二级缓存以及为特定实体类指定缓存策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

二级缓存配置
1.添加缓存插件对应的jar包
ehcache-1.2.3.jar
commons-logging-1.1.1.jar
2.将ehcache.xml文件添加到项目的src目录下:
<ehcache>
<diskStore path="C:\\temp"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="15"
timeToLiveSeconds="120"
overflowToDisk="true"/>
</ehcache>
3.开启二级缓存
<!--在hibernate.cfg.xml配置二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">
true
</property>

<property name="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
5.指定使用二级缓存的实体类
示例:在Grade.hbm.xml文件中
<class name="entity.Grade" table="GRADE" schema="t26j">
<cache usage="read-write"/>
</class>
注意:
[size=medium][color=red][size=large]1.在测试的时候,用hibernateSessionFactory.getSession()得到session对象[/size][/color][/size]
[size=large][color=darkblue]如果不用session工厂则配置就不会起作用[/color][/size]
2.使用new Configuration().configure().buildSessionFactory()
创建同一个工厂,通过这一个工厂产生多个session
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值