SSH框架学习笔记(五)----hibernate开启二级缓存

hibernate开启二级缓存

  1. 在hibernate.cfg.xml文件中开启二级缓存:
    1. 设置启用二级缓存:
      <property name="hibernate.cache.use_second_level_cache">true</property>
    2. 设置二级缓存的实现类:
      <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>

      需要导入hibernate-ehcache.jar包

      <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>5.1.2.Final</version>
          </dependency>
  2.   配置缓存实现类所需的配置文件ehcache.xml
    <?xml version="1.0" encoding="GBK" ?>
    <ehcache>
        <diskStore path="java.io.tmpdir"/>
        <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskPersistent="false"/>
    </ehcache>
  3. 对具体的类用注解方式开启二级缓存
    @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
原文地址:https://www.cnblogs.com/XD-thinker/p/6734199.html