Java-实体与集合转换

时间:2022-01-19 21:08:34
 import java.beans.BeanInfo;
import java.beans.IntrospectionException;
import java.beans.Introspector;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map; /**
* 用于对javabean和map之间的相互装换的工具类
*/
public class BeanMapConvertUtils { /**
* 将Map转为javaBean
*/
public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) {
if (map == null)
return null;
T obj = null;
BeanInfo beanInfo = null;
try {
obj = beanClass.newInstance();
beanInfo = Introspector.getBeanInfo(obj.getClass());
} catch (IntrospectionException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null; // 如果在创建实例和获取beaninfo出现异常则直接返回null
}
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (setter != null) {
String key = property.getName();
try {
setter.invoke(obj, map.get(key));
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
}
}
}
return obj;
} /**
* @Description: 将Map转为javaBean
* @param map 要转换的map对象
* @param beanClass 需要转成的javabean的class
* @param requiredPropertys 需要转换后包含的属性名集合
*/
public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass, Collection<String> requiredPropertys) {
if (map == null)
return null;
T obj = null;
BeanInfo beanInfo = null;
try {
obj = beanClass.newInstance();
beanInfo = Introspector.getBeanInfo(obj.getClass());
} catch (IntrospectionException | InstantiationException | IllegalAccessException e) {
e.printStackTrace();
return null; // 如果在创建实例和获取beaninfo出现异常则直接返回null
}
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
Method setter = property.getWriteMethod();
if (setter != null) {
String key = property.getName();
if (requiredPropertys != null && !requiredPropertys.contains(key)) {// 如果不包含需要的属性则跳过
continue;
}
try {
setter.invoke(obj, map.get(key));
} catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
}
}
}
return obj;
} /**
* 将javabean集合转为map集合
* @param objList avabean集合
* @param requiredPropertys 需要转换后包含的属性名集合
*/
public static List<Map<String, Object>> objectsToMaps(Collection<?> objList, Collection<String> requiredPropertys) {
List<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
if (objList != null) {
for (Object obj : objList) {
Map<String, Object> objectToMap = objectToMap(obj, requiredPropertys);
if (objectToMap != null) {
arrayList.add(objectToMap);
}
}
}
return arrayList;
} /**
* 将javaBean转为Map
*/
public static Map<String, Object> objectToMap(Object obj) {
if (obj == null)
return null;
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
map.put(key, value);
}
} catch (Exception e) {
map = null;
e.printStackTrace();
}
return map;
} /**
* 将Map转为javaBean
* @param obj 要转换的javabean对象
* @param requiredPropertys 需要转换后包含的属性名集合
*/
public static Map<String, Object> objectToMap(Object obj, Collection<String> requiredPropertys) {
if (obj == null)
return null;
Map<String, Object> map = new HashMap<String, Object>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
if (key.compareToIgnoreCase("class") == 0) {
continue;
}
if (requiredPropertys != null && !requiredPropertys.contains(key)) {// 如果不包含需要的属性则跳过
continue;
}
Method getter = property.getReadMethod();
Object value = getter != null ? getter.invoke(obj) : null;
map.put(key, value);
}
} catch (Exception e) {
map = null;
e.printStackTrace();
}
return map;
}
}