【问题标题】:java: converting DynaBean (apache-commons-beanutils) to Listjava: 将 DynaBean (apache-commons-beanutils) 转换为 List
【发布时间】:2010-12-30 15:55:43
【问题描述】:

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

有没有一种方法可以将 DynaBean 转换为 List 而无需遍历每一行并手动创建列表?

谢谢!

【问题讨论】:

    标签: java list apache-commons-beanutils


    【解决方案1】:

    到目前为止,我没有得到任何答案,所以我编写了遍历行并创建 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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-11-14
      • 2014-02-02
      • 2017-06-14
      • 1970-01-01
      • 1970-01-01
      • 2018-01-02
      • 2014-03-21
      • 1970-01-01
      相关资源
      最近更新 更多