?ResultSetExtractor
?負責處理整個結果集,通常和 ?RowMapper
?配合使用,或者實現對結果集的更復雜處理。
舉一個例子查詢所有用戶,并且構建一個用戶 ID 和 名字 的 Map 映射
String queryString = "select * from test_user";
ResultSetExtractor extractor = new ResultSetExtractor<Map<Integer, String>>() {
public Map<Integer, String> extractData(ResultSet rs) throws SQLException {
Map<Integer, String> hashMap = new HashMap<>();
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
hashMap.put(id, name);
}
return hashMap;
}
};
Map<Integer, String> result = jdbcTemplate.query(queryString, extractor);
執(zhí)行結果為
{1=mali, 2=dative, 3=jon wes, 4=mary, 5=matt}
更多建議: