- 导入相关依赖
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sxd</groupId>
<artifactId>springboot-redis</artifactId>
<version>1.0.0</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.10.RELEASE</version>
</parent>
<dependencies>
<!-- web组件 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
</dependencies>
</project>
- 配置文件
spring:
redis:
host: 192.168.6.128
port: 6379
password: "!QAZ2wsx"
database: 0
jedis:
pool:
max-wait: -1 # 连接池最大阻塞等待时间,单位:毫秒。默认为-1,表示不限制
max-active: 8 # 连接池最大连接数,默认为8。使用负数表示没有限制。
max-idle: 8 # 默认连接数量最大空闲的连接数,默认为:8.使用负数表示没有限制
min-idle: 0 # 默认连接数量最小空闲的连接数,默认为:0.允许设置0和正数
timeout: 10000 # redis 连接超时时间,单位:毫秒
- 启动类
package com.sxd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class AppStart {
public static void main(String[] args) {
SpringApplication.run(AppStart.class, args);
}
}
- sercice层
package com.sxd.service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import java.util.Set;
import java.util.concurrent.TimeUnit;
@Service
public class RedisService {
@Autowired
private StringRedisTemplate stringRedisTemplate;
public void setString(String key, Object object, Long time) {
if (object instanceof String) {
setString(key, object);
}
if (object instanceof Set) {
setSet(object);
}
stringRedisTemplate.expire(key, time, TimeUnit.SECONDS);
}
public String getString(String key) {
return stringRedisTemplate.opsForValue().get(key);
}
private void setSet(Object object) {
Set<String> values = (Set<String>)object;
values.forEach(value -> stringRedisTemplate.opsForSet().add(value));
}
private void setString(String key, Object object) {
String value = (String)object;
stringRedisTemplate.opsForValue().set(key, value);
}
}
- controller层
package com.sxd.controller;
import com.sxd.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisService redisService;
@GetMapping("/set")
public String set(@RequestParam String key, @RequestParam Object value) {
redisService.setString(key, value, 60L);
return "Success!";
}
@GetMapping("get")
public String get(@RequestParam String key) {
return redisService.getString(key);
}
}
- 启动项目查看效果
根据具体路由访问:http://localhost:8080/redis/set?key=name&value=123
http://localhost:8080/redis/get?key=name
- 总结
springboot整合redis步骤如下:
导入相关依赖
配置文件配置
启动类
service层开发 (redis的查询/新增)
controller层
项目结构: