- <!-- hibernate缓存包 -->
- <dependency>
- <groupId>org.hibernate</groupId>
- <artifactId>hibernate-ehcache</artifactId>
- <version>${hibernate.version}</version>
- </dependency>
- "1.0" encoding="UTF-8"?>
- <ehcache>
-
- <diskStore path="G:/ehcache/"/>
-
-
-
-
- <defaultCache maxElementsInMemory="10000"
- eternal="false"
- timeToIdleSeconds="300"
- timeToLiveSeconds="600"
- overflowToDisk="true"/>
-
-
-
- <cache name="testCache"
- eternal="false"
- maxElementsInMemory="100"
- timeToIdleSeconds="1200"
- timeToLiveSeconds="1200"
- overflowToDisk="false">
- cache>
- ehcache>
- <!-- 类级二级缓存:指定哪些类要使用二级缓存 -->
- <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
- "1.0" encoding="UTF-8"?>
- 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">xxxxxproperty>
- <property name="hibernate.connection.driver_class">xxxxproperty>
- <property name="hibernate.connection.url">xxxxproperty>
- <property name="hibernate.connection.username">xxxxproperty>
- <property name="hibernate.connection.password">123456property>
- <property name="hibernate.show_sql">trueproperty>
- <property name="hibernate.format_sql">trueproperty>
- <property name="hibernate.hbm2ddl.auto">updateproperty>
-
- <property name="hibernate.cache.use_second_level_cache">trueproperty>
- <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.internal.EhcacheRegionFactoryproperty>
-
- <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()));
- }