springboot整合redis

首先开启本地redis服务:
springboot整合redis
创建简单的springboot项目,并添加依赖:

<!--springboot对redis的依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

        <!-- https://mvnrepository.com/artifact/redis.clients/jedis -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.0</version>
        </dependency>
        <dependency>
            <groupId>net.sf.json-lib</groupId>
            <artifactId>json-lib</artifactId>
            <version>2.2.3</version>
            <classifier>jdk15</classifier><!-- 指定jdk版本 -->
        </dependency>
application.properties文件的配置:
#redis数据库索引,默认为0;
redis.database=1
#redis默认服务器地址
redis.host=127.0.0.1
#redis默认端口号
redis.port=6379
#redis默认密码为空
redis.password=

Controller:

@RestController
public class RedisController {
    @Autowired
    private StringRedisTemplate  stringRedisTemplate;
    @RequestMapping("/test")
    public String test(){
        stringRedisTemplate.opsForValue().set("jjc","HelloWorld");
        return stringRedisTemplate.opsForValue().get("jjc");
    }
 @RequestMapping("/getUser")
    public String get(){
        User user =new User();
        user.setId(90901);
        user.setAge(18);
        user.setName("jack");
        user.setSex("男");
       //使用JSONObject将User对象转换为json字符串
        JSONObject object = JSONObject.fromObject(user);
        stringRedisTemplate.opsForValue().set("user",object.toString());
        String s= stringRedisTemplate.opsForValue().get("user");
        return s  ;
    }

}

启动项目,访问接口:
springboot整合redis

springboot整合redis

这时候在本地redis使用 get key命令可以得到的程序中插入的value。
springboot整合redis
springboot整合redis

连接redis成功。