为什么网站目录不收录,外卖平台做网站,做网站彩票网站,外贸人才网官网文章目录 简介引入依赖application.yml配置常用注解使用1. 启动类添加注解使用方法上添加注解 简介
Spring Cache是一个框架#xff0c;实现了基于注解的缓存功能底层可以使用EHCache、Caffeine、Redis实现缓存。
注解一般放在Controller的方法上#xff0c;CachePut 注解一… 文章目录 简介引入依赖application.yml配置常用注解使用1. 启动类添加注解使用方法上添加注解 简介
Spring Cache是一个框架实现了基于注解的缓存功能底层可以使用EHCache、Caffeine、Redis实现缓存。
注解一般放在Controller的方法上CachePut 注解一般有两个参数第一个时存储的名称第二个时名称后边的key使用SpEL动态的计算key。其余的注解也都是这两个参数。在用户端的查询操作需要使用Cacheable服务器端的增删改都使用CacheEvict
引入依赖
SpringCache
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId
/dependencyRedis
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId
/dependencymysql
dependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope
/dependencymybatis
dependencygroupIdorg.mybatis.spring.boot/groupIdartifactIdmybatis-spring-boot-starter/artifactIdversion2.2.0/version
/dependencyapplication.yml配置
server:port: 8888
spring:datasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/test?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8zeroDateTimeBehaviorconvertToNulluseSSLfalseallowPublicKeyRetrievaltrueusername: rootpassword: 123456redis:host: localhostport: 6379password: 123456database: 1
logging:level:com:itheima:mapper: debugservice: infocontroller: info
常用注解 使用
1. 启动类添加注解
package com.itheima;import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;Slf4j
SpringBootApplication
-----------------------------------------
添加下面的注解
EnableCaching
-----------------------------------------
public class CacheDemoApplication {public static void main(String[] args) {SpringApplication.run(CacheDemoApplication.class,args);log.info(项目启动成功...);}
}
使用方法上添加注解
package com.itheima.controller;import com.itheima.entity.User;
import com.itheima.mapper.UserMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;RestController
RequestMapping(/user)
Slf4j
public class UserController {Autowiredprivate UserMapper userMapper;/*** 新增方法返回值放入缓存*/PostMapping//参数1缓存名称参数2动态获取方法参数字段//最总保存效果是 [参数1::参数2]。例如[userCache::1]CachePut(cacheNames userCache, key #user.id)//也可以这样写参数2不再是方法参数而是方法的返回值//CachePut(cacheNames userCache,key #result.id)//也可以这样写p0代表第一个参数p1代表第二个参数//CachePut(cacheNames userCache, key #p0.id)public User save(RequestBody User user) {userMapper.insert(user);return user;}//删除 userCache::idCacheEvict(cacheNames userCache, key #id)DeleteMappingpublic void deleteById(Long id) {userMapper.deleteById(id);}//删除 所有的 userCache::*CacheEvict(cacheNames userCache, allEntries true)DeleteMapping(/delAll)public void deleteAll() {userMapper.deleteAll();}//cache生成 userCache::idCacheable(cacheNames userCache, key #id)GetMappingpublic User getById(Long id) {User user userMapper.getById(id);return user;}}