java redis hash_Java实体和redis中的Hash相互的转换

这篇博客提供了两个Java工具类,用于将Java实体对象转换为Redis中的Hash,以及将Redis中的Hash转换回Java实体。代码中包含了对字段值为"null"字符串的处理,避免转换时出现问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

项目中使用redis作为缓存,此代码用来将Java实体映射成redis中对应的Hash,和将redis中的Hash映射成Java实体

1.[代码][Java]代码

package com.i91ku.onepiece.cache.utils;

import com.google.common.collect.Maps;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.ConvertUtils;

import org.apache.commons.beanutils.converters.*;

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.util.Map;

/**

*
CreateDate : 14-12-22 上午11:51

*

* @author gongweixin

* @version 1.0

*/

public class EntityUtils {

static {

ConvertUtils.register(new LongConverter(null), Long.class);

ConvertUtils.register(new ByteConverter(null), Byte.class);

ConvertUtils.register(new IntegerConverter(null), Integer.class);

ConvertUtils.register(new DoubleConverter(null), Double.class);

ConvertUtils.register(new ShortConverter(null), Short.class);

ConvertUtils.register(new FloatConverter(null), Float.class);

}

public static Map objectToHash(Object obj) {

try {

Map map = Maps.newHashMap();

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor property : propertyDescriptors) {

if (!property.getName().equals("class")) {

map.put(property.getName(), "" + property.getReadMethod().invoke(obj));

}

}

return map;

} catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) {

throw new RuntimeException(e);

}

}

@SuppressWarnings("unchecked")

public static void hashToObject(Map, ?> map, Object obj) {

try {

for (Map.Entry, ?> entry : map.entrySet()) {

if (entry.getValue().equals("null")) {

entry.setValue(null);

}

}

BeanUtils.populate(obj, (Map)map);

} catch (IllegalAccessException | InvocationTargetException e) {

throw new RuntimeException(e);

}

}

@SuppressWarnings("unchecked")

public static T hashToObject(Map, ?> map, Class c) {

try {

Object obj = c.newInstance();

hashToObject(map, obj);

return (T)obj;

} catch (InstantiationException | IllegalAccessException e) {

throw new RuntimeException(e);

}

}

}

2.[代码]之前的实现如果某个字段用户设置的就是"null"这个字符串会有问题,以下为修复版

package com.i91ku.onepiece.cache.utils;

import com.google.common.collect.Maps;

import com.i91ku.common.util.ObjectUtil;

import org.apache.commons.beanutils.BeanUtils;

import org.apache.commons.beanutils.ConvertUtils;

import org.apache.commons.beanutils.converters.*;

import java.beans.BeanInfo;

import java.beans.IntrospectionException;

import java.beans.Introspector;

import java.beans.PropertyDescriptor;

import java.lang.reflect.InvocationTargetException;

import java.util.Map;

/**

*
CreateDate : 14-12-22 上午11:51

*

* @author gongweixin

* @version 1.1

*/

public class EntityUtils {

static {

ConvertUtils.register(new LongConverter(null), Long.class);

ConvertUtils.register(new ByteConverter(null), Byte.class);

ConvertUtils.register(new IntegerConverter(null), Integer.class);

ConvertUtils.register(new DoubleConverter(null), Double.class);

ConvertUtils.register(new ShortConverter(null), Short.class);

ConvertUtils.register(new FloatConverter(null), Float.class);

}

public static Map objectToHash(Object obj) {

try {

Map map = Maps.newHashMap();

BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());

PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();

for (PropertyDescriptor property : propertyDescriptors) {

if (!property.getName().equals("class")) {

if (property.getReadMethod().invoke(obj) != null) {

map.put(property.getName(), "" + property.getReadMethod().invoke(obj));

}

}

}

return map;

} catch (IntrospectionException | InvocationTargetException | IllegalAccessException e) {

throw new RuntimeException(e);

}

}

@SuppressWarnings("unchecked")

private static void hashToObject(Map, ?> map, Object obj) {

try {

BeanUtils.populate(obj, (Map)map);

} catch (IllegalAccessException | InvocationTargetException e) {

throw new RuntimeException(e);

}

}

@SuppressWarnings("unchecked")

public static T hashToObject(Map, ?> map, Class c) {

if (ObjectUtil.isEmpty(map)) {

return null;

}

try {

Object obj = c.newInstance();

hashToObject(map, obj);

return (T)obj;

} catch (InstantiationException | IllegalAccessException e) {

throw new RuntimeException(e);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值