SpringBoot整合Redis

1.所需包

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


2.配置
#redis  
spring.redis.hostName=127.0.0.1
spring.redis.port=6379
spring.redis.pool.maxActive=8
spring.redis.pool.maxWait=-1
spring.redis.pool.maxIdle=8
spring.redis.pool.minIdle=0
spring.redis.timeout=0

3.demo
package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


@RestController
public class HiController {
    
    @Autowired
    private RedisTemplate redisTemplate;


    @RequestMapping(value = "/redis",method = RequestMethod.GET)
    public Set<String> redis(){
        Set<String> list=new HashSet<>();
 redisTemplate.opsForSet().add("kk","k1","k2");
        redisTemplate.opsForSet().add("kk","k3","k4");

        return  redisTemplate.opsForSet().members("kk");
    }
}

 4.结果

访问 http://localhost:8765/redis, 即可看到返回的json字符串

原文地址:https://www.cnblogs.com/tiancai/p/9019438.html