hibernate开启二级缓存
- 在hibernate.cfg.xml文件中开启二级缓存:
- 设置启用二级缓存:
<property name="hibernate.cache.use_second_level_cache">true</property>
- 设置二级缓存的实现类:
<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>
- 设置启用二级缓存:
- 配置缓存实现类所需的配置文件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>
- 对具体的类用注解方式开启二级缓存
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)