快速入门
测试Demon
导入依赖
<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: 185.222.22.22
port: 6379
password: 2312343697
jedis:
pool:
# 最大连接
max-active: 8
# 最大空闲连接
max-idle: 8
# 最小连接
min-idle: 0
# 等待时常
max-wait: 100ms
测试入门
package com.example.springboot_redis;
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 SpringbootRedisApplicationTests {
@Autowired
// 自动装配
private RedisTemplate redisTemplate;
@Test
void contextLoads() {
// 字符串操作存储一个值
redisTemplate.opsForValue().set("name","虎哥");
// 获取string值
Object name = redisTemplate.opsForValue().get("name");
System.out.println(name);
}
}
RedisSerializer
引入json处理依赖不然会报错
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
自定义RedisTemplate来序列号字符串
package com.example.springboot_redis.redis.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;
import java.lang.reflect.GenericArrayType;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<String,Object>redisTemplate(RedisConnectionFactory connectionFactory){
// 创建RedisTemplate对象
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 设置连接工厂
template.setConnectionFactory(connectionFactory);
// 创建Json的序列化
GenericJackson2JsonRedisSerializer jsonRedisSerializer = new GenericJackson2JsonRedisSerializer();
// 设置key的序列化
template.setKeySerializer(RedisSerializer.string());
// 设置Hashkey的序列号
template.setHashKeySerializer(RedisSerializer.string());
// 设置Value的序列号
template.setValueSerializer(jsonRedisSerializer);
// 设置Hashvalue的序列化
template.setHashValueSerializer(jsonRedisSerializer);
// 返回
return template;
}
}
添加一个泛型
private RedisTemplate<String,Object> redisTemplate;
测试
序列化对象
测试对象
package com.example.springboot_redis.pojo;
public class User {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public User() {
}
public User(String name, Integer age) {
this.name = name;
this.age = age;
}
}
测试类
@Test
void testSaveUser(){
// 写入数据
redisTemplate.opsForValue().set("user:100",new User("富爸爸",11));
// 获取数据
User o = (User) redisTemplate.opsForValue().get("user:100");
System.out.println(o);
}
获取结果时候会帮我们自动反序列化回来成一个对象
StringRedisTemplate
// 操作字符串
RedisTemplate redisTemplate.opsForValue()
// 操作hash
RedisTemplate redisTemplate.opsForHash()
// 操作list
RedisTemplate redisTemplate.opsForList()
// 操作set
RedisTemplate redisTemplate.opsForSet()
// 操作有序set
RedisTemplate redisTemplate.opsForZSet()
新建一个测试类
自动装配换成stringRedisTemplate
package com.example.springboot_redis;
package com.example.springboot_redis;
import com.example.springboot_redis.pojo.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 StringRedistemp {
@Autowired
// 自动装配
private StringRedisTemplate stringRedisTemplate;
// 序列号对象
private static final ObjectMapper mapper= new ObjectMapper();
@Test
void contextLoads() throws JsonProcessingException {
// 创建对象
User user =new User("pyy",12);
// 手动序列化
String users = mapper.writeValueAsString(user);
// 写入数据
stringRedisTemplate.opsForValue().set("name:2",users);
String name = stringRedisTemplate.opsForValue().get("name:2");
// 手动反序列化
User user1 = mapper.readValue(name, User.class);
System.out.println(user1);
}
}
操作Hash类型
@Test
void testHash(){
stringRedisTemplate.opsForHash().put("user:200","name","配音员");
Map<Object, Object> entries = stringRedisTemplate.opsForHash().entries("user:200");
System.out.println(entries);
}