redis(七)---- SpringBoot和redis整合

SpringBoot和Redis整合非常简单

添加pom依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

application.properties添加redis连接配置

spring.redis.host=192.168.2.12
spring.redis.port=6379
spring.redis.password=123456

使用RedisTemplate

针对不同的redis数据类型,spring封装了不同的template,本例只展示了StringRedisTemplate的最基本使用方法。

@RestController
@RequestMapping("redis")
public class RedisController {

    private final StringRedisTemplate stringRedisTemplate;

    @Autowired
    public RedisController(StringRedisTemplate stringRedisTemplate) {
        this.stringRedisTemplate = stringRedisTemplate;
    }

    @GetMapping("set/{key}/{value}")
    public String setString(@PathVariable String key, @PathVariable String value) {
        ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();
        operations.append(key, value);
        return "success";
    }
}

 

posted @ 2018-10-26 16:34  柚子苹果果  阅读(197)  评论(0编辑  收藏  举报