How to only return String objects from JdbcTemplat

2019-03-03 11:10发布

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条回答
贼婆χ
2楼-- · 2019-03-03 11:55

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);
    }
}
查看更多
登录 后发表回答