SpringBoot2.2.1连接、操作Redis(入门)
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.ls</groupId>
<artifactId>springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springboot</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
配置文件:
spring:
redis:
database: 0
host: 192.168.1.111
port: 6379
password: lsroot
timeout: 5000
服务层:
@Service
public class RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
/**
* 重载setObject方法
* @param key
* @param value
*/
public void setObject(String key,Object value){
this.setObject(key,value,null);
}
/**
* redis薪资值
* @param key
* @param value
* @param time
*/
public void setObject(String key,Object value,Long time){
if(StringUtils.isEmpty(key)||value == null){
return;
}
if (value instanceof String){
// 获取String类型的值
String stringValue=(String) value;
if(time == null){
stringRedisTemplate.opsForValue().set(key,stringValue);
}else{
stringRedisTemplate.opsForValue().set(key,stringValue,time,TimeUnit.SECONDS);
}
return;
}
if (value instanceof List){
// 获取list类型的值
List<String> list=(List<String>) value;
for (String string:list){
stringRedisTemplate.opsForList().leftPush(key,string);
}
}
}
/**
* 删除value
* @param key
*/
public void delValue(String key){
stringRedisTemplate.delete(key);
}
/**
* 获取value
* @param key
* @return value
*/
public String getValue(String key){
return stringRedisTemplate.opsForValue().get(key);
}
}
控制层:
@RestController
public class RedisTest {
@Autowired
private RedisService redisService;
@RequestMapping("/setRedis")
public String setRedis(String key, String value) {
redisService.setObject(key, value,200000L);
return "success";
}
@RequestMapping("/setList")
public String setList(String key) {
List<String> list=new ArrayList<String>();
list.add("111");
list.add("2222");
redisService.setObject(key,list);
return "success";
}
@RequestMapping("/delStringKey")
public String reStrRedis(String key){
redisService.delValue(key);
return "success";
}
@RequestMapping("/getKey")
public String getKey(String key){
return redisService.getValue(key);
}
}