HasorDB ResultSetExtractor(結果集處理)

2021-12-30 11:24 更新

?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}


以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號