Use of query, queryForObject, queryForList, and queryForMap methods in Jdbctemplate

     The growth of a programmer is actually very difficult. How should I put it, it means fighting monsters all the way to upgrade. When you encounter so many monsters that you can't fight them, you can't upgrade anymore; or in other words, it is a process of constantly entering and exiting pits. Once one or more pits cannot be jumped out, it is over.

    I have encountered basically the same problem twice recently at work regarding the use of methods in jdbctemplate. Once I encountered it myself, and once when a colleague asked me; md, it’s better to jump out of this pit as soon as possible. I can only study and summarize it carefully. .

    You may be familiar with these query, queryForObject, queryForList, and queryForMap methods in Jdbctemplate and have used them all. So what are the pitfalls?

 [query method] The return result is a list, and the elements in the list must be custom beans; they cannot be list<String>. As for why, I looked at the source code of this method several times but couldn't find the answer. I'd like some experts to give me some advice.

    So, have you ever encountered pitfalls when using query to execute select name from tabName; similar to sql? I have encountered it. The process is like this. I want to check whether a policy number has been frozen. If it has been frozen, take out the freezing date. I started to use queryForObject. As a result, some policies had not been frozen and could not find the record, and an exception was thrown. Well, I used query and the returned list<String>, the sql was written like this: select createDate as freezing time from tabName where cntrno=? and status='frozen' ; A problem arises, query cannot return list<String>.

 

 [queryForObject] Query out a record and encapsulate it into an object. What can be returned is String, Integer, Double or custom bean. But if the queried record is 0 or greater than 1, I'm sorry to throw an exception.

      The source code is as follows:

     

 

   

[queryForList] This method returns a list. This method is quite special. It can return List<String> or list<Map<String,Objec>>. why? Look at the source code:

    

 

     As can be seen from the source code, it depends on whether singleColumnRowMapper or ColumnMapRowMapper is called. So what is the difference between these 2? The former is

 Mapping a single column (field), which maps the result set to a map.

   In other words, your sql is select * fromtabName; this returns list<map>. If your sql is select name from tabName; or select max(score) from tabName;, the return is list<basic type>

 

 

  [queryForMap] Query a row of data and encapsulate it into a Map. An exception is thrown if multiple rows of records are queried .

     Source code analysis is as follows:

    

 

 

【Summarize】

     //1. Query a row of data and return an int result  
    jdbcTemplate.queryForInt("select count(*) from test");  


   //2.  Query a row of data and convert the row of data into a Map and return  
   jdbcTemplate.queryForMap("select * from test where name='name5'");  


   //3. Query a row of any type of data . The last parameter specifies the return result type  
   jdbcTemplate.queryForObject("select count(*) from test", Integer.class);  


   //4. Query a batch of data . By default, each row of data is converted into Map       
   jdbcTemplate.queryForList("select * from test");  


     //5. Query only one column of data list , the column type is String type, and the column name is name  
      jdbcTemplate.queryForList("  
        select name from test where name=?", new Object[]{"name5"}, String.class);  


      //6. Query a batch of data and return it as SqlRowSet, which is similar to ResultSet, but is no longer bound to the connection.  
      SqlRowSet rs = jdbcTemplate.queryForRowSet("select * from test");     

 

 

 

Guess you like

Origin blog.csdn.net/dhklsl/article/details/103510888