这是图片

配置信息

1
2
3
4
5
6
7
8
9
10
11
spring:
redis:
host: 192.168.235.128
port: 6379
password: 123456
lettuce:
pool:
max-active: 8
max-idle: 8
min-idle: 0
max-wait: 100

导入依然

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.12</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>SpringDataRedisText</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringDataRedisText</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<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>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</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>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>

</project>




RedisTemplate 序列化(配置类)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
//创建redistemplate对象
RedisTemplate<String, Object> objectObjectRedisTemplate = new RedisTemplate<>();
// 设置连接工厂
objectObjectRedisTemplate.setConnectionFactory(redisConnectionFactory);
// 创建json序列化工具
GenericJackson2JsonRedisSerializer genericJackson2JsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// 设置key的序列化
objectObjectRedisTemplate.setKeySerializer(RedisSerializer.string());
objectObjectRedisTemplate.setHashKeySerializer(RedisSerializer.string());
// 设置value的序列化
objectObjectRedisTemplate.setValueSerializer(genericJackson2JsonRedisSerializer);
objectObjectRedisTemplate.setHashValueSerializer(genericJackson2JsonRedisSerializer);
// 返回
return objectObjectRedisTemplate;
}
}

实体类

1
2
3
4
5
6
7
@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
private Integer id;
private String name;
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

package com.example.springdataredistext;

import com.example.springdataredistext.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach;
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;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.Map;

@SpringBootTest
class SpringDataRedisTextApplicationTests {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ObjectMapper mapper = new ObjectMapper();
@Test
void contextLoads() {
redisTemplate.opsForValue().set("spring","helloRedis");
Object o = redisTemplate.opsForValue().get("spring");
System.out.println(o);
}
@Test
void text2(){
redisTemplate.opsForValue().set("spring:user:2",new User(2,"冯越"));
User user =(User) redisTemplate.opsForValue().get("spring:user:2");
System.out.println(user);
}
@Test
void text3() throws JsonProcessingException {
User user = new User(3,"我");
String json = mapper.writeValueAsString(user);
stringRedisTemplate.opsForValue().set("spring:user:3",json);
String re = stringRedisTemplate.opsForValue().get("spring:user:3");
User result = mapper.readValue(re,User.class);
System.out.println(result);
}
@Test
public void text4(){
stringRedisTemplate.opsForHash().put("spring:user:400","id","400");
stringRedisTemplate.opsForHash().put("spring:user:400","name","冯越");
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("spring:user:400");
System.out.println(entries);
}


}