---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;
}

利用反射把数据集合转换成List的更多相关文章

  1. C# 利用反射动态将字符串转换成属性对应的类型值

    /// <summary> /// 为指定对象分配参数 /// </summary> /// <typeparam name="T">对象类型& ...

  2. C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串

    在工作中,如果需要跟XML打交道,难免会遇到需要把一个类型集合转换成XML格式的情况.之前的方法比较笨拙,需要给不同的类型,各自写一个转换的函数.但是后来接触反射后,就知道可以利用反射去读取一个类型的 ...

  3. 利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model

    利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model   使用场景:网站配置项目,为了便于管理,网站有几个Model类来管理配置文件, 比如ConfigWebsiteMo ...

  4. JAVA 利用反射自定义数据层框架

    之前的随笔一直都在介绍c#,主要公司最近的业务都是做桌面程序,那么目前c#中的WPF肯定是我做桌面程序的不二之选,做了半年的WPF,也基本摸清了c#写代码的套路和规则(本人之前是两年多的JAVA开发者 ...

  5. C#中把任意类型的泛型集合转换成SQLXML数据格式的小例子

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  6. 将java类的泛型集合转换成json对象

    一般用extjs开发传输都是用json比较多,这个将来大家也许会用到... ConvertJsonUtils.java package com.sunweb.util.jsonfactory; imp ...

  7. C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。

    C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...

  8. Atitit利用反射获取子类 集合 以及继承树

    Atitit利用反射获取子类 集合 以及继承树 想从父类往下找子类的确是不可能的,要知道只要类不是final的话谁都有继承它的自由不需要事前通知父类. Eclipse实现不是重父类开始找而是重子类往回 ...

  9. 在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理

    using System; using System.Collections.Generic; using System.Collections.Specialized; using System.L ...

随机推荐

  1. 如何为企业选择最理想的Linux服务器系统?

    [2013年10月12日 51CTO外电头条]什么样的Linux服务器最合适您的企业?简言之,它需要为员工带来工作所需的理想支持效果. 相对于成百上千种Linux桌面系统,Linux服务器系统的数量其 ...

  2. 【UVA1416】(LA4080) Warfare And Logistics (单源最短路)

    题目: Sample Input4 6 10001 3 21 4 42 1 32 3 33 4 14 2 2Sample Output28 38 题意: 给出n个节点m条无向边的图,每条边权都为正.令 ...

  3. jsp的url后跟中文参数传参出现乱码

    ①重新编码:String urlParam= request.getParameter("urlParam");  urlParam= new String(urlParam.ge ...

  4. configure: error: cannot find protoc, the Protocol Buffers compiler

    centos 6 安装mosh 1.2 2012-05-07 17:21:41标签:centos mosh 关于mosh(引用于) 芬兰研究员Tatu Ylönen于1995年设计出最早的SSH协议, ...

  5. HDU 5912 Fraction 【模拟】 (2016中国大学生程序设计竞赛(长春))

    Fraction Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  6. codeforces -- 283A

    A. Cows and Sequence time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  7. Win32中常用消息

    一 .WM_PAINT消息 1 WM_PAINT的产生   由于窗口的互相覆盖等,产生需要绘制的区域,那么会产生WM_PAINT消息. 一般情况下,不直接发送WM_PAINT消息,通过API声明需要绘 ...

  8. unity3d 制造自己的水体water effect(一)

    first,I wish you a happy new year, and study in spring festival’s eve means you are hardworking,haha ...

  9. mac上的键盘生活——打字训练

    我的打字真的很有问题,错误率实在是太高了,于是乎下定决心改掉打字习惯,起码为了对得起我的机械键盘   所谓双击键(shift)就是当一个键钮上有两个字符时的辅助选择键,像标注在数字1上的 !,就是在你 ...

  10. Google辅助类软件

    本博文的主要内容有 .Google辅助类软件的介绍 .重点首推!  Google软件精选管理器 1.Google辅助类软件的介绍 1.  Google软件精选管理器的下载和安装使用 2.  Googl ...