我有一个查询,它提供以下输出:
State CST QTY
aa 0 10
aa 1 20
aa 2 30
aa 3 40
ac 0 10
ac 1 20
ac 2 30
ac 3 40
我在 spring mvc 中使用 jdbc 模板:在实现 RowMapper 时,我很困惑需要使用哪种数据结构以及如何映射每一行以获得以下预期 O/P 的输出:
STATE CST0 CST CST2 CST3
aa 10 20 30 40
ac 10 20 30 40
谁能帮帮我吗?
最佳答案
如果您使用的是 Google Guava 库,则可以使用名为 Multimap 的新集合。 。多重映射从唯一键映射到值集合:Map<K, List<V>>
或Map<K, Set<V>>
。
而不是使用 RowMapper
,我宁愿使用接口(interface) RowCallbackHandler
这是一个类似的处理结果集的接口(interface):
public class MyRowMapper implements RowCallbackHandler{
private Multimap<String, Integer> stateByQuantity = new ArrayListMultimap();
void processRow(ResultSet rs) throws SQLException {
String state = rs.getString("State");
Integer qty = rs.getInt("QTY");
stateByQuantity.put(state, qty);
}
}
关于java - 如何在jdbc模板中实现RowMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38140942/