File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed
skill_db/src/main/java/jp/wicresoft/jdbc Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments