實例如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
package com.ljq.util; import java.beans.BeanInfo; import java.beans.Introspector; import java.beans.PropertyDescriptor; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Map工具類 * * @author jqlin */ public class MapUtils { /** * 從map集合中獲取屬性值 * * @param <E> * @param map * map集合 * @param key * 鍵對 * @param defaultValue * 默認值 * @return * @author jiqinlin */ @SuppressWarnings ({ "unchecked" , "rawtypes" }) public final static <E> E get(Map map, Object key, E defaultValue) { Object o = map.get(key); if (o == null ) return defaultValue; return (E) o; } /** * Map集合對象轉化成 JavaBean集合對象 * * @param javaBean JavaBean實例對象 * @param mapList Map數據集對象 * @return * @author jqlin */ @SuppressWarnings ({ "rawtypes" }) public static <T> List<T> map2Java(T javaBean, List<Map> mapList) { if (mapList == null || mapList.isEmpty()){ return null ; } List<T> objectList = new ArrayList<T>(); T object = null ; for (Map map : mapList){ if (map != null ){ object = map2Java(javaBean, map); objectList.add(object); } } return objectList; } /** * Map對象轉化成 JavaBean對象 * * @param javaBean JavaBean實例對象 * @param map Map對象 * @return * @author jqlin */ @SuppressWarnings ({ "rawtypes" , "unchecked" , "hiding" }) public static <T> T map2Java(T javaBean, Map map) { try { // 獲取javaBean屬性 BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass()); // 創建 JavaBean 對象 Object obj = javaBean.getClass().newInstance(); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if (propertyDescriptors != null && propertyDescriptors.length > 0 ) { String propertyName = null ; // javaBean屬性名 Object propertyValue = null ; // javaBean屬性值 for (PropertyDescriptor pd : propertyDescriptors) { propertyName = pd.getName(); if (map.containsKey(propertyName)) { propertyValue = map.get(propertyName); pd.getWriteMethod().invoke(obj, new Object[] { propertyValue }); } } return (T) obj; } } catch (Exception e) { e.printStackTrace(); } return null ; } /** * JavaBean對象轉化成Map對象 * * @param javaBean * @return * @author jqlin */ @SuppressWarnings ({ "rawtypes" , "unchecked" }) public static Map java2Map(Object javaBean) { Map map = new HashMap(); try { // 獲取javaBean屬性 BeanInfo beanInfo = Introspector.getBeanInfo(javaBean.getClass()); PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors(); if (propertyDescriptors != null && propertyDescriptors.length > 0 ) { String propertyName = null ; // javaBean屬性名 Object propertyValue = null ; // javaBean屬性值 for (PropertyDescriptor pd : propertyDescriptors) { propertyName = pd.getName(); if (!propertyName.equals( "class" )) { Method readMethod = pd.getReadMethod(); propertyValue = readMethod.invoke(javaBean, new Object[ 0 ]); map.put(propertyName, propertyValue); } } } } catch (Exception e) { e.printStackTrace(); } return map; } } |
以上就是小編為大家帶來的JavaBean和Map轉換封裝類的方法全部內容了,希望大家多多支持服務器之家~