经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring Boot » 查看文章
Spring Boot设置并使用缓存的步骤
来源:jb51  时间:2019/3/15 8:34:02  对本文有异议

几个缓存注解的作用:

@Cacheable:将方法的返回结果根据key指定的键保存在缓存中,以后要获取相同的数据直接从缓存中共获取

  • cacheNames/value:指定Cache组件名称
  • key:指定缓存时使用的key,默认使用方法参数值,可以使用#a0、#p0、#参数名等,支持SpEL表达式,root可省略
  • keyGenerator:指定key的生成器的组件id,如自定义的KeyGenerator
  • cacheManager:指定缓存管理器
  • cacheResolver:指定缓存解析器
  • condition:指定在哪种条件下缓存,如condition = “#id>=1”在参数>=1时缓存
  • unless:指定该条件为真时不缓存
  • sync:指定是否使用异步模式

@CachePut:不管缓存中是否有需要的数据,都会执行该注解标注的方法,并将结果更新到缓存,属性见上

@CacheEvit:执行方法后,清除key指定的缓存

  • allEntries:默认为false,值为true,删除所有缓存
  • beforeInvocation:默认为false,值为true,在方法调用之前清除缓存

@CacheConfig:定义一些通用或公共的规则,如cacheNames、keyGenerator等

可使用的SpEL表达式:

使用缓存的步骤:

(1)创建一个Spring Boot应用,勾选Cache、Web、MySQL、Mybatis模块,在主程序类上添加注解,开启基于注解的缓存

  1. @MapperScan(basePackages = "com.youngpain.cache.mapper")
  2. @SpringBootApplication
  3. @EnableCaching

(2)创建JavaBean,和数据库中的表对应,并配置数据源

  1. spring:
  2. datasource:
  3. url: jdbc:mysql://localhost:3306/mybatis_database
  4. username: root
  5. password: 1741248769
  6. driver-class-name: com.mysql.jdbc.Driver
  7. redis:
  8. host: 39.108.114.57
  9. #开启驼峰命名法
  10. mybatis:
  11. configuration:
  12. map-underscore-to-camel-case: true
  13. logging:
  14. level:
  15. com.youngpain.cache.mapper: debug

(3)创建mapper接口进行增删改查操作

  1. /**
  2. * 部门表的增删改查操作
  3. */
  4. public interface DepartmentMapper {
  5. @Insert("insert into department(id,depart_name,depart_build) values(#{id},#{depart_name},#{depart_build})")
  6. void insertDepartment(Department department);
  7. @Delete("delete from department where id=#{id}")
  8. void deleteDepartment(Integer id);
  9. @Update("update department set depart_name=#{departName},depart_build=#{departBuild} where id=#{id}")
  10. void updateDepartment(Department department);
  11. @Select("select * from department where id=#{id}")
  12. Department getDepartmentById(Integer id);
  13. }

(4)创建service

  1. @Service
  2. @CacheConfig(cacheNames = {"departs"})
  3. public class DepartmentService {
  4. @Autowired
  5. DepartmentMapper departmentMapper;
  6. @Cacheable(key = "#a0.id")
  7. public void insertDepartment(Department department) {
  8. departmentMapper.insertDepartment(department);
  9. }
  10. @CacheEvict(key = "#p0")
  11. public void deleteDepartment(Integer id) {
  12. departmentMapper.deleteDepartment(id);
  13. }
  14. @CachePut(key = "#a0.id")
  15. public Department updateDepartment(Department department) {
  16. departmentMapper.updateDepartment(department);
  17. return department;
  18. }
  19. @Cacheable(key = "#id", condition = "#p0>=1")
  20. public Department getDepartmentById(Integer id) {
  21. return departmentMapper.getDepartmentById(id);
  22. }
  23. }

(5)创建controller

  1. @Controller
  2. public class DepartmentController {
  3. @Autowired
  4. DepartmentService departmentService;
  5. @GetMapping("/index")
  6. public String index() {
  7. return "index";
  8. }
  9. @GetMapping("/deleteDepart/{id}")
  10. public String deleteDepart(@PathVariable("id") Integer id, Model model) {
  11. model.addAttribute("condition", "delete");
  12. Department delete = departmentService.getDepartmentById(id);
  13. model.addAttribute("department", delete);
  14. departmentService.deleteDepartment(id);
  15. return "success";
  16. }
  17. @PostMapping("/updateDepart")
  18. public String updateDepart(Department department, Model model) {
  19. model.addAttribute("condition", "update");
  20. Department update = departmentService.updateDepartment(department);
  21. model.addAttribute("department", update);
  22. return "success";
  23. }
  24. @GetMapping("/getDepart/{id}")
  25. public String getDepartmentById(@PathVariable("id") Integer id, Model model) {
  26. model.addAttribute("condition", "delete");
  27. Department get = departmentService.getDepartmentById(id);
  28. model.addAttribute("department", get);
  29. return "success";
  30. }
  31. }

(6)测试结果:

@Cacheable:第一次查询数据,控制台发出sql语句,之后再查询直接从缓存中获取
@CachePut:调用方法修改某个数据后,再次查询该数据是从缓存中获取的更新后的数据
@CacheEvict:调用该方法后,再次查询某个数据需要重新发出sql语句查询

ps:之前只是用markdown记笔记,今天第一次用markdown写文章,写起来好舒服啊QAQ

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对w3xue的支持。如果你想了解更多相关内容请查看下面相关链接

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号