springboot 使用redistemplate替代jedis操作redis

 1.引入依赖

<!-- springboot整合redis -->  
        <dependency>  
            <groupId>org.springframework.boot</groupId>  
            <artifactId>spring-boot-starter-data-redis</artifactId>  
        </dependency>

2.在application.yml中配置redis

yml中改为yml的写法:

redis配置,以下有默认配置的也可以使用默认配置
  redis:
    host: 127.0.0.1
    port: 6379
    pool:
      max-active: 8
      max-wait: 1
      max-idle: 8
      min-idle: 0
    timeout: 0

3.编写相关的实体类

    这里注意一定要实现序列化接口用于序列化

public class Girl implements Serializable{

    private static final long serialVersionUID = -3946734305303957850L;

4.在使用类中注入redisTemplate

@Autowired

RedisTemplate  redisTemplate;

//存入数据

redisTemplate.opsForValue().set(key, girl, 600, TimeUnit.SECONDS);

redisTemplate.取出的数据不能直接反序列化为entity实体,List<JSONObject> 否则使用List<MerCategory>.stream操作时会报错JSONObject not cast MerCategory

参考:

经验分享 程序员 微信小程序 职场和发展