我使用apache-commons-beanutils DynaBean类从数据库获取行,并在mysql函数外部处理它们。
有没有一种方法可以将DynaBean转换为列表,而不需要遍历每一行并手动创建列表?
谢谢!
发布于 2010-12-31 00:55:53
到目前为止,我还没有得到任何答案,所以我编写了一个函数来迭代这些行,并创建一个HashMap类型的ArrayList (String,Object)。
public ArrayList<HashMap<String,Object>> convertDynaBeanListToArrayList(List<DynaBean> theList) {
ArrayList<HashMap<String,Object>> result = new ArrayList<HashMap<String,Object>>();
DynaProperty[] dynaProperties = null;
for (Integer i=0;i<theList.size();i++) {
DynaBean row = theList.get(i);
HashMap<String,Object> resultRow=new HashMap<String,Object>();
// each raw got the same column names, no need to fetch this for every line
if (dynaProperties == null) {
dynaProperties = row.getDynaClass().getDynaProperties();
}
for (Integer j=0;j<dynaProperties.length;j++) {
String columnName=dynaProperties[j].getName();
resultRow.put(columnName, row.get(columnName));
}
result.add(resultRow);
}
return result;
}https://stackoverflow.com/questions/4564069
复制相似问题