Spring JDBCTemplet通用RowMapper帮助类

  1 import java.lang.reflect.Method;
  2 import java.math.BigDecimal;
  3 import java.math.BigInteger;
  4 import java.sql.ResultSet;
  5 import java.sql.ResultSetMetaData;
  6 import java.sql.SQLException;
  7 import java.text.SimpleDateFormat;
  8 import java.util.ArrayList;
  9 import java.util.Date;
 10 import java.util.Iterator;
 11 import java.util.List;
 12 import java.util.Map;
 13 import java.util.Map.Entry;
 14 
 15 import org.springframework.jdbc.core.RowMapper;
 16 
 17 public class SpringJdbcTempletHelper {
 18 
 19     public static <T> RowMapper<T> getRowMapper(Class<T> clazz) {
 20         if (clazz == null) {
 21             return null;
 22         }
 23 
 24         return new RowMapper<T>() {
 25             @Override
 26             public T mapRow(ResultSet rs, int rowNum) throws SQLException {
 27                 try {
 28 
 29                     T bean = clazz.newInstance();
 30 
 31                     Method[] mds = clazz.getDeclaredMethods();
 32                     ResultSetMetaData metaData = rs.getMetaData();
 33                     for (int i = 1; i < metaData.getColumnCount(); i++) {
 34                         // resultSet数据下标从1开始
 35                         String columnName = metaData.getColumnName(i).replace("f_", "").replace("tb_", "")
 36                                 .replace("_", "").toLowerCase();
 37 
 38                         Object val = null;
 39                         for (Method method : mds) {
 40                             String methodName = method.getName().replace("_", "").toLowerCase();
 41                             if (methodName.startsWith("set") && methodName.substring(3).equals(columnName)) {
 42                                 Class<?> type = method.getParameterTypes()[0];
 43                                 if (type == Integer.class) {
 44                                     val = rs.getInt(i);
 45                                 } else if (type == Long.class || type == BigInteger.class) {
 46                                     val = rs.getLong(i);
 47                                 } else if (type == BigDecimal.class) {
 48                                     val = rs.getBigDecimal(i);
 49                                 } else if (type == Double.class) {
 50                                     val = rs.getDouble(i);
 51                                 } else if (type == Float.class) {
 52                                     val = rs.getFloat(i);
 53                                 } else if (type == Boolean.class) {
 54                                     val = rs.getBoolean(i);
 55                                 } else if (type == Byte.class) {
 56                                     val = rs.getByte(i);
 57                                 } else if (type == Short.class) {
 58                                     val = rs.getShort(i);
 59                                 } else if (type == String.class) {
 60                                     val = rs.getString(i);
 61                                 } else if (type == Date.class) {
 62                                     val = rs.getTimestamp(i);
 63                                 } else {
 64                                     val = rs.getShort(i);
 65                                 }
 66                                 if (val == null) {
 67                                     break;
 68                                 } else {
 69                                     method.invoke(bean, val);
 70                                 }
 71                             }
 72                         }
 73                     }
 74                     return bean;
 75                 } catch (Exception e) {
 76                     e.printStackTrace();
 77                 }
 78                 return null;
 79             }
 80         };
 81     }
 82 
 83     public static <T> List<T> converMapListToBeanList(List<Map<String, Object>> dataList, Class<T> clazz) {
 84         List<T> lst = new ArrayList<>();
 85 
 86         if (dataList == null || dataList.size() < 1 || clazz == null) {
 87             return lst;
 88         }
 89 
 90         try {
 91             // 获取实体bean所有声明的方法
 92             Method[] mds = clazz.getDeclaredMethods();
 93 
 94             for (Map<String, Object> data : dataList) {
 95 
 96                 // 迭代遍历map
 97                 Iterator<Entry<String, Object>> iterator = data.entrySet().iterator();
 98                 T obj = clazz.newInstance();
 99 
100                 // 根据map的键去找以set开头包含map的键的set方法,过程中下划线不影响查找方法,大小写不影响查找
101                 while (iterator.hasNext()) {
102                     Map.Entry<java.lang.String, java.lang.Object> entry = (Map.Entry<java.lang.String, java.lang.Object>) iterator
103                             .next();
104                     String key = entry.getKey();
105                     Object val = entry.getValue();
106 
107                     if (key == null || val == null) {
108                         continue;
109                     }
110 
111                     Method method = findSetMethod(key, mds);
112                     if (method == null) {
113                         continue;
114                     }
115                     // 反射调用字段的set方法
116 
117                     Class<?> t = method.getParameterTypes()[0];
118                     if (t == val.getClass()) {
119                         method.invoke(obj, val);
120                     } else if (Number.class.isAssignableFrom(t)) {
121                         if (t == Integer.class) {
122                             val = Integer.valueOf(val.toString());
123                         } else if (t == Long.class) {
124                             val = Long.valueOf(val.toString());
125                         } else if (t == Double.class) {
126                             val = Double.valueOf(val.toString());
127                         } else if (t == Float.class) {
128                             val = Float.valueOf(val.toString());
129                         } else {
130                             val = Integer.valueOf(val.toString());
131                             val = t.cast(val);
132                         }
133                     } else if (Date.class.isAssignableFrom(t)) {
134                         SimpleDateFormat[] sdf = new SimpleDateFormat[] { new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"),
135                                 new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS"),
136                                 new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSS"), new SimpleDateFormat("yyyy-MM-dd"),
137                                 new SimpleDateFormat("yyyy/MM/dd"), new SimpleDateFormat("HH:mm:ss"),
138                                 new SimpleDateFormat("HH:mm:ss.SSS"), };
139                         for (SimpleDateFormat simpleDateFormat : sdf) {
140                             try {
141                                 val = simpleDateFormat.format(val.toString());
142                             } catch (Exception e) {
143                                 continue;
144                             }
145                         }
146                     } else if (Boolean.class.isAssignableFrom(t)) {
147                         if (val.toString().equals("0") || val.toString().equalsIgnoreCase("false")) {
148                             val = false;
149                         } else {
150                             val = true;
151                         }
152                     }
153                 }
154                 lst.add(obj);
155             }
156         } catch (Exception e) {
157             e.printStackTrace();
158         }
159         return lst;
160     }
161 
162     private static Method findSetMethod(String key, Method[] methods) {
163 
164         String methodName = "";
165         String tmpKey = "";
166 
167         for (Method method : methods) {
168             methodName = method.getName().replace("_", "").toLowerCase().substring(3);
169             tmpKey = key.toLowerCase().replace("_", "");
170 
171             if (methodName.startsWith("set") && tmpKey.indexOf(methodName) > -1) {
172                 return method;
173             }
174         }
175         return null;
176     }
177 }
View Code
原文地址:https://www.cnblogs.com/swtjavaspace/p/9836997.html