SpringBoot整合Redis
pom引入redis依赖
<!-- 引入 redis 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
yml添加配置
spring:
redis:
database: 0 # 第几个库
host: ip
port: 6379
password: 密码
写个Controller测试
package com.jinsh.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController("redis")
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/get")
public String get(String key){
String result = (String) redisTemplate.opsForValue().get(key);
return result;
}
@GetMapping("/set")
public String set(String key, String value){
redisTemplate.opsForValue().set(key, value);
return "OK";
}
@GetMapping("/del")
public String del(String key){
redisTemplate.delete(key);
return "OK";
}
}
SpringBoot整合Redis