## 1.maven引入相關依賴 ~~~xml org.springframework.boot spring-boot-starter-data-redis org.apache.commons commons-pool2 2.11.1 com.fasterxml.jackson.core jac ...
1.maven引入相關依賴
<dependencies>
<!-- spring-boot-starter-data-redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- commons-pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>2.11.1</version>
</dependency>
<!--jackjson-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<!--lombok-->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!--junit-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2.配置redis
application.yml
spring:
# 配置redis
redis:
host: 192.168.***.***
port: 6379
password: ******
lettuce:
pool:
max-active: 8 # 連接池最大連接數(使用負值表示沒有限制)
max-idle: 8 # 連接池中的最大空閑連接
max-wait: 100 # 連接池最大阻塞等待時間(使用負值表示沒有限制)
min-idle: 0 # 連接池中的最小空閑連接
database: 0 # redis資料庫索引(預設為0)
3.配置序列化
package com.example.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
/**
* Redis 序列化方式配置
*
*/
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
// 創建RedisTemplate<String, Object>對象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置連接工廠
template.setConnectionFactory(factory);
// json方式序列化對象
GenericJackson2JsonRedisSerializer jsonRedisSerializer =
new GenericJackson2JsonRedisSerializer();
// key採用String的序列化方式
template.setKeySerializer(RedisSerializer.string());
// hash的key也採用String的序列化方式
template.setHashKeySerializer(RedisSerializer.string());
// value序列化方式採用jackson
template.setValueSerializer(jsonRedisSerializer);
// hash的value序列化方式採用jackson
template.setHashValueSerializer(jsonRedisSerializer);
return template;
}
}
4.測試類中進行測試
package com.example;
import com.example.entity.User;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
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;
@SpringBootTest
class SpringDataRedisTestApplicationTests {
@Autowired
private RedisTemplate<String,Object> redisTemplate;
@Autowired
private StringRedisTemplate stringRedisTemplate;
private static final ObjectMapper objectMapper = new ObjectMapper();
/**
* 測試redis的String類型
*/
@Test
void testString() {
redisTemplate.opsForValue().set("name", "minqiliang");
System.out.println(redisTemplate.opsForValue().get("name"));
}
/**
* 使用StringRedisTemplate操作redis(需要手動進行序列化和反序列化)
* @throws JsonProcessingException
*/
@Test
void testString2() throws JsonProcessingException {
// 創建一個對象
User user = new User("001", "minqiliang", 18);
// 由於StringRedisTemplate預設使用的是String的序列化器,所以這裡需要將對象轉換成json字元串
String json = objectMapper.writeValueAsString(user);
// 存入redis
stringRedisTemplate.opsForValue().set("user:001", json);
// 從redis中取出數據
String s = stringRedisTemplate.opsForValue().get("user:001");
// 將json字元串轉換成對象
User u = objectMapper.readValue(s, User.class);
System.out.println(u);
}
@Test
void testHash() {
// 存入redis
redisTemplate.opsForHash().put("user:002", "id", "002");
redisTemplate.opsForHash().put("user:002", "name", "張三");
redisTemplate.opsForHash().put("user:002", "age", 18);
// 獲取對應的key的所有值
System.out.println(redisTemplate.opsForHash().entries("user:002"));
System.out.println("====================================");
// 獲取對應的key的某個值
System.out.println(redisTemplate.opsForHash().get("user:002", "id"));
System.out.println(redisTemplate.opsForHash().get("user:002", "name"));
System.out.println(redisTemplate.opsForHash().get("user:002", "age"));
}
}