利用反射把数据集合转换成List
2024-10-20 00:34:32
---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;
}
最新文章
- 解决 Oracle exp导出表数据时空表不能导出的问题
- KnockoutJS 3.X API 第四章 表单绑定(6) click绑定
- Django学习(二)
- Python 汉字转拼音库 pypinyin
- ubuntu 14.04 下FTP服务器的搭建--锁定用户目录,解决vsftpd: refusing to run with writable root inside chroot()
- Android Studio Error2
- ubuntu下MySQL安装配置及基本操作
- 一个简单的网页读字符串 SpeechLib
- SPOJ QTREE4 lct
- 方案猿身高project联赛,艺术家,相反,养殖场!-------三笔
- 使用travis-ci自动部署github上的项目
- USACO奶牛赛跑(逆序对)
- 【LintCode&#183;入门】斐波那契数列
- java查看程序执行时间
- ORACLE DB TRIGGER详解
- MySQL操作(备份很重要)
- Java 问题定位工具 ——jstack
- springboot 创建非web项目及数据源简单使用
- Qt Md5应用示例
- iOS.Debug.Simulator
热门文章
- 【测试】解决loadrunner11无法运行负载测试
- Bluetooth LE(低功耗蓝牙) - 第六部分(完)
- [LeetCode#202] Roman to Integer
- unity3d Human skin real time rendering with blood and water drop effect真实模拟人皮实时渲染之血液和水珠掉落效果
- eclipse配置j2ee项目
- zoj 3757 Alice and Bob and Cue Sports 月赛A 模拟
- 让nginx支持文件上传的几种模式
- Spring MVC之messageConverters
- 【Android - 进阶】之图片三级缓存的原理及实现
- 自己在安装centos 系统时, 是使用英文安装 成功,现在系统语言为英语,如何设置为中文?