hibernate开启二级缓存

来源:互联网 发布:java qrcode.jar 官网 编辑:程序博客网 时间:2024/05/11 23:36

一、在hibernate.cfg.xml中加入:

<!-- 开启二级缓存 --><property name="hibernate.cache.use_query_cache">true</property><property name="cache.use_second_level_cache">true</property><property name="cache.provider_class">org.hibernate.cache.EhCacheProvider</property>

二、在src目录下加入ehcache.xml文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>  <ehcache>  <diskStore path="java.io.tmpdir"/>   <defaultCachemaxElementsInMemory="150000"eternal="true"         timeToIdleSeconds="120"       timeToLiveSeconds="120"       overflowToDisk="true"         diskPersistent="false"       diskExpiryThreadIntervalSeconds="120"       />  </ehcache>  

三、在需要从缓存读数据的DAO查询代码中加入setCacheable(true)