springboot整合aliyun的redis 使用StringRedisTemplate读写string类型数据

1.导入依赖

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

2.写配置文件,在yml配置文件中写入配置信息

spring: 
  redis:
    port: 6379
    host: ********************* #aliyun的redis地址
    password: ******* #此处填写密码

2.在要进行读、写操作的类注入 RedisTemplate

我在此处用StringRedisTemplate做示例,两种用法基本类似。

将List 类型数据写入redis

写入:

@Service
public class DictionaryItemServiceImpl implements DictionaryItemService {
     @Autowired
    private DictionaryItemMapper dictionaryItemMapper;
     @Autowired
    private StringRedisTemplate redisTemplate;

QueryWrapper dictionaryItemQueryWrapper = new QueryWrapper<>();
            dictionaryItemQueryWrapper.eq("dictionary_type",dictionaryType)
                    .eq("is_delete",0);
List dictionaryItems = dictionaryItemMapper.selectList(dictionaryItemQueryWrapper);
redisTemplate.opsForValue().set("key", JSON.toJSONString(dictionaryItems),30, TimeUnit.MINUTES);


}

读取:


    @Autowired
    private StringRedisTemplate redisTemplate;

   String s = redisTemplate.opsForValue().get("key");
   List dictionaryItemList = JSON.parseArray(s, DictionaryItem.class);

 

你可能感兴趣的:(spring,boot,redis,java)