Spring 整合 Hibernate 时启用二级缓存实例详解
写在前面:
1. 本例使用 Hibernate3 + Spring3;
2. 本例的查询使用了 HibernateTemplate;
1. 导入 ehcache-x.x.x.jar 包;
2. 在 applicationContext.xml 文件中找到 sessionFactory 相应的配置信息并在设置 hibernateProperties 中添加如下代码:
true true org.hibernate.cache.EhCacheProvider
3. 由于查询使用了 hibernateTemplate,所以还要在 hibernateTemplate 中做相应配置,找到 hibernateTemplate 的配置项,添加如下代码:
true
4. 在要缓存的实体类中加入如下注解:
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
注:
usage 可以有以下几个取值:
5. 配置 ehcache.xml 文件:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!