hibernate二级缓存实例

hibernate.cfg.xml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

<session-factory>

<property name="connection.driver_class">com.mysql.jdbc.Driver</property>
<property name="connection.url">jdbc:mysql://localhost:3306/test</property>
<property name="connection.username">root</property>
<property name="connection.password">root</property>
<property name="dialect">org.hibernate.dialect.MySQL5InnoDBDialect</property>
<property name="show_sql">true</property>
<!-- 二级缓存 -->
<property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>
<property name="hibernate.cache.use_second_level_cache">true</property>

<mapping resource="com/llf/hbm/User.hbm.xml"/> 
<mapping class = "com.llf.bean.Hib"></mapping>
</session-factory>

</hibernate-configuration>

ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="true"
monitoring="autodetect" dynamicConfig="true">

<diskStore path="java.io.tmpdir/ehcache" />

<!-- defaultCache将用于所有持久化类。 我们还可以通过使用 cache 元素来明确定义持久化类。
eternal 如果我们指定eternal =“true”,则不需要定义timeToIdleSeconds和timeToLiveSeconds属性,
因为它将由hibernate内部处理。 指定eternal =“false”给程序员控制,
但是我们需要定义timeToIdleSeconds和timeToLiveSeconds属性timeToIdleSeconds它定义了二级缓存中对象可以空闲多少秒。
timeToLiveSeconds它定义了在第二级缓存中对象可以存储多少秒,无论它是否空闲。 -->

<defaultCache maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="120" timeToLiveSeconds="120" diskSpoolBufferSizeMB="30"
maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU" statistics="true">
<persistence strategy="localTempSwap" />
</defaultCache>

<cache name="org.hibernate.cache.internal.StandardQueryCache"
maxEntriesLocalHeap="5" eternal="false" timeToLiveSeconds="120">
<persistence strategy="localTempSwap" />
</cache>

<cache name="org.hibernate.cache.spi.UpdateTimestampsCache"
maxEntriesLocalHeap="5000" eternal="true">
<persistence strategy="localTempSwap" />
</cache>
<cache name="com.llf.bean.Hib" maxElementsInMemory="100"
eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="200" />
</ehcache>

User.hbm.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.llf.bean">
<class name="User" table="user">
<cache usage="read-only"/>
<id name="user_id" type="int" column="user_id">
<generator class="identity"></generator>
</id>
<property name="name" type="java.lang.String" column="name"></property>
<property name="password" type="java.lang.String" column="password"></property>
</class>
</hibernate-mapping>

猜你喜欢

转载自www.cnblogs.com/-llf/p/10611620.html