1导入Redis依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.yml配置中配置Redis(主机、端口)
spring:
redis:
host: localhost
port: 6379
3.调用RedisTemplate提供的操作各种数据存储类型的接口API ,客户端操作RedisTemplate
@SpringBootTest
class SpringbootredisApplicationTests {
@Autowired
private RedisTemplate redisTemplate;
@Test
void set() {
ValueOperations valueOperations = redisTemplate.opsForValue();
valueOperations.set("age",18);
}
@Test
void get() {
ValueOperations valueOperations = redisTemplate.opsForValue();
Object age = valueOperations.get("age");
System.out.println(age);
}
}
4StringReidsTemplate以字符串 作为key和value,与redis客户端操作等效
@SpringBootTest
class SpringbootredisApplicationTests {
@Autowired
private StringRedisTemplate stringRedisTemplate;
@Test
void set(){
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
stringStringValueOperations.set("testy","testValue");
}
@Test
void get(){
ValueOperations<String, String> stringStringValueOperations = stringRedisTemplate.opsForValue();
String testy = stringStringValueOperations.get("testy");
System.out.println(testy);
}
}
浙公网安备 33010602011771号