问题
By default, queryForList() returns each for as a Map<String, Object>. The object can be plain String, java.sql.Timestamp, etc.
List<Map<String, Object>> result = jdbcTemplate.queryForList(sql, params);
Question: how can I enfore returning any values as String.class? So I'd be having a Map<String, String>.
I tried: jdbcTemplate.queryForList(sql, params, Map<String, String>.class)
But that statement seems to be invalid and does not compile.
回答1:
Maybe there is a better way, but the following works:
jdbcTemplate.query(sql, params, new ColumnMapRowMapper() {
@Override
protected Object getColumnValue(ResultSet rs, int index) {
return rs.getString(index);
}
}
来源:https://stackoverflow.com/questions/50042282/how-to-only-return-string-objects-from-jdbctemplate-queryforlist