以下配置基于spring boot版本1.4.2.RELEASE块蚌,默認(rèn)引入的hibernate版本為5.0.11.Final聂示,ehcache版本2.10.3耿战。
Ehcache作為Hibernate的二級(jí)緩存的實(shí)現(xiàn)琉用。
1.application.properties中,添加:
#打開hibernate統(tǒng)計(jì)信息
spring.jpa.properties.hibernate.generate_statistics = true
#打開二級(jí)緩存
spring.jpa.properties.hibernate.cache.use_second_level_cache = true
#打開查詢緩存
spring.jpa.properties.hibernate.cache.use_query_cache = true
#指定緩存provider
spring.jpa.properties.hibernate.cache.region.factory_class = org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory
#配置shared-cache-mode
spring.jpa.properties.javax.persistence.sharedCache.mode = ENABLE_SELECTIVE
2.接口方法加上@QueryHints注解
@Query("from UserAddress where receiveAddress like %:receiveAddress%")
@QueryHints({ @QueryHint(name = "org.hibernate.cacheable", value ="true") }) // 使用查詢緩存
public List<UserAddress> findList(@Param("receiveAddress") String receiveAddress);