02-dbutils源碼之QueryRunner & 一個例子

在01中講了下AbstractQueryRunner的基本內容,對於JDBC比較熟悉的童鞋均可以很容易明白。接下來會分析下AbstractQueryRunner的子類QueryRunner。這個類是最經常使用的了。
        
        下面是類的繼承關係:
        

        下面是類的outline
        
        在這outline中,我圈出了要分析的源碼。重點在於分析當中的私有方法。由於私有方法是通用的。例如上圖中我圈中第一個和第二個公開方法query()內部就是調用第三個私有方法query()。

        
     下面看一下詳細的說明
/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package  org. apache. commons. dbutils;
import  java. sql. Connection;
import  java. sql. PreparedStatement;
import  java. sql. ResultSet;
import  java. sql. SQLException;
import  javax. sql. DataSource;
/**
 * Executes SQL queries with pluggable strategies for handling
 * <code>ResultSet</code>s.  This class is thread safe.
 * SQL使
 * ResultSetHandler
 *  @see  ResultSetHandler
 */
public  class  QueryRunner  extends  AbstractQueryRunner {
     /**
      * Constructor for QueryRunner.
      */
     public  QueryRunner() {
         super();
    }
     /**
      * Constructor for QueryRunner that controls the use of <code>ParameterMetaData</code>.
      *
      *  @param  pmdKnownBroken Some drivers don't support { @link  java.sql.ParameterMetaData#getParameterType(int) };
      * if <code>pmdKnownBroken</code> is set to true, we won't even try it; if false, we'll try it,
      * and if it breaks, we'll remember not to use it again.
      */
     public  QueryRunner( boolean  pmdKnownBroken) {
         super( pmdKnownBroken);
    }
     /**
      * Constructor for QueryRunner that takes a <code>DataSource</code> to use.
      *
      * Methods that do not take a <code>Connection</code> parameter will retrieve connections from this
      * <code>DataSource</code>.
      *
      *  @param  ds The <code>DataSource</code> to retrieve connections from.
      */
     public  QueryRunner( DataSource  ds) {
         super( ds);
    }
     /**
      * Constructor for QueryRunner that takes a <code>DataSource</code> and controls the use of <code>ParameterMetaData</code>.
      * Methods that do not take a <code>Connection</code> parameter will retrieve connections from this
      * <code>DataSource</code>.
      *
      *  @param  ds The <code>DataSource</code> to retrieve connections from.
      *  @param  pmdKnownBroken Some drivers don't support { @link  java.sql.ParameterMetaData#getParameterType(int) };
      * if <code>pmdKnownBroken</code> is set to true, we won't even try it; if false, we'll try it,
      * and if it breaks, we'll remember not to use it again.
      */
     public  QueryRunner( DataSource  dsboolean  pmdKnownBroken) {
         super( dspmdKnownBroken);
    }
     /**
      * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.
      * INSERTUPDATEDELETE
      *  @param  conn The Connection to use to run the query.  The caller is
      * responsible for closing this Connection.
      *  @param  sql The SQL to execute.
      *  @param  params An array of query replacement parameters.  Each row in
      * this array is one set of batch replacement values.
      *  @return  The number of rows updated per statement.
      *  @throws  SQLException if a database access error occurs
      *  @since  DbUtils 1.1
      */
     public  int[]  batch( Connection  connString  sqlObject[][]  paramsthrows  SQLException {
         return  this. batch( connfalsesqlparams);
    }
     /**
      * Execute a batch of SQL INSERT, UPDATE, or DELETE queries.  The
      * <code>Connection</code> is retrieved from the <code>DataSource</code>
      * set in the constructor.  This <code>Connection</code> must be in
      * auto-commit mode or the update will not be saved.
      *
      *  @param  sql The SQL to execute.
      *  @param  params An array of query replacement parameters.  Each row in
      * this array is one set of batch replacement values.
      *  @return  The number of rows updated per statement.
      *  @throws  SQLException if a database access error occurs
      *  @since  DbUtils 1.1
      */
     public  int[]  batch( String  sqlObject[][]  paramsthrows  SQLException {
         Connection  conn  =  this. prepareConnection();
         return  this. batch( conntruesqlparams);
    }
     /**
      * Calls update after checking the parameters to ensure nothing is null.
      *  @param  conn The connection to use for the batch call.
      *  @param  closeConn True if the connection should be closed, false otherwise.
      *  @param  sql The SQL statement to execute.
      *  @param  params An array of query replacement parameters.  Each row in
      * this array is one set of batch replacement values.
      *  @return  The number of rows updated in the batch.
      *  @throws  SQLException If there are database or parameter errors.
      */
     private  int[]  batch( Connection  connboolean  closeConnString  sqlObject[][]  paramsthrows  SQLException {
         if ( conn  ==  null) {
              throw  new  SQLException( "Null connection");
        }
         if ( sql  ==  null) {
              if ( closeConn) {
                   close( conn);
             }
              throw  new  SQLException( "Null SQL statement");
        }
         if ( params  ==  null) {
              if ( closeConn) {
                   close( conn);
             }
              throw  new  SQLException( "Null parameters. If parameters aren't need, pass an empty array.");
        }
         PreparedStatement  stmt  =  null;
         int[]  rows  =  null;
         try {
              stmt  =  this. prepareStatement( connsql);
              for ( int  i  =  0i  <  params. lengthi ++) {
                   this. fillStatement( stmtparams[ i]);
                   stmt. addBatch();
             }
              rows  =  stmt. executeBatch();
        }  catch ( SQLException  e) {
              this. rethrow( esql, ( Object[]) params);
        }  finally {
              close( stmt);
              if ( closeConn) {
                   close( conn);
             }
        }
         return  rows;
    }
     /**
      * Execute an SQL SELECT query with a single replacement parameter. The
      * caller is responsible for closing the connection.
      *  @param  <T> The type of object that the handler returns
      *  @param  conn The connection to execute the query in.
      *  @param  sql The query to execute.
      *  @param  param The replacement parameter.
      *  @param  rsh The handler that converts the results into an object.
      *  @return  The object returned by the handler.
      *  @throws  SQLException if a database access error occurs
      *  @deprecated  Use { @link  #query(Connection, String, ResultSetHandler, Object...)}
      */
    @ Deprecated
     public  < T >  T  query( Connection  connString  sqlObject  paramResultSetHandler < T >  rshthrows  SQLException {
         return  this. < T > query( connfalsesqlrshnew  Object[]{ param});
    }
     /**
      * Execute an SQL SELECT query with replacement parameters.  The
      * caller is responsible for closing the connection.
      *  @param  <T> The type of object that the handler returns
      *  @param  conn The connection to execute the query in.
      *  @param  sql The query to execute.
      *  @param  params The replacement parameters.
      *  @param  rsh The handler that converts the results into an object.
      *  @return  The object returned by the handler.
      *  @throws  SQLException if a database access error occurs
      *  @deprecated  Use { @link  #query(Connection,String,ResultSetHandler,Object...)} instead
      */
    @ Deprecated
     public  < T >  T  query( Connection  connString  sqlObject[]  paramsResultSetHandler < T >  rshthrows  SQLException {
                   return  this. < T > query( connfalsesqlrshparams);
    }
     /**
      * Execute an SQL SELECT query with replacement parameters.  The
      * caller is responsible for closing the connection.
      *  @param  <T> The type of object that the handler returns
      *  @param  conn The connection to execute the query in.
      *  @param  sql The query to execute.
      *  @param  rsh The handler that converts the results into an object.
      *  @param  params The replacement parameters.
      *  @return  The object returned by the handler.
      *  @throws  SQLException if a database access error occurs
      */
     public  < T >  T  query( Connection  connString  sqlResultSetHandler < T >  rshObject...  paramsthrows  SQLException {
         return  this. < T > query( connfalsesqlrshparams);
    }
     /**
      * Execute an SQL SELECT query without any replacement parameters.  The
      * caller is responsible for closing the connection.
      *  @param  <T> The type of object that the handler returns
      *  @param  conn The connection to execute the query in.
      *  @param  sql The query to execute.
      *  @param  rsh The handler that converts the results into an object.
      *  @return  The object returned by the handler.
      *  @throws  SQLException if a database access error occurs
      */
     public  < T >  T  query( Connection  connString  sqlResultSetHandler < T >  rshthrows  SQLException {
         return  this. < T > query( connfalsesqlrsh, ( Object[])  null);
    }
     /**
      * Executes the given SELECT SQL with a single replacement parameter.
      * The <code>Connection</code> is retrieved from the
      * <code>DataSource</code> set in the constructor.
      *  @param  <T> The type of object that the handler returns
      *  @param  sql The SQL statement to execute.
      *  @param  param The replacement parameter.
      *  @param  rsh The handler used to create the result object from
      * the <code>ResultSet</code>.
      *
      *  @return  An object generated by the handler.
      *  @throws  SQLException if a database access error occurs
      *  @deprecated  Use { @link  #query(String, ResultSetHandler, Object...)}
      */
    @ Deprecated
     public  < T >  T  query( String  sqlObject  paramResultSetHandler < T >  rshthrows  SQLException {
         Connection  conn  =  this. prepareConnection();
         return  this. < T > query( conntruesqlrshnew  Object[]{ param});
    }
     /**
      * Executes the given SELECT SQL query and returns a result object.
      * The <code>Connection</code> is retrieved from the
      * <code>DataSource</code> set in the constructor.
      * 使使
      *  @param  <T> The type of object that the handler returns
      *  @param  sql The SQL statement to execute.
      *  @param  params Initialize the PreparedStatement's IN parameters with
      * this array.
      *
      *  @param  rsh The handler used to create the result object from
      * the <code>ResultSet</code>.
      *
      *  @return  An object generated by the handler.
      *  @throws  SQLException if a database access error occurs
      *  @deprecated  Use { @link  #query(String, ResultSetHandler, Object...)}
      */
    @ Deprecated
     public  < T >  T  query( String  sqlObject[]  paramsResultSetHandler < T >  rshthrows  SQLException {
         Connection  conn  =  this. prepareConnection();
         return  this. < T > query( conntruesqlrshparams);
    }
     /**
      * Executes the given SELECT SQL query and returns a result object.
      * 
      * The <code>Connection</code> is retrieved from the
      * <code>DataSource</code> set in the constructor.
      * 
      *  @param  <T> The type of object that the handler returns
      *  @param  sql The SQL statement to execute.
      *  @param  rsh The handler used to create the result object from
      * the <code>ResultSet</code>.
      * handlerResultSet使
      *  @param  params Initialize the PreparedStatement's IN parameters with
      * this array.
      *  @return  An object generated by the handler.
      *  @throws  SQLException if a database access error occurs
      */
     public  < T >  T  query( String  sqlResultSetHandler < T >  rshObject...  paramsthrows  SQLException {
         //
         Connection  conn  =  this. prepareConnection();
        
         return  this. < T > query( conntruesqlrshparams);
    }
     /**
      * Executes the given SELECT SQL without any replacement parameters.
      * 使select * from t_user
      * The <code>Connection</code> is retrieved from the
      * <code>DataSource</code> set in the constructor.
      *  @param  <T> The type of object that the handler returns
      *  @param  sql The SQL statement to execute.
      *  @param  rsh The handler used to create the result object from
      * the <code>ResultSet</code>.
      *
      *  @return  An object generated by the handler.
      *  @throws  SQLException if a database access error occurs
      */
     public  < T >  T  query( String  sqlResultSetHandler < T >  rshthrows  SQLException {
         //
         Connection  conn  =  this. prepareConnection();
         return  this. < T > query( conntruesqlrsh, ( Object[])  null);
    }
     /**
      * Calls query after checking the parameters to ensure nothing is null.
      *  @param  conn The connection to use for the query call.
      *  @param  closeConn True if the connection should be closed, false otherwise.
      *  @param  sql The SQL statement to execute.
      *  @param  params An array of query replacement parameters.  Each row in
      * this array is one set of batch replacement values.
      *  @return  The results of the query.
      *  @throws  SQLException If there are database or parameter errors.
      */
     private  < T >  T  query( Connection  connboolean  closeConnString  sqlResultSetHandler < T >  rshObject...  params)
              throws  SQLException {
         //null
         if ( conn  ==  null) {
              throw  new  SQLException( "Null connection");
        }
         //SQLnull
         if ( sql  ==  null) {
              if ( closeConn) { //SQL
                   close( conn);
             }
              throw  new  SQLException( "Null SQL statement");
        }
         //null
         if ( rsh  ==  null) {
              if ( closeConn) {
                   close( conn);
             }
              throw  new  SQLException( "Null ResultSetHandler");
        }
         //PreparedStatement
         PreparedStatement  stmt  =  null;
         //ResultSet
         ResultSet  rs  =  null;
         T  result  =  null; //
         try {
             //PreparedStatement
              stmt  =  this. prepareStatement( connsql);
              //?
              this. fillStatement( stmtparams);
              //SQLwrapRSreturn
              rs  =  this. wrap( stmt. executeQuery());
              /**
                * dbutils
                * ResultSetHandlerResultSetHandler
                * ResultSetHandler
                */
              result  =  rsh. handle( rs);
        }  catch ( SQLException  e) {
              this. rethrow( esqlparams);
        }  finally { //
              try {
                   close( rs);
             }  finally {
                   close( stmt);
                   if ( closeConn) {
                        close( conn);
                  }
             }
        }
         //ResultSetHandlerhandle
         return  result;
    }
     /**
      * Execute an SQL INSERT, UPDATE, or DELETE query without replacement
      * parameters.
      * SQL
      *  @param  conn The connection to use to run the query.
      *  @param  sql The SQL to execute.
      *  @return  The number of rows updated.
      *  @throws  SQLException if a database access error occurs
      */
     public  int  update( Connection  connString  sqlthrows  SQLException {
         return  this. update( connfalsesql, ( Object[])  null);
    }
     /**
      * Execute an SQL INSERT, UPDATE, or DELETE query with a single replacement
      * parameter.
      *
      *  @param  conn The connection to use to run the query.
      *  @param  sql The SQL to execute.
      *  @param  param The replacement parameter.
      *  @return  The number of rows updated.
      *  @throws  SQLException if a database access error occurs
      */
     public  int  update( Connection  connString  sqlObject  paramthrows  SQLException {
         return  this. update( connfalsesqlnew  Object[]{ param});
    }
     /**
      * Execute an SQL INSERT, UPDATE, or DELETE query.
      *
      *  @param  conn The connection to use to run the query.
      *  @param  sql The SQL to execute.
      *  @param  params The query replacement parameters.
      *  @return  The number of rows updated.
      *  @throws  SQLException if a database access error occurs
      */
     public  int  update( Connection  connString  sqlObject...  paramsthrows  SQLException {
         return  update( connfalsesqlparams);
    }
     /**
      * Executes the given INSERT, UPDATE, or DELETE SQL statement without
      * any replacement parameters. The <code>Connection</code> is retrieved
      * from the <code>DataSource</code> set in the constructor.  This
      * <code>Connection</code> must be in auto-commit mode or the update will
      * not be saved.
      * INSERT,UPDATE,DELETEConnection
      * 
      *  @param  sql The SQL statement to execute.
      *  @throws  SQLException if a database access error occurs
      *  @return  The number of rows updated.
      */
     public  int  update( String  sqlthrows  SQLException {
         Connection  conn  =  this. prepareConnection();
         //使null
         return  this. update( conntruesql, ( Object[])  null);
    }
     /**
      * Executes the given INSERT, UPDATE, or DELETE SQL statement with
      * a single replacement parameter.  The <code>Connection</code> is
      * retrieved from the <code>DataSource</code> set in the constructor.
      * This <code>Connection</code> must be in auto-commit mode or the
      * update will not be saved.
      * INSERT,UPDATE,DELETEConnection
      * 
      *  @param  sql The SQL statement to execute.
      *  @param  param The replacement parameter.
      *  @throws  SQLException if a database access error occurs
      *  @return  The number of rows updated.
      */
     public  int  update( String  sqlObject  paramthrows  SQLException {
         Connection  conn  =  this. prepareConnection();
         return  this. update( conntruesqlnew  Object[]{ param});
    }
     /**
      * Executes the given INSERT, UPDATE, or DELETE SQL statement.  The
      * <code>Connection</code> is retrieved from the <code>DataSource</code>
      * set in the constructor.  This <code>Connection</code> must be in
      * auto-commit mode or the update will not be saved.
      * INSERT,UPDATE,DELETEConnection
      * 
      *  @param  sql The SQL statement to execute.
      *  @param  params Initializes the PreparedStatement's IN (i.e. '?')
      * parameters.
      *  @throws  SQLException if a database access error occurs
      *  @return  The number of rows updated.
      */
     public  int  update( String  sqlObject...  paramsthrows  SQLException {
         Connection  conn  =  this. prepareConnection();
         return  this. update( conntruesqlparams);
    }
     /**
      * Calls update after checking the parameters to ensure nothing is null.
      * 調updatenull
      *  @param  conn The connection to use for the update call.
      *  @param  closeConn True if the connection should be closed, false otherwise.
      *  @param  sql The SQL statement to execute.
      *  @param  params An array of update replacement parameters.  Each row in
      * this array is one set of update replacement values.
      *  @return  The number of rows updated.
      *  @throws  SQLException If there are database or parameter errors.
      */
     private  int  update( Connection  connboolean  closeConnString  sqlObject...  paramsthrows  SQLException {
         //
         if ( conn  ==  null) {
              throw  new  SQLException( "Null connection");
        }
         //SQLcloseConntrue
         if ( sql  ==  null) {
              if ( closeConn) {
                  //close
                   close( conn);
             }
              throw  new  SQLException( "Null SQL statement");
        }
        
         //PreparedStatement
         PreparedStatement  stmt  =  null;
         //updateinsertdelete
         int  rows  =  0;
         try {
             //prepareStatement
              stmt  =  this. prepareStatement( connsql);
              //prepareStatement
              this. fillStatement( stmtparams);
              //SQL
              rows  =  stmt. executeUpdate();
        }  catch ( SQLException  e) {
              this. rethrow( esqlparams);
        }  finally { //
              close( stmt);
              if ( closeConn) {
                   close( conn);
             }
        }
         //
         return  rows;
    }
}
 
以私有的query()進行一下說明:
在JDBC中,咱們經過Connection拿到PreparedStatement,就要爲PreparedStatement進行設置參數。而後execute,返回了ResultSet,而後就要對ResultSet進行處理。
在DBUtils中,只不過是對各個步驟進行了封裝。
得到PreparedStatement,就是this.prepareStatement(conn, sql);
爲PreparedStatement進行設置參數:this.fillStatement(stmt, params);
對ResultSet進行處理:rsh.handle(rs);,rsh是ResultSetHandler接口類型的。rs就是ResultSet。

下面會經過一個例子,讓你們瞭解dbutils的基本使用

-------------------------------------------------------------------------------------------------------------------------------------------------------------------
一、數據庫,數據庫名是testdb
/*
MySQL Data Transfer
Source Host: localhost
Source Database: testdb
Target Host: localhost
Target Database: testdb
Date: 2013/9/12 10:19:21
*/

SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for t_user
-- ----------------------------
DROP TABLE IF EXISTS `t_user`;
CREATE TABLE `t_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(40) DEFAULT NULL,
  `birthday` date DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `t_user` VALUES ('1', '黃錦成', '2013-09-11');
INSERT INTO `t_user` VALUES ('2', 'zxx', '2012-09-11');

二、jar包  


三、數據源幫助類
/**
 * 使C3P0
 *  @author  Administrator
 */
public  class  DataSourceUtil {
     private  static  ComboPooledDataSource  cpds  =  null;
     static{
         cpds  =  new  ComboPooledDataSource();
         try {
              cpds. setDriverClass"com.mysql.jdbc.Driver" );
              cpds. setJdbcUrl"jdbc:mysql:///testdb" );
              cpds. setUser( "root");                                  
              cpds. setPassword( "123");
        }  catch ( PropertyVetoException  e) {
              throw  new  RuntimeException( e);
        }            
    }
    
     public  static  DataSource  getDataSource(){
         if( cpds == null){
              throw  new  RuntimeException( "null");
        }
         return  cpds;
    }
}
 
四、javabean  
 
import  java. util. Date;
public  class  User {
     private  Integer  id;
     private  String  name;
     private  Date  birthday;
    
     public  Integer  getId() {
         return  id;
    }
     public  void  setId( Integer  id) {
         this. id  =  id;
    }
     public  String  getName() {
         return  name;
    }
     public  void  setName( String  name) {
         this. name  =  name;
    }
     public  Date  getBirthday() {
         return  birthday;
    }
     public  void  setBirthday( Date  birthday) {
         this. birthday  =  birthday;
    }
    
    @ Override
     public  String  toString() {
         return  "User [id="  +  id  +  ", name="  +  name  +  ", birthday="  +  birthday
                   +  "]";
    }
    
}
 
五、進行測試
  這裏進行的是update測試
package  com. hjc. dbutils. test;
import  java. util. Date;
import  org. apache. commons. dbutils. QueryRunner;
import  org. junit. Before;
import  org. junit. Test;
import  com. hjc. dbutils. utils. DataSourceUtil;
public  class  UpdateTest {
    
     /*
     * QueryRunner
     */
     QueryRunner  queryRunner  =  null;
    @ Before
     public  void  setUp(){
         queryRunner  =  new  QueryRunner( DataSourceUtil. getDataSource());
    }
    
     /*
     * 調QueryRunnerupdate
     * updateinsertdelete
     * QueryRunnerupdatesqlupdateinsertdelete
     */
    @ Test
     public  void  testUpdate()  throws  Exception{
         String  sql  =  "insert into t_user(name,birthday) values(?,?)";
         Object[]  params  =  new  Object[]{ "hjc", new  Date()};
        
         queryRunner. update( sqlparams);
    }
    
    
    
}
 

這裏進行的是query測試  
 
package  com. hjc. dbutils. test;
import  java. util. Date;
import  org. apache. commons. dbutils. QueryRunner;
import  org. apache. commons. dbutils. ResultSetHandler;
import  org. apache. commons. dbutils. handlers. BeanHandler;
import  org. junit. Before;
import  org. junit. Test;
import  com. hjc. dbutils. domain. User;
import  com. hjc. dbutils. utils. DataSourceUtil;
public  class  BeanHandlerTest {
    
     QueryRunner  queryRunner  =  null;
    @ Before
     public  void  setUp(){
         queryRunner  =  new  QueryRunner( DataSourceUtil. getDataSource());
    }
    
    @ Test
     public  void  testBeanHandler()  throws  Exception{
         String  sql  =  "select * from t_user where id=?";
         Object[]  params  =  new  Object[]{ 1};
         ResultSetHandler < User >  rsh  =  new  BeanHandler < User >( User. class);
        
         User  userResult  =  queryRunner. query( sqlrshparams);
         System. out. println( userResult);
    }
     /**
     * 使BeanHandler
     *  @throws  Exception
     */
    @ Test
     public  void  testBeanHandler2()  throws  Exception{
         String  sql  =  "select * from t_user";
         ResultSetHandler < User >  rsh  =  new  BeanHandler < User >( User. class);
        
         User  userResult  =  queryRunner. query( sqlrsh);
         System. out. println( userResult);
    }
    
}
 
BeanHandler是用於對一行記錄進行處理的,它將一行記錄轉換成一個javabean對象。轉換是如何實現的呢?它使用了反射技術。
javabean的屬性和數據庫表字段之間又是怎樣映射的呢?咱們在設計時,會把javabean屬性和表字段設置爲同樣的。例如javabean中是id、name、birthday,那麼在數據庫表中的字段就是id、name、birthday。

上面使用的 BeanHandler ResultSet 的一個實現類。


能夠看到有個BeanListHandler,那麼這個與BeanHandler什麼關係呢?BeanHandler是將一行記錄變成一個javabean對象,那麼BeanListHandler就是將多行記錄變成多個javabean對象,而後將這些javabean對象放入到一個List集合中。
BeanHandler是用來處理ResultSet中的第一行記錄的,除了第一行記錄,其餘的都不處理。想處理多行的,那麼就用BeanListHandler吧!

下面簡單說下其餘Handler的用法:
ArrayHandler:將一行記錄變成一個數組,ArrayListHandler就是一個數組的List集合
MapHandler:將一行記錄變成一個Map對象,列名爲key,列值爲value,MapListHandler就是一個Map對象的List集合

上面總共講了6個Handler,都是比較經常使用的,以後的源碼分析會重點進行講解。
相關文章
相關標籤/搜索