【jdbcTemplate】使用jdbcTemplate查詢的三種回調


用於查詢的回調接口定義主要有以下三種:

  • org.springframework.jdbc.core.ResultSetExtractor.  基本上屬於JdbcTemplate內部使用的Callback接口,相對於下面兩個Callback接口來說,ResultSetExtractor擁有更多的控制權,因為使用它,你需要自行處理ResultSet:

        
        
        
                
          
          
          
                  
    1. public interface ResultSetExtractor { 
    2. Object extractData(ResultSet rs) throws SQLException, DataAccessException;
    3. }

    在直接處理完ResultSet之后,你可以將處理后的結果以任何你想要的形式包裝后返回。


  • org.springframework.jdbc.core.RowCallbackHandler.  RowCallbackHandler相對於ResultSetExtractor來說,僅僅關注單行結果的處理,處理后的結果可以根據需要存放到當前RowCallbackHandler對象內或者使用JdbcTemplate的程序上下文中,當然,這個完全是看個人愛好了。 RowCallbackHandler的定義如下:

        
        
        
                
          
          
          
                  
    1. public interface RowCallbackHandler {
    2. void processRow(ResultSet rs) throws SQLException;
    3. }



  • org.springframework.jdbc.core.RowMapper.  ResultSetExtractor的精簡版,功能類似於RowCallbackHandler,也只關注處理單行的結果,不過,處理后的結果會由ResultSetExtractor實現類進行組合。 RowMapper的接口定義如下:

        
        
        
                
          
          
          
                  
    1. public interface RowMapper {
    2. Object mapRow(ResultSet rs, int rowNum) throws SQLException;
    3. }


為了說明這三種Callback接口的使用和相互之間的區別,我們暫且設定如下場景:

數據庫表customer中存在多行信息,對該表查詢后,我們需要將每一行的顧客信息都映射到域對象Customer中,並以java.util.List的形式返回所有的查詢結果。

現在,我們分別使用這三種Callback接口對customer表進行查詢:


ResultSetExtractor

 
 
 
         
   
   
   
           
  1. List customerList = (List)jdbcTemplate.query("select * from customer", 
  2. new ResultSetExtractor(){
  3. public Object extractData(ResultSet rs) throws SQLException,DataAccessException { 
  4. List customers = new ArrayList();
  5. while(rs.next()) { 
  6. Customer customer = new Customer(); 
  7. customer.setFirstName(rs.getString(1)); 
  8. customer.setLastName(rs.getString(2)); 
  9. ... 
  10. customers.add(customer); 
  11. } 
  12. return customers; 
  13. }
  14. });



RowMapper對每一行進行處理

 
 
 
         
   
   
   
           
  1. List customerList = jdbcTemplate.query("select * from customer", 
  2. new RowMapper(){ 
  3. public Object mapRow(ResultSet rs, int rowNumber) throws SQLException { 
  4. Customer customer = new Customer(); 
  5. customer.setFirstName(rs.getString(1)); 
  6. customer.setLastName(rs.getString(2)); 
  7. ... 
  8. return customer; 
  9. }
  10. });



processRow

 
 
 
         
   
   
   
           
  1. final List customerList = new ArrayList(); 

  2. jdbcTemplate.query("select * from customer", 
  3. new RowCallbackHandler(){ 
  4. public void processRow(ResultSet rs) throws SQLException { 
  5. Customer customer = new Customer(); 
  6. customer.setFirstName(rs.getString(1)); 
  7. customer.setLastName(rs.getString(2)); 
  8. ... 
  9. customerList.add(customer); 
  10. }
  11. }
  12. );



如果你沒有發現最大的差異在哪里,那么容我細表:

  • 使用三種Callback接口作為參數的query方法的返回值不同:

    • 以ResultSetExtractor作為方法參數的query方法返回Object型結果,要使用查詢結果,我們需要對其進行強制轉型

    • 以RowMapper接口作為方法參數的query方法直接返回List型的結果;

    • 以RowCallbackHandler作為方法參數的query方法,返回值為void;

  • 使用ResultSetExtractor作為Callback接口處理查詢結果,我們需要自己聲明集合類,自己遍歷ResultSet,自己根據每行數據組裝Customer對象,自己將組裝后的Customer對象添加到集合類中,方法最終只負責將組裝完成的集合返回;


     





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM