zoukankan      html  css  js  c++  java
  • 利用反射把数据集合转换成List

    ---ResultSet数据集

        public static List toList(ResultSet rs, Class cls) {
            List list = new ArrayList();
            try {
    
                BeanInfo beanInfo = Introspector.getBeanInfo(cls); // 获取类属性
    
                // 给 JavaBean 对象的属性赋值
                PropertyDescriptor[] propertyDescriptors = beanInfo
                        .getPropertyDescriptors();
                ResultSetMetaData meta = rs.getMetaData();
    
                Object obj = null;
                while (rs.next()) {
    
                    obj = cls.newInstance(); // 创建 JavaBean 对象
    
                    for (int j = 1; j <= meta.getColumnCount(); j++) {
    
                        for (int i = 0; i < propertyDescriptors.length; i++) {
                            PropertyDescriptor descriptor = propertyDescriptors[i];
                            String propertyName = descriptor.getName();
    
                            String propertyType = descriptor.getPropertyType()
                                    .getName();
    
                            if (meta.getColumnName(j)
                                    .equalsIgnoreCase(propertyName)) {
    
                                Method method = descriptor.getWriteMethod();
                                Object value = rs.getObject(j);
                                if (propertyType.equals("java.lang.String")
                                        && value == null) {
                                    method.invoke(obj, "");
                                } else if (propertyType.equals("java.lang.String")) {
                                    method.invoke(obj,  value.toString());
                                } else if (propertyType.equals("java.util.Date")) {
                                    method.invoke(obj, (Date) value);
                                } else if (propertyType.equals("java.lang.Integer")) {
                                    method.invoke(obj, new Integer((String) value));
                                } else if (propertyType.equals("float")) {
                                    method.invoke(obj,
                                            Float.parseFloat((String) value));
                                }else if(propertyType.equals("java.math.BigDecimal"))
                                {
                                    method.invoke(obj,(BigDecimal)value);
                                }
                                else {
                                    method.invoke(obj, value);
                                }
                                break;
                            }
                        }
                    }
                    list.add(obj);
                }
            } catch (Exception ex) {
                ex.printStackTrace();
            } finally {
                return list;
            }
        }
    }

    ---Map数据集

    public static Object convertMap(Class type, Map map) {
    Object obj = null;
    try {
    
    BeanInfo beanInfo = Introspector.getBeanInfo(type); // 获取类属性
    obj = type.newInstance(); // 创建 JavaBean 对象
    
    // 给 JavaBean 对象的属性赋值
    PropertyDescriptor[] propertyDescriptors = beanInfo
    .getPropertyDescriptors();
    // 获取key的集合
    Set<String> keySet = map.keySet();// 获取mapKEY
    
    for (int i = 0; i < propertyDescriptors.length; i++) {
    PropertyDescriptor descriptor = propertyDescriptors[i];
    String propertyName = descriptor.getName();
    String strPropertyName = propertyName.toLowerCase();// 大写转小写
    String propertyType=descriptor.getPropertyType().getName();
    // 遍历key集合,获取value
    for (String key : keySet) {
    
    String strKey = key.toLowerCase();
    
    if (strPropertyName.equals(strKey)) {// 对比key与属性是否相同
    // 下面一句可以 try 起来,这样当一个属性赋值失败的时候就不会影响其他属性赋值。
    Object value = map.get(key);
    
    /* Object[] args = new Object[1];
    args[0] = value;*/
    
    Method method= descriptor.getWriteMethod();
    if(propertyType.equals("java.lang.String")){ 
    method.invoke(obj,value.toString()); 
    } 
    else if(propertyType.equals("java.util.Date")){ 
    method.invoke(obj, (Date)value); 
    } 
    else if(propertyType.equals("java.lang.Integer")){ 
    method.invoke(obj, new Integer((String)value)); 
    }
    else if(propertyType.equals("float")){ 
    method.invoke(obj, Float.parseFloat((String)value)); 
    } 
    else{ 
    method.invoke(obj, value);
    } 
    break;
    }
    }
    }
    } catch (Exception e) {
    // TODO: handle exception
    logger.error("map转换对象错误", e);
    }
    
    return obj;
    }
    
     
  • 相关阅读:
    求最大公约数
    计算变量
    JavaScript高级浏览器原理V8引擎js执行原理
    JavaScript小游戏按下按钮移动方块
    黑马程序员WPF中的DoubleAnimation
    黑马程序员浅谈partial class的理解
    黑马程序员C#与Javascript变量、函数之间的相互调用
    黑马程序员关于工作流的模式
    黑马程序员C#解析HTML
    黑马程序员关于System.Collections空间
  • 原文地址:https://www.cnblogs.com/dashi/p/4260392.html
Copyright © 2011-2022 走看看