二级缓存的配置,相当于给model类做配置。随后对该类做出的操作均是在二级缓存中。废话不多说,直接上代码。
一、创建model类及其hbm文件。这里不多说。就进行常规的配置即可。
二、在hibernate.cfg.xml中开启二级缓存及Query查询缓存。
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- 对hibernate全局设置 -->
<hibernate-configuration>
<!-- 对session工厂的设置 -->
<session-factory>
<!-- 数据库连接的基本设置 四本一言基本设置-->
<property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/hibernate?characterEncoding=utf8</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">123456</property>
<property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<!-- 默认是不使用二级缓存 。设置开启二级缓存 -->
<property name="hibernate.cache.use_second_level_cache">true</property>
<!--
开启查询缓存
如果没有开启二级缓存,只开启了查询缓存,此时,
缓存中只存储查询出来的数据的id,如果后续再查询相同
的数据,根据缓存中的id分条查询数据,即所谓的N+1。
-->
<property name="hibernate.cache.use_query_cache">true</property>
<property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>
<!-- 控制台是否显示sql语句 -->
<property name="show_sql">true</property>
<!-- 格式sql -->
<property name="format_sql">true</property>
<!-- 自动建表 -->
<property name="hbm2ddl.auto">update</property>
<!-- 引入映射文件 -->
<mapping resource="com/xalo/model/ClassRoom.hbm.xml"/>
<!-- 指定使用缓存对象的策略 并指定并发策略(usage) -->
<class-cache usage="read-only" class="com.xalo.model.ClassRoom"/>
</session-factory>
</hibernate-configuration>
需要注意的是class-cache标签必须在mapping标签之下。
三、配置ehcache.xml文件
<!--
~ Hibernate, Relational Persistence for Idiomatic Java
~
~ License: GNU Lesser General Public License (LGPL), version 2.1 or later.
~ See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
-->
<ehcache>
<!-- Sets the path to the directory where cache .data files are created.
If the path is a Java System Property it is replaced by
its value in the running VM.
The following properties are translated:
user.home - User's home directory
user.dir - User's current working directory
java.io.tmpdir - Default temp file path -->
<!-- 当缓存内容超出指定的大小,超出部分的存储位置-->
<diskStore path="java.io.tmpdir"/>
<!--Default Cache configuration. These will applied to caches programmatically created through
the CacheManager.
The following attributes are required for defaultCache:
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<!--
所有使用到二级缓存的对象的默认设置
maxElementsInMemory:允许在内存中缓存的对象个数
eternal:缓存是否永久有效
timeToIdleSeconds:设置 缓存对象 最大空闲的时间 超过该时间销毁
timeToLiveSeconds:设置 缓存对象 最大存活时间 超过该时间销毁
overflowToDisk:超出允许缓存个数后 是否存储到指定位置硬盘
-->
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="true"
/>
<!-- 单独配置(个性化)某个二级缓存对象 -->
<cache
name="com.xalo.model.ClassRoom"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/>
<!--Predefined caches. Add your cache configuration settings here.
If you do not have a configuration for your cache a WARNING will be issued when the
CacheManager starts
The following attributes are required for defaultCache:
name - Sets the name of the cache. This is used to identify the cache. It must be unique.
maxInMemory - Sets the maximum number of objects that will be created in memory
eternal - Sets whether elements are eternal. If eternal, timeouts are ignored and the element
is never expired.
timeToIdleSeconds - Sets the time to idle for an element before it expires. Is only used
if the element is not eternal. Idle time is now - last accessed time
timeToLiveSeconds - Sets the time to live for an element before it expires. Is only used
if the element is not eternal. TTL is now - creation time
overflowToDisk - Sets whether elements can overflow to disk when the in-memory cache
has reached the maxInMemory limit.
-->
<!-- Sample cache named sampleCache1
This cache contains a maximum in memory of 10000 elements, and will expire
an element if it is idle for more than 5 minutes and lives for more than
10 minutes.
If there are more than 10000 elements it will overflow to the
disk cache, which in this configuration will go to wherever java.io.tmp is
defined on your system. On a standard Linux system this will be /tmp"
-->
<!-- <cache name="sampleCache1"
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="300"
timeToLiveSeconds="600"
overflowToDisk="true"
/> -->
<!-- Sample cache named sampleCache2
This cache contains 1000 elements. Elements will always be held in memory.
They are not expired. -->
<!-- <cache name="sampleCache2"
maxElementsInMemory="1000"
eternal="true"
timeToIdleSeconds="0"
timeToLiveSeconds="0"
overflowToDisk="false"
/> -->
<!-- Place configuration for your caches following -->
</ehcache>
随后对model类做出操作均是在二级缓存中进行的了