JavaBean转Map工具类

原文地址:https://blog.csdn.net/qisuanli0846/article/details/124041706


import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cglib.beans.BeanMap;

import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
* @author :Cheney
* @date :Created in 2022/4/8 14:42
* @description:TODO
* @modified By:
* @version: 0.0.1$
*/

@Slf4j
public class JavaBeanUtils {
/**
* 将对象转化为map
* @param bean
* @param <T>
* @return
*/
public static <T> Map<String, Object> beanToMap(T bean) {
Map<String, Object> map = Maps.newHashMap();
if (bean != null) {
BeanMap beanMap = BeanMap.create(bean);
for (Object key : beanMap.keySet()) {
map.put(key.toString(), beanMap.get(key));
}
}
return map;
}

/**
* 将list<JavaBean>转化为List<Map<String, Object>>
*
* @param objList
* @param <T>
* @return
*/
public static <T> List<Map<String, Object>> objectsToMaps(List<T> objList) {
List<Map<String, Object>> list = Lists.newArrayList();
if (objList != null && objList.size() > 0) {
Map<String, Object> map = null;
T bean = null;
for (int i = 0, size = objList.size(); i < size; i++) {
bean = objList.get(i);
map = beanToMap(bean);
list.add(map);
}
}
return list;
}

/**
* 通过lambda表达式将list<JavaBean>转化为List<Map<String, Object>>
*
* @param objList
* @param <T>
* @return
*/
public static <T> List<Map<String, Object>> objectsToMaps1(List<T> objList) {
return objList.stream().map(new Function<T, Map<String, Object>>() {
@Override
public Map<String, Object> apply(T t) {
Map<String, Object> map = new HashMap<>();
BeanMap beanMap = BeanMap.create(t);
for (Object key : beanMap.keySet()) {
map.put(key.toString(), beanMap.get(key));
}
return map;
}
}).collect(Collectors.toList());
//return collect;
}

/**
* 将一个 Map 对象转化为一个 JavaBean
* @param type 要转化的类型
* @param map 包含属性值的 map
* @return 转化出来的 JavaBean 对象
* @throws IntrospectionException
* 如果分析类属性失败
* @throws IllegalAccessException
* 如果实例化 JavaBean 失败
* @throws InstantiationException
* 如果实例化 JavaBean 失败
* @throws InvocationTargetException
* 如果调用属性的 setter 方法失败
*/
public static Object convertMap(Class type, Map map) throws IntrospectionException, IllegalAccessException, InstantiationException, InvocationTargetException {
BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
Object obj = type.newInstance(); // 创建 JavaBean 对象

// 给 JavaBean 对象的属性赋值
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();

if (map.containsKey(propertyName)) {
// 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值
try {
Object value = map.get(propertyName);

Object[] args = new Object[1];
args[0] = value;

descriptor.getWriteMethod().invoke(obj, args);
} catch (Exception e) {
log.error("Map对象转化JavaBean有些属性赋值失败,无需处理");
}
}
}
return obj;
}


/**
* 将一个 JavaBean 对象转化为一个 Map
* @param bean 要转化的JavaBean 对象
* @return 转化出来的 Map 对象
* @throws IntrospectionException 如果分析类属性失败
* @throws IllegalAccessException 如果实例化 JavaBean 失败
* @throws InvocationTargetException 如果调用属性的 setter 方法失败
*/
public static Map convertBean(Object bean)
throws IntrospectionException, IllegalAccessException, InvocationTargetException {
Class type = bean.getClass();
Map returnMap = new HashMap();
BeanInfo beanInfo = Introspector.getBeanInfo(type);

PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (int i = 0; i< propertyDescriptors.length; i++) {
PropertyDescriptor descriptor = propertyDescriptors[i];
String propertyName = descriptor.getName();
if (!propertyName.equals("class")) {
Method readMethod = descriptor.getReadMethod();
Object result = readMethod.invoke(bean, new Object[0]);
if (result != null) {
returnMap.put(propertyName, result);
} else {
returnMap.put(propertyName, "");
}
}
}
return returnMap;
}
}
文章知识点与官方知识档案匹配,可进一步学习相关知识
————————————————
版权声明:本文为CSDN博主「Cheney94」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qisuanli0846/article/details/124041706

posted @ 2022-07-20 13:40  eyesfree  阅读(545)  评论(0编辑  收藏  举报