开发者

java: converting DynaBean (apache-commons-beanutils) to List

I use apache-commons-beanutils DynaBean class in order to fetch rows from a database and to handle them outside of the mysql function.

is there a way to convert a DynaBean to a List without iterating through each row and manually creating the l开发者_JAVA百科ist ?

thanks!


so far I didn't get any answers so I wrote the function that iterates through the rows and creates an ArrayList of HashMap type (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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜