How exactly do the queryForMap () and queryForList () methods provided by Spring JdbcTemplate work internally?

I am studying for Spring Core certification and I have some doubts about using JdbcTemplate .

I know that JdbcTemplate gives me different methods for making queries (which are written in SQL).

So, I have the following methods:

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 the SQL statement and the class type of the returned object respectively . In the previous case, it was specified that the result of the query is returned in a Long object .

What happens if the result returned from the query is not of the specified type? (for example, if the query returns a String and I set the Long as parameter of the queryForObject () method? How are these situations handled?

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 only used when my query is known to return a single string . Is it correct?

So now I am in doubt about using the queryForMap () method .

I know that the Map interface stores objects using the system...

Thus, it is believed that the Card stores several pairs... So I think this can store multiple rows: the map key is the row key of my table, and the value contains the values ​​of the other columns (into an object).

But it looks like the logic of the queryForMap () method is quite different. How it works?

It may be returning a map that works like this:

key : contains the key of the primary key of the table and has several associated values ​​associated with the contents of other fields in my table. Or what?

3) queryForList () :

public List getAllPersonInfo() {
    String sql = "select * from PERSON";
    return jdbcTemplate.queryForList(sql);
}

      

Reading the documentation, it seems to me that it is being used when I expect my query to return multiple rows . And it seems to me that the output of the method is a List of Maps (so according to the previous method, each Map is a single returned string ** (but previous doubts about how the string is stored in the Map is stored).

Can you help me deeply understand exactly how these methods work?

Tpx

+3


source to share


2 answers


  • You will get an exception. You can just write a test to validate it.
  • The documentation for the method says: the request will be a single row request, so yes, the request should return one row. It also says: Returns: The result of a Map (one entry for each column, using the column name as the key), which should clear your doubts and refute your wrong guess.
  • The documentation states that the results will be matched against a list (one entry for each row) of Maps (one entry for each column using the column name as the key), which again should be enough to clear your doubts and disprove your wrong assumption.


+9


source


To get a list of lines containing the map in a line:



List<Map<String, Object>> rsMapList = jdbcTemplate.queryForList(sql, params);

      

0


source







All Articles