利用反射把数据集合转换成List
2015-01-29 17:07
471 查看
---ResultSet数据集
---Map数据集
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; }
相关文章推荐
- 通过反射,将datatable转换为List集合(反射读取实体类的属性,并赋值),通过接口来实现MySql和MsSql数据的切换(二层反射),静态构造函数,抽象类和接口的区别
- Java利用反射向集合(List或Map)中存入任意类型的数据(绕过编译阶段)
- 利用泛型和反射,将从数据库select出来的集合直接转换成对象
- 利用反射将Datatable、SqlDataReader转换成List模型
- 将泛型集合类(List<T>)转换成DataTable,返回的DataTable所有列的数据类型为string型,且列顺序与字符串提供的字段顺序一致
- java获取list集合转换成json数据
- 将List 集合 转换为Jsion 数据
- DataTable转换list泛型集合(反射)
- c# list集合转换成json数据
- android利用SharedPreferences保存list集合数据
- java 利用反射 从数据库取出数据对象list 类似hibernate
- 利用反射将Datatable、SqlDataReader转换成List模型
- java中利用jar包将list,对象等转换为json数据格式
- 利用java反射把ResultSet转换成List
- object对象java 利用反射 从数据库取出数据对象list 类似hibernate
- 如何将一个List<Map<String,String>>类型的集合数据转换成json格式输出
- VA利用反射进行数据库的读取免数据类型转换
- java中List集合数据灵活转换
- 将list集合的数据转换为json字符串需要注意的问题
- 利用泛型和反射对Java对象集合转换成Json的工具类