1、集成环境
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
在使用2.2.4的Spring boot情况下 集成
2、导入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
3、配置application.yml
spring:
redis:
host: 192.168.1.12
port: 6379
jedis:
pool:
max-active: 8
max-wait: -1
max-idle: 500
min-idle: 0
4、编写RedisUtil
/**
* Redis 工具类
*/
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate redisTemplate;
public void setRedisTemplate(StringRedisTemplate redisTemplate) {
this.redisTemplate = redisTemplate;
}
public StringRedisTemplate getRedisTemplate() {
return this.redisTemplate;
}
/** -------------------key相关操作--------------------- */
}
我主要是参考了这个开源库
https://gitee.com/whvse/RedisUtil/blob/master/RedisUtil.java
由于使用了StringRedisTemplate ,所以存储的是String
如果我们想存对象的话,需要自己做处理
而我则是用Gson 这个框架