项目中使用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);
}
}
}