zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    js设置滚动条定位到所属容器的最底部
    js如何获取前后连续n天的时间
    vue实现点击区域外部的区域,关闭该区域
    使用typed.js实现页面上的写字功能
    Python3基础 函数 多值参数 元组与字典形式(键值对分别指出)
    Python3基础 函数 返回值 利用元组返回多个值
    Python3基础 变量命名 区分大小写
    Python3基础 函数 参数为list可变类型时,使用append会影响到外部实参
    Python3基础 函数 多值参数 元组与字典形式(使用星号对列表与字典进行拆包)
    Python3基础 函数 参数 多个参数都有缺省值,需要指定参数进行赋值
  • 原文地址:https://www.cnblogs.com/swtjavaspace/p/9836997.html
Copyright © 2011-2022 走看看