Java-实体与集合转换

  1 import java.beans.BeanInfo;
  2 import java.beans.IntrospectionException;
  3 import java.beans.Introspector;
  4 import java.beans.PropertyDescriptor;
  5 import java.lang.reflect.InvocationTargetException;
  6 import java.lang.reflect.Method;
  7 import java.util.ArrayList;
  8 import java.util.Collection;
  9 import java.util.HashMap;
 10 import java.util.List;
 11 import java.util.Map;
 12 
 13 /**
 14  * 用于对javabean和map之间的相互装换的工具类
 15  */
 16 public class BeanMapConvertUtils {
 17     
 18     /**
 19      * 将Map转为javaBean
 20      */
 21     public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass) {
 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         return obj;
 45     }
 46 
 47     /**
 48      * @Description: 将Map转为javaBean
 49      * @param map 要转换的map对象
 50      * @param beanClass 需要转成的javabean的class
 51      * @param requiredPropertys 需要转换后包含的属性名集合
 52      */
 53     public static <T> T mapToObject(Map<String, Object> map, Class<T> beanClass, Collection<String> requiredPropertys) {
 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         return obj;
 80     }
 81 
 82     /**
 83      *  将javabean集合转为map集合
 84      * @param objList avabean集合
 85      * @param requiredPropertys 需要转换后包含的属性名集合
 86      */
 87     public static List<Map<String, Object>> objectsToMaps(Collection<?> objList, Collection<String> requiredPropertys) {
 88         List<Map<String, Object>> arrayList = new ArrayList<Map<String, Object>>();
 89         if (objList != null) {
 90             for (Object obj : objList) {
 91                 Map<String, Object> objectToMap = objectToMap(obj, requiredPropertys);
 92                 if (objectToMap != null) {
 93                     arrayList.add(objectToMap);
 94                 }
 95             }
 96         }
 97         return arrayList;
 98     }
 99 
100     /**
101      * 将javaBean转为Map
102      */
103     public static Map<String, Object> objectToMap(Object obj) {
104         if (obj == null)
105             return null;
106         Map<String, Object> map = new HashMap<String, Object>();
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         return map;
124     }
125 
126     /**
127      *  将Map转为javaBean
128      * @param obj 要转换的javabean对象
129      * @param requiredPropertys 需要转换后包含的属性名集合
130      */
131     public static Map<String, Object> objectToMap(Object obj, Collection<String> requiredPropertys) {
132         if (obj == null)
133             return null;
134         Map<String, Object> map = new HashMap<String, Object>();
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         return map;
155     }
156 }

猜你喜欢

转载自www.cnblogs.com/wang1001/p/9768285.html