Spring boot 中集成Redis

1、集成环境


        org.springframework.boot
        spring-boot-starter-parent
        2.2.4.RELEASE
         
 

在使用2.2.4的Spring boot情况下 集成

2、导入依赖


  org.springframework.boot
  spring-boot-starter-data-redis

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 这个框架

你可能感兴趣的:(Spring boot 中集成Redis)