Hibernate 二级缓存 使用

本文介绍Hibernate框架中缓存的应用,包括一级缓存和二级缓存的配置与使用方法。通过具体示例展示了如何配置缓存策略及EHCache插件,并演示了如何启用查询缓存以提高应用程序性能。

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

//javabean public class User { private int id; private String username; private String password; //setter getter }

// 使用 缓存

User 使用缓存

<hibernate-mapping package="tao.hib.bean"> <class name="User" table="sys_user"> <!-- 缓存策略 --> <cache usage="read-write"/> <id name="id" type="integer"> <generator class="native"></generator> </id> <property name="username" type="string" length="20"/> <property name="password" type="string" length="20"/> </class> </hibernate-mapping>

// hib.cfg.xml 缓存

启动查询缓存

<!-- 设置二级缓存插件EHCache的Provider类 --> <property name="hibernate.cache.provider_class"> org.hibernate.cache.EhCacheProvider </property> <!-- 启动"查询缓存" --> <property name="hibernate.cache.use_query_cache">true</property> <mapping resource="tao/hib/bean/User.hbm.xml" />

// 缓存配置文件

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <!-- maxElementsInMemory为缓存对象的最大数目, eternal设置是否永远不过期 timeToIdleSeconds对象处于空闲状态的最多秒数 timeToLiveSeconds对象处于缓存状态的最多秒数 --> <diskStore path="java.io.tmpdir" /> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" /> <cache name="org.hibernate.cache.StandardQueryCache" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="300" timeToLiveSeconds="4200" overflowToDisk="true" /> <cache name="org.hibernate.cache.UpdateTimestampsCache" maxElementsInMemory="5000" eternal="true" timeToIdleSeconds="0" timeToLiveSeconds="0" overflowToDisk="false" /> <cache name="tao.hib.bean.User" maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="10000" timeToLiveSeconds="10000" overflowToDisk="true" /> </ehcache>

@SuppressWarnings("unchecked") public static void main(String[] args) { Session s = HibernateSessionFactory.getSession(); // s.getTransaction().begin(); // for(int i=0;i<20;i++){ // User user = new User(); // user.setUsername(Math.random()+""); // user.setPassword(Math.random()+""); // s.save(user); // } Criteria c = s.createCriteria(User.class); c.add(Restrictions.eq("username", "NyiiMzA7O5mT")); c.setCacheable(true); List l = c.list(); User User = (User)l.get(0); System.out.println("-1-"+User.getUsername()); HibernateSessionFactory.closeSession(); // try{ // Thread.sleep(5000); // } catch (InterruptedException e){ // e.printStackTrace(); // } s = HibernateSessionFactory.getSession(); c=s.createCriteria(User.class); c.add(Restrictions.eq("username", "NyiiMzA7O5mT")); c.setCacheable(true); l=c.list(); User=(User)l.get(0); System.out.println("-2-"+User.getUsername()); HibernateSessionFactory.closeSession(); //s.getTransaction().commit(); }

// 再查询时候, 虽然第一次 Session 关闭了,但是开启了 二级缓存: SessionFactory 级别的。

c.setCacheable(true);

再 第二次执行

l=c.list();

的时候 将不会执行hql

其执行结果如下

----------

Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from sys_user this_ where this_.username=?
-1-NyiiMzA7O5mT
-2-NyiiMzA7O5mT

=========================================

如果不开启 二次缓存

c.setCacheable(false); // 默认

执行结果则如下:

--------------------------------------------

Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from sys_user this_ where this_.username=?
-1-NyiiMzA7O5mT

Hibernate: select this_.id as id0_0_, this_.username as username0_0_, this_.password as password0_0_ from sys_user this_ where this_.username=?
-2-NyiiMzA7O5mT

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值