1 导入ehcache相关依 org.hibernate hibernate-ehcache 5.2.12.Final
2 src添加ehcache.xml
3 hibernate.cfg.xml中添加二级缓存相关配置
<!-- 开启二级缓存 --> <property name="hibernate.cache.use_second_level_cache">true</property> <!-- 开启查询缓存 --> <property name="hibernate.cache.use_query_cache">true</property> <!-- EhCache驱动 --> <property name="hibernate.cache.region.factory_class">org.hibernate.cache.ehcache.EhCacheRegionFactory</property>4 指定实体类开启二级缓存
<!-- 注解式开发 --> <cache usage="read-write" region=""/>注1:查全部需要编写代码来开启二级缓存的 query.setCacheRegion(“entity.Dict”);//指定缓存策略,名字必须实体类的完整类名 query.setCacheable(true);//手动开启二级缓存
