Spring Boot设置并使用缓存的步骤
程序员文章站
2023-12-20 21:59:52
几个缓存注解的作用:
@cacheable:将方法的返回结果根据key指定的键保存在缓存中,以后要获取相同的数据直接从缓存*获取
cachenames/val...
几个缓存注解的作用:
@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模块,在主程序类上添加注解,开启基于注解的缓存
@mapperscan(basepackages = "com.youngpain.cache.mapper") @springbootapplication @enablecaching
(2)创建javabean,和数据库中的表对应,并配置数据源
spring: datasource: url: jdbc:mysql://localhost:3306/mybatis_database username: root password: 1741248769 driver-class-name: com.mysql.jdbc.driver redis: host: 39.108.114.57 #开启驼峰命名法 mybatis: configuration: map-underscore-to-camel-case: true logging: level: com.youngpain.cache.mapper: debug
(3)创建mapper接口进行增删改查操作
/** * 部门表的增删改查操作 */ public interface departmentmapper { @insert("insert into department(id,depart_name,depart_build) values(#{id},#{depart_name},#{depart_build})") void insertdepartment(department department); @delete("delete from department where id=#{id}") void deletedepartment(integer id); @update("update department set depart_name=#{departname},depart_build=#{departbuild} where id=#{id}") void updatedepartment(department department); @select("select * from department where id=#{id}") department getdepartmentbyid(integer id); }
(4)创建service
@service @cacheconfig(cachenames = {"departs"}) public class departmentservice { @autowired departmentmapper departmentmapper; @cacheable(key = "#a0.id") public void insertdepartment(department department) { departmentmapper.insertdepartment(department); } @cacheevict(key = "#p0") public void deletedepartment(integer id) { departmentmapper.deletedepartment(id); } @cacheput(key = "#a0.id") public department updatedepartment(department department) { departmentmapper.updatedepartment(department); return department; } @cacheable(key = "#id", condition = "#p0>=1") public department getdepartmentbyid(integer id) { return departmentmapper.getdepartmentbyid(id); } }
(5)创建controller
@controller public class departmentcontroller { @autowired departmentservice departmentservice; @getmapping("/index") public string index() { return "index"; } @getmapping("/deletedepart/{id}") public string deletedepart(@pathvariable("id") integer id, model model) { model.addattribute("condition", "delete"); department delete = departmentservice.getdepartmentbyid(id); model.addattribute("department", delete); departmentservice.deletedepartment(id); return "success"; } @postmapping("/updatedepart") public string updatedepart(department department, model model) { model.addattribute("condition", "update"); department update = departmentservice.updatedepartment(department); model.addattribute("department", update); return "success"; } @getmapping("/getdepart/{id}") public string getdepartmentbyid(@pathvariable("id") integer id, model model) { model.addattribute("condition", "delete"); department get = departmentservice.getdepartmentbyid(id); model.addattribute("department", get); return "success"; } }
(6)测试结果:
@cacheable:第一次查询数据,控制台发出sql语句,之后再查询直接从缓存中获取
@cacheput:调用方法修改某个数据后,再次查询该数据是从缓存中获取的更新后的数据
@cacheevict:调用该方法后,再次查询某个数据需要重新发出sql语句查询
ps:之前只是用markdown记笔记,今天第一次用markdown写文章,写起来好舒服啊qaq
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对的支持。如果你想了解更多相关内容请查看下面相关链接