1 importjava.beans.BeanInfo;2 importjava.beans.IntrospectionException;3 importjava.beans.Introspector;4 importjava.beans.PropertyDescriptor;5 importjava.lang.reflect.InvocationTargetException;6 importjava.lang.reflect.Method;7 importjava.util.ArrayList;8 importjava.util.Collection;9 importjava.util.HashMap;10 importjava.util.List;11 importjava.util.Map;12
13 /**
14 * 用于对javabean和map之间的相互装换的工具类15 */
16 public classBeanMapConvertUtils {17
18 /**
19 * 将Map转为javaBean20 */
21 public static T mapToObject(Map map, ClassbeanClass) {22 if (map == null)23 return null;24 T obj = null;25 BeanInfo beanInfo = null;26 try{27 obj =beanClass.newInstance();28 beanInfo =Introspector.getBeanInfo(obj.getClass());29 } catch (IntrospectionException | InstantiationException |IllegalAccessException e) {30 e.printStackTrace();31 return null; //如果在创建实例和获取beaninfo出现异常则直接返回null
32 }33 PropertyDescriptor[] propertyDescriptors =beanInfo.getPropertyDescriptors();34 for(PropertyDescriptor property : propertyDescriptors) {35 Method setter =property.getWriteMethod();36 if (setter != null) {37 String key =property.getName();38 try{39 setter.invoke(obj, map.get(key));40 } catch (IllegalAccessException | IllegalArgumentException |InvocationTargetException e) {41 }42 }43 }44 returnobj;45 }46
47 /**
48 * @Description: 将Map转为javaBean49 *@parammap 要转换的map对象50 *@parambeanClass 需要转成的javabean的class51 *@paramrequiredPropertys 需要转换后包含的属性名集合52 */
53 public static T mapToObject(Map map, Class beanClass, CollectionrequiredPropertys) {54 if (map == null)55 return null;56 T obj = null;57 BeanInfo beanInfo = null;58 try{59 obj =beanClass.newInstance();60 beanInfo =Introspector.getBeanInfo(obj.getClass());61 } catch (IntrospectionException | InstantiationException |IllegalAccessException e) {62 e.printStackTrace();63 return null; //如果在创建实例和获取beaninfo出现异常则直接返回null
64 }65 PropertyDescriptor[] propertyDescriptors =beanInfo.getPropertyDescriptors();66 for(PropertyDescriptor property : propertyDescriptors) {67 Method setter =property.getWriteMethod();68 if (setter != null) {69 String key =property.getName();70 if (requiredPropertys != null && !requiredPropertys.contains(key)) {//如果不包含需要的属性则跳过
71 continue;72 }73 try{74 setter.invoke(obj, map.get(key));75 } catch (IllegalAccessException | IllegalArgumentException |InvocationTargetException e) {76 }77 }78 }79 returnobj;80 }81
82 /**
83 * 将javabean集合转为map集合84 *@paramobjList avabean集合85 *@paramrequiredPropertys 需要转换后包含的属性名集合86 */
87 public static List> objectsToMaps(Collection> objList, CollectionrequiredPropertys) {88 List> arrayList = new ArrayList>();89 if (objList != null) {90 for(Object obj : objList) {91 Map objectToMap =objectToMap(obj, requiredPropertys);92 if (objectToMap != null) {93 arrayList.add(objectToMap);94 }95 }96 }97 returnarrayList;98 }99
100 /**
101 * 将javaBean转为Map102 */
103 public static MapobjectToMap(Object obj) {104 if (obj == null)105 return null;106 Map map = new HashMap();107 try{108 BeanInfo beanInfo =Introspector.getBeanInfo(obj.getClass());109 PropertyDescriptor[] propertyDescriptors =beanInfo.getPropertyDescriptors();110 for(PropertyDescriptor property : propertyDescriptors) {111 String key =property.getName();112 if (key.compareToIgnoreCase("class") == 0) {113 continue;114 }115 Method getter =property.getReadMethod();116 Object value = getter != null ? getter.invoke(obj) : null;117 map.put(key, value);118 }119 } catch(Exception e) {120 map = null;121 e.printStackTrace();122 }123 returnmap;124 }125
126 /**
127 * 将Map转为javaBean128 *@paramobj 要转换的javabean对象129 *@paramrequiredPropertys 需要转换后包含的属性名集合130 */
131 public static Map objectToMap(Object obj, CollectionrequiredPropertys) {132 if (obj == null)133 return null;134 Map map = new HashMap();135 try{136 BeanInfo beanInfo =Introspector.getBeanInfo(obj.getClass());137 PropertyDescriptor[] propertyDescriptors =beanInfo.getPropertyDescriptors();138 for(PropertyDescriptor property : propertyDescriptors) {139 String key =property.getName();140 if (key.compareToIgnoreCase("class") == 0) {141 continue;142 }143 if (requiredPropertys != null && !requiredPropertys.contains(key)) {//如果不包含需要的属性则跳过
144 continue;145 }146 Method getter =property.getReadMethod();147 Object value = getter != null ? getter.invoke(obj) : null;148 map.put(key, value);149 }150 } catch(Exception e) {151 map = null;152 e.printStackTrace();153 }154 returnmap;155 }156 }