最新下载
热门教程
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
ORM小工具
时间:2022-07-02 17:42:05 编辑:袖梨 来源:一聚教程网
ORM小工具
代码:
package org.blueidea.tools;
import java.lang.reflect.Field;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.BeanUtils;
/**
* @author Administrator
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
* ResultSetMetaData 可用于获取关于 ResultSet 对象中列的类型和属性信息的对象
* ResultSet.getMetaData() 检索此 ResultSet 对象的列的编号、类型和属性
*/
public class DTOPopulator {
public static List populate(ResultSet rs,Class clazz) throws Exception{
ResultSetMetaData metaData = rs.getMetaData();
int colCount = metaData.getColumnCount();
List ret = new ArrayList();
Field[] fields = clazz.getDeclaredFields();
while(rs.next()){
Object newInstance = clazz.newInstance();
for(int i=1;i<=colCount;i++){
try{
Object value = rs.getObject(i);
for(int j=0;j
if(f.getName().equalsIgnoreCase(metaData.getColumnName(i).replaceAll("_","")))
{
BeanUtils.copyProperty(newInstance,f.getName(),value);
}
}
}catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
ret.add(newInstance);
}
return ret;
}
}
相关文章
- PS怎么设计抽象错乱的人物效果? 07-12
- 剑星阿妮斯服装怎么获取 阿妮斯服装获取方式一览 07-12
- 网站优化如何提高用户的信任度? 07-12
- 比特币Coinbase溢价回升,但强度仍低于6月峰值——这对BTC意味着什么? 07-12
- 无畏契约源能行动霓虹实战 霓虹实战技巧详解 07-12
- 幻兽帕鲁流沙蛇去哪捕捉 帕鲁流沙蛇抓取方法介绍 07-12