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 数据。

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM