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