`
simgsg
  • 浏览: 91392 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

spring中配置二级缓存

 
阅读更多
<div class="postbody">
<p>1.首先,在spring的hibernate配置里(我的是applicationContext-hibernate.xml)加上如下属性:</p>
<p>&lt;bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"&gt;  <br>&lt;property name="dataSource"&gt;<br>&lt;ref bean="dataSource"/&gt;<br>&lt;/property&gt;<br>&lt;property name="mappingResources"&gt;<br>&lt;list&gt;<br>&lt;value&gt;org/appfteaching/model/TArticleclass.hbm.xml&lt;/value&gt;<br>&lt;/list&gt;<br>&lt;/property&gt;<br>&lt;property name="hibernateProperties"&gt;<br>&lt;props&gt;<br>&lt;prop key="hibernate.dialect"&gt;${hibernate.dialect}&lt;/prop&gt;<br> &lt;prop key="hibernate.show_sql"&gt;${hibernate.show_sql}&lt;/prop&gt;<br> &lt;prop key="hibernate.jdbc.fetch_size"&gt;${hibernate.jdbc.fetch_size}&lt;/prop&gt;<br> &lt;prop key="hibernate.jdbc.batch_size"&gt;${hibernate.jdbc.batch_size}&lt;/prop&gt;<br><span style="color: #ff0000;">&lt;prop key="hibernate.cache.use_query_cache"&gt;true&lt;/prop&gt;<br> &lt;prop key="hibernate.cache.provider_class"&gt;org.hibernate.cache.EhCacheProvider&lt;/prop&gt;<br></span>&lt;/props&gt;<br>&lt;/property&gt;<br>&lt;/bean&gt;</p>
<p>2.其次,在src目录下的ehcache.xml中配置如下信息(如果是默认ehcache.xml则会有&lt;cache name="sampleCache1"&gt;和&lt;cache name="sampleCache2&gt;",去掉)</p>
<p>&lt;cache name="org.hibernate.cache.StandardQueryCache"<br> maxElementsInMemory="10000"<br> eternal="false"<br> timeToIdleSeconds="300"<br> timeToLiveSeconds="4200"<br> overflowToDisk="true"<br> /&gt;</p>
<p> &lt;!-- Sample cache named sampleCache2<br> This cache contains 1000 elements. Elements will always be held in memory.<br> They are not expired. --&gt;</p>
<p> &lt;cache name="org.hibernate.cache.UpdateTimestampsCache"<br> maxElementsInMemory="5000"<br> eternal="true"<br> timeToIdleSeconds="0"<br> timeToLiveSeconds="0"<br> overflowToDisk="false"<br> /&gt; </p>
<p>3.将你要缓存的model加进ehcache.xml里</p>
<p>&lt;cache name="org.appfteaching.model.TArticleclass" <br> maxElementsInMemory="1000"<br> eternal="false"<br> timeToIdleSeconds="100"<br> timeToLiveSeconds="4200"<br> overflowToDisk="true"<br> /&gt; </p>
<p>4.最后一步,在TArticleclass.hbm.xml里加上</p>
<p>&lt;cache usage="read-write"/&gt;</p>
<p>启动Tomcat,如发现如下错误</p>
<p>Could not find configuration [org.hibernate.cache.UpdateTimestampsCache]; using defaults.<br>Could not find configuration [org.hibernate.cache.StandardQueryCache]; using defaults.</p>
<p>则是第二步没有做,加上即可.配置完毕</p>
</div>
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics