Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 确保jdbctemplate中返回的列的顺序_Java_Spring_Jdbctemplate - Fatal编程技术网

Java 确保jdbctemplate中返回的列的顺序

Java 确保jdbctemplate中返回的列的顺序,java,spring,jdbctemplate,Java,Spring,Jdbctemplate,我正在尝试使用JDBCTemplate检索给定查询的映射列表。以下方法似乎适合我的用例: public List<Map<String,Object>> queryForList(String sql) throws DataAccessException 公共列表查询列表(字符串sql) 抛出DataAccessException 但是,我想确保对于给定的行,映射的顺序是有保证的。i、 例

我正在尝试使用JDBCTemplate检索给定查询的映射列表。以下方法似乎适合我的用例:

public List<Map<String,Object>> queryForList(String sql)
                                      throws DataAccessException
公共列表查询列表(字符串sql)
抛出DataAccessException


但是,我想确保对于给定的行,映射的顺序是有保证的。i、 例如,LinkedHashMap会更好,因为我想保留列的顺序。有没有更好的方法使用JDBCTemplate实现这一点?

我认为可以使用自定义ColumnMapRowMapper为每行创建LinkedHashMap,然后将其传递给查询方法:

RowMapper RowMapper=newcolumnmapprowmapper()
{
受保护映射createColumnMap(int columnCount)
{
返回新的LinkedHashMap(columnCount);
}
};
JdbcTemplate JdbcTemplate=新JdbcTemplate();
字符串sql=“选择…”;
尝试
{           
List results=jdbcTemplate.query(sql,行映射器);
// ...
}
捕获(数据访问异常)
{
e、 printStackTrace();
}

我自己做这件事时发现了一个有趣的事实。。。如果使用
NamedParameterJdbcTemplate
,则默认情况下会确保列顺序

例如,queryForList()方法最终创建ColumnMapRowMapper():

@覆盖
公共列表查询列表(字符串sql,SqlParameterSource paramSource)
抛出DataAccessException{
返回查询(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);
    }
/**
*创建要用作列映射的映射实例。
*默认情况下,将创建链接的不区分大小写的映射。
*@param columnCount列计数,用作初始值
*地图容量
*@返回新的地图实例
*@see org.springframework.util.LinkedCaseInsensitiveMap
*/
受保护映射createColumnMap(int columnCount){
返回新的LinkedCaseInsensitiveMap(columnCount);
}
链接的哈希映射将适当地保留顺序

@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);
    }