Java 用于检索多行的 JDBCTemplate queryForMap

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24221187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 10:52:28  来源:igfitidea点击:

JDBCTemplate queryForMap for retrieving multiple rows

javaspringspring-3jdbctemplate

提问by user182944

Can I use queryForMapif there are multiple rows returned by the query.

queryForMap如果查询返回多行,我可以使用吗?

For a single row, the below code works fine.

对于单行,下面的代码工作正常。

public Map<String, Object> retrieveMultipleRowsColumns(String deptName){
    return jdbcTemplate.queryForMap("SELECT DEPT_ID,DEPT_NAME FROM DEPT WHERE DEPT_NAME = ?", deptName);
}

How to modify this for multiple rows?

如何为多行修改这个?

采纳答案by John Farrelly

Use queryForListsee the javadoc for full details. It returns List<Map<String,Object>>

queryForList有关完整的详细信息,参阅 javadoc。它返回List<Map<String,Object>>

public List<Map<String, Object>> retrieveMultipleRowsColumns(String deptName){
    return jdbcTemplate.queryForList("SELECT DEPT_ID,DEPT_NAME FROM DEPT WHERE DEPT_NAME = ?", deptName);
}

回答by suresh chaudhari

public <T> List<T> queryForList(String sql,
                            Class<T> elementType,
                            Object... args)
                 throws DataAccessException

Description copied from interface: JdbcOperations Query given SQL to create a prepared statement from SQL and a list of arguments to bind to the query, expecting a result list. The results will be mapped to a List (one entry for each row) of result objects, each of them matching the specified element type.

从接口复制的描述:JdbcOperations 查询给定 SQL 以从 SQL 创建准备好的语句和绑定到查询的参数列表,期望结果列表。结果将映射到结果对象的列表(每行一个条目),每个对象都匹配指定的元素类型。

Specified by:queryForListin interface JdbcOperations , Parameters:sql- SQL query to execute elementType - the required type of element in the result list (for example, Integer.class)args- arguments to bind to the query (leaving it to the PreparedStatement to guess the corresponding SQL type); may also contain SqlParameterValueobjects which indicate not only the argument value but also the SQL type and optionally the scale Returns:a List of objects that match the specified element type Throws: DataAccessException - if the query failsSee Also:JdbcOperations.queryForList(String, Class), SingleColumnRowMapper

指定者:queryForList在接口 JdbcOperations 中, Parameters:sql- 执行的 SQL 查询 elementType - 结果列表中所需元素的类型(例如,Integer.class)args- 绑定到查询的参数(将其留给 PreparedStatement 以猜测相应的 SQL 类型); 也可以contain SqlParameterValue对象不仅指示参数值,还指示 SQL 类型和可选的比例返回:匹配指定元素类型的对象列表抛出:DataAccessException - 如果查询失败另见:JdbcOperations.queryForList(String, Class),单列行映射器

回答by Brian Beech

I know this is really old, but there is a much simpler way to do this if you're looking for a map.

我知道这真的很旧,但是如果您正在寻找地图,有一种更简单的方法可以做到这一点。

Simply implement the ResultSetExtractor interface to define what type you want to return. Below is an example of how to use this. You'll be mapping it manually, but for a simple map, it should be straightforward.

只需实现 ResultSetExtractor 接口即可定义要返回的类型。下面是一个如何使用它的例子。您将手动映射它,但对于简单的地图,它应该很简单。

jdbcTemplate.query("select string1,string2 from table where x=1", new ResultSetExtractor<Map>(){
    @Override
    public Map extractData(ResultSet rs) throws SQLException,DataAccessException {
        HashMap<String,String> mapRet= new HashMap<String,String>();
        while(rs.next()){
            mapRet.put(rs.getString("string1"),rs.getString("string2"));
        }
        return mapRet;
    }
});

This will give you a return type of Map that has multiple rows (however many your query returned) and not a list of Maps. You can view the ResultSetExtractor docs here: http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/ResultSetExtractor.html

这将为您提供具有多行(无论您返回多少查询)的 Map 返回类型,而不是 Maps 列表。您可以在此处查看 ResultSetExtractor 文档:http: //docs.spring.io/spring/docs/current/javadoc-api/org/springframework/jdbc/core/ResultSetExtractor.html