我自己在执行此操作时发现的有趣事实...如果您使用NamedParameterJdbcTemplate,无论如何都会更好,默认情况下将确保列顺序。
例如,queryForList() 方法最终会创建一个 ColumnMapRowMapper():
@Override
public List<Map<String, Object>> queryForList(String sql, SqlParameterSource paramSource)
throws DataAccessException {
return query(sql, paramSource, new ColumnMapRowMapper());
}
在其实现中使用它:
/**
* Create a Map instance to be used as column map.
* <p>By default, a linked case-insensitive Map will be created.
* @param columnCount the column count, to be used as initial
* capacity for the Map
* @return the new Map instance
* @see org.springframework.util.LinkedCaseInsensitiveMap
*/
protected Map<String, Object> createColumnMap(int columnCount) {
return new LinkedCaseInsensitiveMap<>(columnCount);
}
链接的哈希映射将适当地保留排序。