java Spring JdbcTemplate 提供的 queryForMap() 和 queryForList() 方法在内部究竟是如何工作的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27471993/
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
How exactly does queryForMap() and queryForList() methods provided by the Spring JdbcTemplate work internally?
提问by JB Nizet
I am studying for the Spring Core certification and I have some doubt related to the use of the JdbcTemplateuse.
我正在学习Spring Core认证,对JdbcTemplate的使用有一些疑问。
I know that JdbcTemplate gives me different methods to perform queries (that are written in SQL).
我知道 JdbcTemplate 为我提供了不同的方法来执行查询(用 SQL 编写)。
So I have the following methods:
所以我有以下方法:
1) queryForObject():
1) queryForObject():
public long getPersonCount() {
String sql = “select count(*) from PERSON”;
return jdbcTemplate.queryForObject(sql, Long.class);
}
So in this method I specify 2 parameters that represent respectively the SQL statement and the classtype of the returned object. In the previous case it is specified that the result of the query is returned into a Longobject.
所以在这个方法中我指定了 2 个参数,分别代表 SQL 语句和返回对象的类类型。在前一种情况下,指定查询结果返回到Long对象中。
What happens if the result returned from the query doesn't match with the specified type? (for example if the query returns a String and I have specified a Long as parameter of the queryForObject() method? How are situations like this handled?
如果查询返回的结果与指定的类型不匹配会发生什么?(例如,如果查询返回一个 String 并且我指定了一个 Long 作为 queryForObject() 方法的参数?如何处理这样的情况?
2) queryForMap():
2) queryForMap():
public Map getPersonInfo(int id) {
String sql = “select * from PERSON where id=?”;
return jdbcTemplate.queryForMap(sql, id);
}
Reading the documentation it seems to me that this is used only when it is known that my query returns a single row. Is it correct?
阅读文档在我看来,这仅在知道我的查询返回单行时使用。这是正确的吗?
So now I have a doubt related the use of the queryForMap()method.
所以现在我对queryForMap()方法的使用有疑问。
I know that the Mapinterface stores objects using system.
我知道Map接口使用存储对象系统。
So a Mapis thought to store multiple couples. So I think that this could store multiple rows: the key of the Map is the key of a row of my table and the value contains the values of the other columns (into an object).
所以一个Map被认为可以存储多个夫妇。所以我认为这可以存储多行: Map 的键是我表中一行的键,该值包含其他列的值(到一个对象中)。
But it seems that the logic of the queryForMap()method is pretty different. How does it work exactly?
但似乎queryForMap()方法的逻辑非常不同。它是如何工作的?
Maybe it returns a Map that works in this way:
也许它会返回一个以这种方式工作的 Map:
key: contains the key of the primary key of the table and with this key are associated multiple values related to the content of the other fields of my table. Or what?
key:包含表的主键的键,与此键相关联的多个值与我的表的其他字段的内容相关。要不然是啥?
3) queryForList():
3) queryForList():
public List getAllPersonInfo() {
String sql = “select * from PERSON”;
return jdbcTemplate.queryForList(sql);
}
Reading the documentation it seems to me that it is used when I expect that my query returns multiple rows. And it seems to me that the method output is a List of Map(so according to the previous method each Map represents a single returned row** (but the previous doubt about how a row is stored into a Map persists).
阅读文档在我看来,当我期望我的查询返回多行时使用它。在我看来,该方法的输出是一个Map 列表(所以根据之前的方法,每个 Map 代表一个返回的行**(但之前关于如何将行存储到 Map 中的疑问仍然存在)。
Can you help me to understand deeply how these methods exactly work?
你能帮我深入理解这些方法究竟是如何工作的吗?
Tnx
田纳西州
回答by JB Nizet
- You'll get an exception. You could simply write a test to confirm it.
- The documentation of the method says: The query is expected to be a single row query, so yes, the query is supposed to return a single row. It also says: Returns: the result Map (one entry for each column, using the column name as the key), which should clear your doubts and refute your wrong assumption.
- The documentation says: The results will be mapped to a List (one entry for each row) of Maps (one entry for each column using the column name as the key), which, once again should be enough to clear your doubts and refute your wrong assumption.
- 你会得到一个例外。您可以简单地编写一个测试来确认它。
- 该方法的文档说:查询应该是单行查询,所以是的,查询应该返回单行。它还说:返回:结果映射(每列一个条目,使用列名作为键),这应该清除您的疑虑并驳斥您的错误假设。
- 文档说:结果将映射到 Maps 的列表(每行一个条目)(使用列名作为键的每列一个条目),这再次应该足以消除您的疑虑并反驳您的错误的假设。
回答by Taras Melnyk
To get list of rows that contains a Map per row:
要获取每行包含一个 Map 的行列表:
List<Map<String, Object>> rsMapList = jdbcTemplate.queryForList(sql, params);