利用反射把数据集合转换成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;
}

 

 

转载于:https://www.cnblogs.com/dashi/p/4260392.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值