Springboot 集成Redis

一、pom添加依赖

        
        
            org.springframework.boot
            spring-boot-starter-data-redis
        
        
        
            org.apache.commons
            commons-pool2
            2.11.1
        

 注意commons-pool2包与spring的版本一致性,若出错尝试升级或降级commons-pool2版本。

二、配置redis信息

spring.redis.host = 127.0.0.1
spring.redis.host.port= 6379
#spring.redis.host.name=
#spring.redis.host.password=

三、测试

使用RedisTemplate进行操作

    @Resource
    RedisTemplate redisTemplate;

    @GetMapping("/redis/{name}")
    public Object redis(@PathVariable String name){
        redisTemplate.opsForValue().set("name",name);
        //设置过期时间1天过期
        redisTemplate.expire("name",1, TimeUnit.DAYS);
        return redisTemplate.opsForValue().get("name");
    }

你可能感兴趣的:(Springboot多模块集成,spring,boot,redis,后端)