本教程使用的SpringBoot版本是 2.4.2
整合Redis十分简单,以下是项目结构和详细配置:
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.2</version>
</parent>
<dependencies>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- spring boot 单元测试依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
</dependencies>
application.properties
spring:
redis:
database: 0
host: 192.168.0.150
port: 6379
password:
测试类
package com.terry.redis.test;
import com.terry.redis.App;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
@SpringBootTest(classes = App.class)
public class TestRedisTemplate {
@Autowired
private RedisTemplate<String, String> redisTemplate;
@Test
public void getTest(){
Object stock = redisTemplate.opsForValue().get("stock");
System.out.println("redis数据:" + stock);
}
}