ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

springboot中@Cache和redis的使用

2022-01-27 14:58:50  阅读:117  来源: 互联网

标签:springboot Cache redis public result mysql userService class


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);
    }

}

4.编写控制器层

@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;
    }

}

5.由于刚学redis,没用过注解,百度了很多,最后自己才知道@Cacheable中的value=“你redis中的key”‘,就相当于给你主动存到了redis中。

 欧克记录一下,其它的详细作用可以看看其他文档。

标签:springboot,Cache,redis,public,result,mysql,userService,class
来源: https://blog.csdn.net/qq_55823436/article/details/122717634

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有