//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