列表内的地图,想要检索所有地图并将其值存储到另一个列表

时间:2018-10-12 07:39:40

标签: java spring

我有jdbcTemplateList<Map<k,V>>中返回的记录,我只想获取Map<K,V>,然后我想在另一个List中添加V并将该列表返回给控制器并显示到jsp中

    public List<Object> searchByempCodeV2(String tabSuffix, String empCode, String yyyyMm) {
    List<Object> targetList = null;
    MapSqlParameterSource param = new MapSqlParameterSource();
    String tableName = "Salary_detail_report_082018";
    String query = "SELECT "
            + " DISTINCT PAY_CODE, "
            + " PAY_CODE_DESC, "
            + " AMOUNT, "
            + " row_number() over (Order by EMP_CODE ) AS ROW_NUM "
            + " FROM " + tableName
            + " WHERE EMP_CODE=" + empCode
            + " AND YYYYMM=" + yyyyMm
            + " AND PAY_CODE NOT IN (997,998,999) "
            + " ORDER BY PAY_CODE ASC ";
    List<Map<String, Object>> employees = queryForList(query);
    if (employees != null && !employees.isEmpty()) {
        for (Map<String, Object> employee : employees) {
            targetList = new ArrayList<>(employee.values());

        }
    }
    return targetList;
}

}

现在我的targetList仅返回了最后一条记录,请提供建议

1 个答案:

答案 0 :(得分:1)

首先创建列表实例:

List<Collection<Object>> targetList = new ArrayList<>();

然后在迭代过程中填写列表:

    for (Map<String, Object> employee : employees) {
        targetList.add(employee.values());
    }

看到区别:add()呼叫而不是=分配。

因此,最后您将拥有List<Collection<Object>>