目录一、背景1、思考2、方案二、源码分析三、注入RedisTemplate1、引入依赖2、Redis 连接信息3、Redis 核心配置类4、Redis工具类四、测试1、创建 Java 实体类 UserInfo2、测试用例
之前介绍过 https://www.jb51.net/article/223539.htm 我们可以看出,在 SpringBoot 对 Redis 做了一系列的自动装配,使用还是非常方便的
通过我们前面的学习,我们已经可以往 Redis 中存入字符串,那么我们要往 Redis 中存入 Java 对象该怎么办呢?
我们可以将 Java 对象转化为 JSON 对象,然后转为 jsON 字符串,存入 Redis,那么我们从 Redis 中取出该数据的时候,我们也只能取出字符串,并转为 Java 对象,这一系列的操作是不是显得有些麻烦呢?
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
以上引入了 redis 的依赖,其余依赖请自行添加
spring:
# Redis配置
redis:
host: 127.0.0.1
port: 6379
database: 10
jedis:
pool:
# 连接池最大连接数(使用负值表示没有限制)
max-active: 50
# 连接池最大阻塞等待时间(使用负值表示没有限制)
max-wait: 3000ms
# 连接池中的最大空闲连接数
max-idle: 20
# 连接池中的最小空闲连接数
min-idle: 5
# 连接超时时间(毫秒)
timeout: 5000ms
Redis 的核心配置我们放在 RedisConfig.java 文件中
package com.zyxx.redistest.common;
import com.fasterXML.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
@Configuration
public class RedisConfig {
@Bean
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
// 配置redisTemplate
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setConnectionFactory(redisConnectionFactory);
// 设置序列化
Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
ObjectMapper om = new ObjectMapper();
om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
om.activateDefaultTyping(LaissezFaireSubTypeValidator.instance, ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY);
jackson2JsonRedisSerializer.setObjectMapper(om);
// key序列化
redisTemplate.seTKEySerializer(new StringRedisSerializer());
// value序列化
redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
// Hash key序列化
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
// Hash value序列化
redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
redisTemplate.afterPropertiesSet();
return redisTemplate;
}
}
我们注入了一个名称为 redisTemplate,类型为 RedisTemplate<String, Object> 的 Bean,key 采用 StringRedisSerializer 序列化方式,value 采用 Jackson2JsonRedisSerializer 序列化方式
我们将对 Redis 进行的一系列操作放在 RedisUtils.java 文件中
package com.zyxx.redistest.common;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class RedisUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
public Object get(final String key) {
if (StringUtils.isBlank(key)) {
return null;
}
try {
return redisTemplate.opsForValue().get(key);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean set(final String key, Object value) {
if (StringUtils.isBlank(key)) {
return false;
}
try {
redisTemplate.opsForValue().set(key, value);
log.info("存入redis成功,key:{},value:{}", key, value);
return true;
} catch (Exception e) {
log.error("存入redis失败,key:{},value:{}", key, value);
e.printStackTrace();
}
return false;
}
}
我们写入了 get,set 两个方法用于测试
package com.zyxx.redistest.common;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
@Data
public class UserInfo implements Serializable {
private Integer id;
private String name;
private Date createTime;
}
package com.zyxx.redistest;
import com.zyxx.redistest.common.RedisUtils;
import com.zyxx.redistest.common.UserInfo;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.Date;
@SpringBootTest
class RedisTestApplicationTests {
@Autowired
private RedisUtils redisUtil;
@Test
void contextLoads() {
UserInfo userInfo = new UserInfo();
userInfo.setId(1);
userInfo.setName("jack");
userInfo.setCreateTime(new Date());
// 放入redis
redisUtil.set("user", userInfo);
// 从redis中获取
System.out.println("获取到数据:" + redisUtil.get("user"));
}
}
我们向 Redis 中存入了一个 key 为 ”user“,value 为 UserInfo 对象的数据,然后再根据 key 获取该数据
可以看出,我们往 Redis 中成功存入 Java 对象数据,并成功获取到了该对象
到此这篇关于SpringBoot整合Redis实现序列化存储Java对象的文章就介绍到这了,更多相关SpringBoot整合Redis序列化存储Java对象内容请搜索我们以前的文章或继续浏览下面的相关文章希望大家以后多多支持我们!
--结束END--
本文标题: SpringBoot整合Redis实现序列化存储Java对象的操作方法
本文链接: https://www.lsjlt.com/news/200975.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
2024-05-03
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0