SpringData是Spring中数据操作的模块,包含对各种数据库的集成,其中对Redis的集成模块就叫做SpringDataRedis,官网地址:Spring Data Redis
1、不同数据类型的操作AP
api | 返回值类型 | 说明 |
redisTemplate.opsForValue() | ValueOpeerations | 操作String类型数据 |
redisTemplate.opsForHash() | HashOpeerations | 操作Hash类型数据 |
redisTemplate.opsForList() | ListOpeerations | 操作Lsit类型数据 |
redisTemplate.opsForSet() | SetOpeerations | 操作Set类型数据 |
redisTemplate.opsForZSet() | ZSetOpeerations | 操作SortedSet类型数据 |
redisTemplate | 通用的命令 |
2、在pom.xml中需要引入
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><!--common-pool--><dependency><groupId>org.apache.commons</groupId><artifactId>commons-pool2</artifactId></dependency><!--Jackson依赖--><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency>
3、yml中配置
spring:redis:host: localhostport: 6379password:lettuce:pool:max-active: 8max-wait: 100msmax-idle: 8min-idle: 0
4、测试
package org.excaple.demo3;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
class Demo3ApplicationTests {@Autowiredprivate RedisTemplate redisTemplate;@Testvoid contextLoads() {// 写入一条String数据redisTemplate.opsForValue().set("name","huli");// 获取string数据Object name = redisTemplate.opsForValue().get("name");System.out.println(name);}}