Skip to content

Commit ca9c701

Browse files
committed
create reflection method convertJdbcMapToBean
1 parent 9575ab1 commit ca9c701

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package jp.wicresoft.jdbc;
2+
3+
import java.lang.reflect.InvocationTargetException;
4+
import java.lang.reflect.Method;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Map;
8+
9+
public class BasicJdbc {
10+
11+
public <T> List<T> convertJdbcMapToBean(List<Map<String, Object>> mapList, Class<T> clazz) {
12+
List<T> resultList = new ArrayList<T>();
13+
for (Map<String, Object> map : mapList) {
14+
try {
15+
// initial Object T
16+
T object = clazz.newInstance();
17+
Method[] methods = clazz.getMethods();
18+
for (String key : map.keySet()) {
19+
// get each object and name
20+
Object value = map.get(key);
21+
// get setMethod
22+
for (Method method : methods) {
23+
if (method.getName().equals(translateSetMethodName(key))) {
24+
method.invoke(object, value);
25+
}
26+
}
27+
}
28+
resultList.add(object);
29+
} catch (SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException | InstantiationException e) {
30+
// TODO Auto-generated catch block
31+
e.printStackTrace();
32+
}
33+
}
34+
return resultList;
35+
}
36+
37+
private String translateSetMethodName(String key) {
38+
return "set" + key.substring(0, 1).toUpperCase() + key.substring(1);
39+
}
40+
}

0 commit comments

Comments
 (0)