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
141
142
143
144
145
146
147
148
149
|
public class BeanMapInter {
/**
* 将javabean对象转换为map
*/
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()) {
Object value = beanMap.get(key);
map.put(key + "", value);
}
}
return map;
}
/**
* 将map转换为javabean对象
*/
public static <T> T mapToBean(Map<String, Object> map, T bean) {
if (map != null) {
BeanMap beanMap = BeanMap.create(bean);
beanMap.putAll(map);
}
return bean;
}
/**
* 内省(Introspector) 是Java 语言对 JavaBean 类属性、事件的一种缺省处理方法。
* @param clazz
* @param object
* @param userName
* @param <T>
* @throws Exception
*/
public static <T> void setProperty(Class<T> clazz, T object, String userName)throws Exception{
PropertyDescriptor propDesc=new PropertyDescriptor(userName, clazz);
Method methodSetUserName=propDesc.getWriteMethod();
methodSetUserName.invoke(object, "wong");
}
public static <T> void getProperty(Class<T> clazz, T object, String userName)throws Exception{
PropertyDescriptor proDescriptor =new PropertyDescriptor(userName, clazz);
Method methodGetUserName=proDescriptor.getReadMethod();
Object objUserName=methodGetUserName.invoke(object);
}
public static Map<String, Object> bean2Map(Object obj) {
if (obj == null) {
return null;
}
Map<String, Object> map = new HashMap<>();
try {
BeanInfo beanInfo = Introspector.getBeanInfo(obj.getClass());
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor property : propertyDescriptors) {
String key = property.getName();
// 过滤class属性
if (!key.equals("class")) {
// 得到property对应的getter方法
Method getter = property.getReadMethod();
Object value = getter.invoke(obj);
map.put(key, value);
}
}
} catch (Exception e) {
e.printStackTrace();
}
return map;
}
public <T> Object mapToBean(Class<T> type, Map<String, Object> map) {
Object obj = null;
try {
BeanInfo beanInfo = Introspector.getBeanInfo(type);
obj = type.newInstance();
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
for (PropertyDescriptor descriptor : propertyDescriptors) {
String propertyName = descriptor.getName();
if (map.containsKey(propertyName)) {
Object value = map.get(propertyName);
descriptor.getWriteMethod().invoke(obj, value);
}
}
} catch (Exception e) {
System.out.println("map转实体类出现异常");
}
return obj;
}
}
@Data
public class RowItem {
private Long id;
private String name;
private Integer age;
private LocalDateTime createTime;
private BigDecimal amount;
public void test(){
Map<String, Function<RowItem, Object>> getMap = new HashMap<>();
getMap.put("id",RowItem::getId);
getMap.put("name",RowItem::getName);
getMap.put("age",RowItem::getAge);
getMap.put("createTime",RowItem::getCreateTime);
getMap.put("amount",RowItem::getAmount);
Map<String, BiConsumer<RowItem, Object>> setMap = new HashMap<>();
setMap.put("id",(row, o)-> row.setId((Long) o));
setMap.put("name",(row, o)-> row.setName(String.valueOf(o)));
setMap.put("age",(row, o)-> row.setAge((Integer) o));
setMap.put("createTime",(row, o)-> row.setCreateTime((LocalDateTime) o));
setMap.put("amount",(row, o)-> row.setAmount((BigDecimal) o));
RowItem rowItem = new RowItem();
rowItem.setId(1L);
rowItem.setName("Ethan");
rowItem.setAge(18);
rowItem.setCreateTime(LocalDateTime.now());
rowItem.setAmount(new BigDecimal("999999"));
Map<String, Object> result = new HashMap<>();
for (Map.Entry<String, Function<RowItem, Object>> entry : getMap.entrySet()) {
result.put(entry.getKey(), entry.getValue().apply(rowItem));
}
System.out.println(result);
result.put("id",2L);
result.put("name","Wang");
result.put("age",30);
result.put("createTime", LocalDateTime.now());
result.put("amount", new BigDecimal("77777777"));
RowItem resultRow = new RowItem();
for (Map.Entry<String, BiConsumer<RowItem, Object>> entry : setMap.entrySet()) {
entry.getValue().accept(resultRow,result.get(entry.getKey()));
}
System.out.println(resultRow);
}
|