前言
记录下SpringBoot集成Redis的方式。
环境
SpringBoot2.5.3 + Redis-x64-3.2.1
具体实现
- pom.xml
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
- application.yml
redis:
localhost: localhost
port: 6379
password:
- 测试
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private StringRedisTemplate redisTemplate;
@GetMapping(value = "/setVal")
public void setVal(@RequestParam String name) {
redisTemplate.opsForValue().set("name", name);
}
@GetMapping(value = "/getVal")
public void getVal() {
String name = redisTemplate.opsForValue().get("name");
System.out.println("name:" + name);
}
}
- 访问接口输出: