注解的方式使用缓存:

第一步:导入jar

1spring-context-support-4.2.4.RELEASE.jar

 

提供了平台缓存管理器相关class

引入:

(2)导入spring-framework-3.0.2.RELEASE-dependencies\net.sourceforge.ehcache\com.springsource.net.sf.ehcache\1.6.2\com.springsource.net.sf.ehcache-1.6.2.jar

 

第二步:引入ehcache.xml文件

 

 

第三步:引入头信息(和上面一样)

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xmlns:cache="http://www.springframework.org/schema/cache"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd
  16. http://www.springframework.org/schema/cache
  17. http://www.springframework.org/schema/cache/spring-cache.xsd">

 

 

第四步:编写applicationContext.xml,使用注解方式:

  1. <!-- 使用注解的方式配置缓存 -->
  2.  
  3. <!-- 第一步:定义ehcache的对象EhCacheManager:spring提供了工厂,专业来new对象 -->
  4.  
  5. <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  6.  
  7. <!-- 注入ehcache的核心配置文件
  8. 通过源代码发现,这里即使不配置,默认就到classpath中寻找ehcache.xml
  9. -->
  10.  
  11. <property name="configLocation" value="classpath:ehcache.xml"/>
  12.  
  13. </bean>
  14.  
  15. <!-- 第二步:具体平台缓存管理器:整合ehcache的实现,需要导入jar -->
  16.  
  17. <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
  18.  
  19. <!-- 注入ehcache的缓存对象 -->
  20.  
  21. <property name="cacheManager" ref="ehCacheManager"/>
  22.  
  23. </bean>
  24.  
  25. <!-- 第三步:配置缓存注解驱动 -->
  26.  
  27. <cache:annotation-driven cache-manager="cacheManager"/>

 

第五步:使用缓存的bean的类型的方法上,加两个注解:

在cn.itcast.ssh.service包中的类BookServiceImpl.java中添加缓存的注解

  1. //图书业务层
  2. @Service("bookService")
  3. @Transactional(readOnly=true)//事务(类级别的事务,一般定义成只读,方法级别的事务定义成可写)
  4.  
  5. public class BookServiceImpl implements IBookService{
  6. //注入dao层
  7. @Autowired
  8. private IBookDao bookDao;
  9. //保存图书
  10. @Transactional(readOnly=false)//事务(方法级别的事务,覆盖类级别的事务)
  11. @CacheEvict(value="bookCache",allEntries=true)
  12. public void saveBook(Book book){
  13. //调用dao层
  14. bookDao.save(book);
  15. }
  16. //查询:复杂条件查询,根据书名模糊查询
  17. @Cacheable(value="bookCache")//value:echache缓存区域的名字
  18.  
  19. public List<Book> findBookListByNameLike(String name){
  20. //1.qbn
  21.  
  22. return bookDao.findByNamedQuery("Book.findBookListByNameLike", "%"+name+"%");
  23. //2.qbc
  24. // DetachedCriteria criteria =DetachedCriteria.forClass(Book.class);//root对象类型
  25. // criteria.add(Restrictions.like("name", "%"+name+"%"));
  26. // return bookDao.findByCriteria(criteria);
  27.   }
  28. }

 

第六步:测试代码:在cn.itcast.ssh.test包中创建BookServiceTest进行测试

  1. @RunWith(SpringJUnit4ClassRunner.class)
  2. @ContextConfiguration(locations="classpath:applicationContext.xml")
  3. public class BookServiceTest {
  4. //注入service
  5. @Autowired
  6. private IBookService bookService;
  7. @Test
  8. public void testSaveBook() {
  9. Book book = new Book();
  10. book.setName("葵花宝典233");
  11. book.setPrice(998d);
  12. bookService.saveBook(book);
  13. }
  14. @Test
  15. public void testFindBookListByNameLike() {
  16. List<Book> list = bookService.findBookListByNameLike("星");
  17. System.out.println(list);
  18. List<Book> list2 = bookService.findBookListByNameLike("星");
  19. System.out.println(list2);
  20. Book book = new Book();
  21. book.setName("葵花宝典2");
  22. book.setPrice(998d);
  23. bookService.saveBook(book);
  24. List<Book> list3 = bookService.findBookListByNameLike("星");
  25. System.out.println(list3);
  26. List<Book> list4 = bookService.findBookListByNameLike("星");
  27. System.out.println(list4);
  28.   }
  29. }

 

查看测试结果,控制台中输出:

第一次查询数据库,第二次从缓存中查询!

 

 

小结:推荐是用注解,简单,xml配置麻烦。

版权声明:本文为best-lwj原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/best-lwj/p/9278499.html