目录
1. 新建 SpringBoot 项目
选择 SpringBoot 中的 Redis 依赖
2. 基础操作
代码结构
├── pom.xml
├── src
│ ├── main
│ │ ├── java
│ │ │ └── com
│ │ │ └── example
│ │ │ └── study
│ │ │ └── SpringBootTestApplication.java
│ │ └── resources
│ │ └── application.properties
│ └── test
│ └── java
│ └── com
│ └── example
│ └── study
│ └── SpringBootTestApplicationTests.java
本地 Redis 可以默认使用
SpringBootTestApplicationTests.java 代码为:
package com.example.study;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import javax.annotation.Resource;
@SpringBootTest
class SpringBootTestApplicationTests {
@Resource
StringRedisTemplate template;
@Test
void contextLoads() {
template.opsForValue().set("key1", "123");
String result = template.opsForValue().get("key1");
System.out.println(result);
}
}