SpringBoot版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/>
</parent>
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<!-- 带上版本号可能会遇到问题 -->
</dependency>
配置文件
spring:
redis:
host: localhost
port: 6379
database: 0
timeout: 10000
lettuce:
pool:
max-active: 10
max-wait: -1
max-idle: 5
min-idle: 0
单元测试示例:
package com.etoak;
import com.alibaba.fastjson.JSON;
import com.etoak.test.Student;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* @ClassName: TestApp
* @Description: 类描述
* @author: wsdhla
* @date: 2023/06/29 18:50
*/
@SpringBootTest
public class TestAppTests {
@Resource
private RedisTemplate redisTemplate;
@Test
public void testRedis() {
List<Student> stuList = new ArrayList<>();
stuList.add(new Student("name1", 100));
stuList.add(new Student("name2", 60));
redisTemplate.opsForValue().set("stuList", stuList);
List<Student> stuList1 = (List<Student>) redisTemplate.opsForValue().get("stuList");
System.out.println(JSON.toJSONString(stuList1));
}
}