怎么在春天引导中设置使用缓存

  介绍

本篇文章为大家展示了怎么在春天引导中设置使用缓存,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。

<强>几个缓存注解的作用:

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

<李>

cacheNames/价值:指定缓存组件名称

<李>

关键:指定缓存时使用的关键,默认使用方法参数值,可以使用# a0, # p0, #参数名等,支持?表达式,根可省略

<李>

keyGenerator:指定关键的生成器的组件id,如自定义的keyGenerator

<李>

cacheManager:指定缓存管理器

<李>

cacheResolver:指定缓存解析器

<李>

条件:指定在哪种条件下缓存,如条件=" # id>=1”在参数祝辞=1时缓存

<李>

除非:指定该条件为真时不缓存

<李>

同步:指定是否使用异步模式

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

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

<李>

allEntries:默认为false,值为true,删除所有缓存

<李>

beforeInvocation:默认为false,值为真,在方法调用之前清除缓存

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

<强>可使用的?表达式:

怎么在弹簧引导中设置使用缓存

<强>使用缓存的步骤:

(1)创建一个弹簧引导应用,勾选缓存,网络,MySQL, Mybatis模块,在主程序类上添加注解,开启基于注解的缓存

@MapperScan (=basePackages “com.youngpain.cache.mapper")   @SpringBootApplication   @EnableCaching

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

春:   ,数据源:   ,,url: jdbc: mysql://localhost: 3306/mybatis_database   用户名:才能根   ,,密码:1741248769   driver-class-name才能:com.mysql.jdbc.Driver   ,复述,:   主持人:才能39.108.114.57   #开启驼峰命名法   mybatis:   ,配置:   map-underscore-to-camel-case才能:真实   日志:   ,水平:   com.youngpain.cache.mapper才能:调试

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

/* *   ,*部门表的增删改查操作   ,*/public  interface  DepartmentMapper  {   @Insert才能(“insert  into 部门(id、depart_name depart_build),值(# {id}, {depart_name} #, # {depart_build})“)   void 才能insertDepartment (Department 部门);   @Delete才能(“delete 得到department  where  id=# {id}“)   void 才能deleteDepartment (Integer  id);   @Update才能(“update  department  set  depart_name=# {departName}, depart_build=# {departBuild}, where  id=# {id}“)   void 才能updateDepartment (Department 部门);   @Select才能(“select  *,得到department  where  id=# {id}“)   Department 才能getDepartmentById (Integer  id);   }

(4)创建服务

@ service   @CacheConfig (cacheNames =, {“departs"})   public  class  DepartmentService  {   @ autowired才能   DepartmentMapper 才能;departmentMapper;   @Cacheable才能(key =,“# a0.id")   public 才能;void  insertDepartment (Department 部门),{   ,,,departmentMapper.insertDepartment(部门);   ,,}   @CacheEvict才能(key =,“# p0")   public 才能;void  deleteDepartment (Integer  id), {   ,,,departmentMapper.deleteDepartment (id);   ,,}   @CachePut才能(key =,“# a0.id")   public 才能;Department  updateDepartment (Department 部门),{   ,,,departmentMapper.updateDepartment(部门);   ,,,return ;   ,,}   @Cacheable才能(key =,“# id",, condition =,“# p0>=1“)   public 才能;Department  getDepartmentById (Integer  id), {   ,,,return  departmentMapper.getDepartmentById (id);   ,,}   }

(5)创建控制器

@ controller   public  class  DepartmentController  {   @ autowired才能   DepartmentService 才能;departmentService;   @GetMapping才能(“/index")   public 才能;String 指数(),{   ,,,return “index";   ,,}   @GetMapping才能(“/deleteDepart/{id}“)   public 才能;String  deleteDepart (@PathVariable (“id"), Integer  id, Model 模型),{   ,,,model.addAttribute (“condition",,“delete");   ,,,Department  delete =, departmentService.getDepartmentById (id);   ,,,model.addAttribute (“department",,删除);   ,,,departmentService.deleteDepartment (id);   ,,,return “success";   ,,}   @PostMapping才能(“/updateDepart")   public 才能;String  updateDepart (Department 部门,Model 模型),{   null   null   null   null   null   null   null   null   null   null   null   null   null

怎么在春天引导中设置使用缓存