1导入redis依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
2.配置redis
spring: datasource: url: jdbc:mysql:///sx username: root password: driver-class-name: com.mysql.jdbc.Driver # cache: # type: redis redis: host: 127.0.0.1 port: 6379
3.启动类上开启缓存
@SpringBootApplication @MapperScan("com.upb.dao") @EnableCaching //开启缓存 public class UpbApplication { public static void main(String[] args) { SpringApplication.run(UpbApplication.class, args); } }
@Controller @Api( tags = "登录接口") public class LoginController { @Autowired Result result; @Autowired UserService userService; @ResponseBody //说明是什么方法(可以理解为方法注释) @RequestMapping("/login") @Cacheable(value = "r-test") public Result login( @RequestBody User user){ System.out.println(userService.getAll()); result.Successful("cg"); return result; } }
欧克记录一下,其它的详细作用可以看看其他文档。