我有jdbcTemplate
在List<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
仅返回了最后一条记录,请提供建议
答案 0 :(得分:1)
首先创建列表实例:
List<Collection<Object>> targetList = new ArrayList<>();
然后在迭代过程中填写列表:
for (Map<String, Object> employee : employees) {
targetList.add(employee.values());
}
看到区别:add()
呼叫而不是=
分配。
因此,最后您将拥有List<Collection<Object>>
。