java:将 DynaBean (apache-commons-beanutils) 转换为列表

发布于 2024-10-10 05:28:57 字数 131 浏览 0 评论 0原文

我使用 apache-commons-beanutils DynaBean 类从数据库中获取行并在 mysql 函数之外处理它们。

有没有办法将 DynaBean 转换为列表,而无需迭代每一行并手动创建列表?

谢谢!

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 list ?

thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半仙 2024-10-17 05:28:57

到目前为止我没有得到任何答案,所以我编写了一个函数来迭代行并创建一个 HashMap 类型(字符串,对象)的 ArrayList。

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;
}

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文