Redis在springboot中使用,读取字符串

1. Springboot starter 提供了 redis 的starter

spring-boot-starter-data-redis

 https://docs.spring.io/spring-boot/docs/2.3.0.M4/reference/htmlsingle/#using-boot-starter

 2. Maven 引入starter

https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-redis

当前最新版本是

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

可以看到它的依赖

 

 3. 更新parent节点

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
    </parent>

 

 

更新redis starter

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

 

4. 在application.properties中增加redis必要配置

#redis
spring.redis.host = localhost
spring.redis.port = 6379

5. 使用默认的 StringRedisTemplate 读取字符串数据

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
String key1 = stringRedisTemplate.opsForValue().get("key1");

至此,springboot工程可以读取出 redis 数据。

 

posted @ 2020-04-23 13:11  pkyou  阅读(2173)  评论(0编辑  收藏  举报