利用反射把数据集合转换成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;
}
利用反射把数据集合转换成List的更多相关文章
- C# 利用反射动态将字符串转换成属性对应的类型值
/// <summary> /// 为指定对象分配参数 /// </summary> /// <typeparam name="T">对象类型& ...
- C#中利用LINQ to XML与反射把任意类型的泛型集合转换成XML格式字符串
在工作中,如果需要跟XML打交道,难免会遇到需要把一个类型集合转换成XML格式的情况.之前的方法比较笨拙,需要给不同的类型,各自写一个转换的函数.但是后来接触反射后,就知道可以利用反射去读取一个类型的 ...
- 利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model
利用泛型和反射,管理配置文件,把Model转换成数据行,并把数据行转换成Model 使用场景:网站配置项目,为了便于管理,网站有几个Model类来管理配置文件, 比如ConfigWebsiteMo ...
- JAVA 利用反射自定义数据层框架
之前的随笔一直都在介绍c#,主要公司最近的业务都是做桌面程序,那么目前c#中的WPF肯定是我做桌面程序的不二之选,做了半年的WPF,也基本摸清了c#写代码的套路和规则(本人之前是两年多的JAVA开发者 ...
- C#中把任意类型的泛型集合转换成SQLXML数据格式的小例子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 将java类的泛型集合转换成json对象
一般用extjs开发传输都是用json比较多,这个将来大家也许会用到... ConvertJsonUtils.java package com.sunweb.util.jsonfactory; imp ...
- C#中对象,字符串,dataTable、DataReader、DataSet,对象集合转换成Json字符串方法。
C#中对象,字符串,dataTable.DataReader.DataSet,对象集合转换成Json字符串方法. public class ConvertJson { #region 私有方法 /// ...
- Atitit利用反射获取子类 集合 以及继承树
Atitit利用反射获取子类 集合 以及继承树 想从父类往下找子类的确是不可能的,要知道只要类不是final的话谁都有继承它的自由不需要事前通知父类. Eclipse实现不是重父类开始找而是重子类往回 ...
- 在一般处理程序中,把Form Post过来的表单集合转换成对象 ,仿 MVC post,反射原理
using System; using System.Collections.Generic; using System.Collections.Specialized; using System.L ...
随机推荐
- Hibernate 缓存 关于注解方式
要引入 import org.hibernate.annotations.Cache; 在类前面添加: @Cache(usage= CacheConcurrencyStrategy.NONSTRICT ...
- 【POJ11855】 Buzzwords (后缀数组)
Description The word “the” is the most commonthree-letter word. It evenshows up inside other words, ...
- mysql之索引方面的知识点总结
索引的类型: 普通索引:这是最基本的索引类型,没唯一性之类的限制. 唯一性索引:和普通索引基本相同,但所有的索引列只能出现一次,保持唯一性. 主键:主键是一种唯一索引,但必须指定为"PRIM ...
- lc面试准备:Invert Binary Tree
1 题目 Invert a binary tree. 4 / \ 2 7 / \ / \ 1 3 6 9 to 4 / \ 7 2 / \ / \ 9 6 3 1 接口: public TreeNod ...
- 14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用
14.8.2 Role of the .frm File for InnoDB Tables InnoDB 表得到 .frm文件的作用 Vsftp:/data01/mysql/zjzc# ls -lt ...
- 使用Redis作为消息队列服务场景应用案例
一.消息队列场景简介 "消息"是在两台计算机间传送的数据单位.消息可以非常简单,例如只包含文本字符串:也可以更复杂,可能包含嵌入对象.消息被发送到队列中,"消息队列&qu ...
- Selenium 前期学习
一.了解selenium必读文档: 官方文档:http://docs.seleniumhq.org/docs/03_webdriver.jsp 二.公司使用c#开发,配合开发的要求,使用visual ...
- php获取客户端ip get_client_ip()
php获取客户端ip get_client_ip() function get_client_ip(){if (getenv("HTTP_CLIENT_IP") && ...
- Mathlab编程-微积分在Matlab中的解法
这一章节将介绍一系列典型的微积分问题(求极限.级数.定积分.导数.重积分等)在Matlab中的求解. 首先关于极限: (1) 数列极限: 给出下面三段例程. 求解数列极限的limit函数参数说明 ...
- Matlab编程-图形处理功能
绘图功能最基本的命令行:plot(y). 二维图形: (1) >> y=rand(100,1); >> plot(y) y是随机的实向量,以生成y的索引为横坐标,y为纵坐标绘图 ...