今天主要讲解下常用的Handler,Handler讲完之后,就能对dbutils整体有了了解了。
来看下ResultSetHandler接口
/** * Implementations of this interface convert ResultSets into other objects. * 此接口的实现类将结果集转化为其他的对象 * @paramthe target type the input ResultSet will be converted to. */public interface ResultSetHandler { /** * Turn the ResultSet
into an Object. * 将结果集转换成对象 * @param rs TheResultSet
to handle. It has not been touched * before being passed to this method. * * @return An Object initialized withResultSet
data. It is * legal for implementations to returnnull
if the *ResultSet
contained 0 rows. * * @throws SQLException if a database access error occurs */ T handle(ResultSet rs) throws SQLException;}
它就只有一个方法声明,很简单,就不讲什么。
在这里我会讲解:BeanHandler、ArrayHandler、BeanListHandler、MapHandler、ScalarHandler这5个Handler。剩下的AbstractKeyedHandler以及实现类和AbstractListHandler及实现类,会在下一篇里讲到。
BeanHandler源码:
/** *ResultSetHandler
implementation that converts the first *ResultSet
row into a JavaBean. This class is thread safe. * ResultSetHandler实现类将结果集的第一行转换成一个javabean * @paramthe target bean type * @see org.apache.commons.dbutils.ResultSetHandler */public class BeanHandler implements ResultSetHandler { /** * The Class of beans produced by this handler. * 这个handler要返回的bean的Class */ private final Class type; /** * The RowProcessor implementation to use when converting rows * into beans. * 将行转换成bean时,使用RowProcessor的哪个实现类。默认使用的是BasicRowProcessor */ private final RowProcessor convert; /** * Creates a new instance of BeanHandler. * 创建一个BeanHandler对象,默认使用的是BasicRowProcessor * @param type The Class that objects returned from handle()
* are created from. */ public BeanHandler(Classtype) { //static final RowProcessor ROW_PROCESSOR = new BasicRowProcessor(); this(type, ArrayHandler.ROW_PROCESSOR); } /** * Creates a new instance of BeanHandler. * 创建一个BeanHandler对象。可以自定义RowProcessor * @param type The Class that objects returned from handle()
* are created from. * @param convert TheRowProcessor
implementation * to use when converting rows into beans. */ public BeanHandler(Classtype, RowProcessor convert) { this.type = type; this.convert = convert; } /** * Convert the first row of the ResultSet
into a bean with the *Class
given in the constructor. * 将RS中的第一行记录转换成一个bean对象,这个bean对象通过在构造器中给定的Class创建。使用了反射。 * @param rsResultSet
to process. * @return An initialized JavaBean ornull
if there were no * rows in theResultSet
. * * @throws SQLException if a database access error occurs * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) */ @Override public T handle(ResultSet rs) throws SQLException { /* * 如果有RS中有记录的时候才进行处理,否则返回null。属性convert在构造函数中赋值了 */ return rs.next() ? this.convert.toBean(rs, this.type) : null; }}
BeanHandler的实现简单,就是调用了BasicRowProcessor中的toBean方法。
BeanListHandler源码:
/** *ResultSetHandler
implementation that converts a *ResultSet
into aList
of beans. This class is * thread safe. * 此实现类将结果集转换成bean的集合 * @paramthe target bean type * @see org.apache.commons.dbutils.ResultSetHandler */public class BeanListHandler implements ResultSetHandler
> { /** * The Class of beans produced by this handler. */ private final Class type; /** * The RowProcessor implementation to use when converting rows * into beans. * 使用的还是BasicRowProcessor */ private final RowProcessor convert; /** * Creates a new instance of BeanListHandler. * * @param type The Class that objects returned from handle()
* are created from. */ public BeanListHandler(Classtype) { this(type, ArrayHandler.ROW_PROCESSOR); } /** * Creates a new instance of BeanListHandler. * * @param type The Class that objects returned from handle()
* are created from. * @param convert TheRowProcessor
implementation * to use when converting rows into beans. */ public BeanListHandler(Classtype, RowProcessor convert) { this.type = type; this.convert = convert; } /** * Convert the whole ResultSet
into a List of beans with * theClass
given in the constructor. * 调用了BasicRowProcessor的toBeanList方法。 * @param rs TheResultSet
to handle. * * @return A List of beans, nevernull
. * * @throws SQLException if a database access error occurs * @see org.apache.commons.dbutils.RowProcessor#toBeanList(ResultSet, Class) */ @Override public Listhandle(ResultSet rs) throws SQLException { return this.convert.toBeanList(rs, type); }}
BeanListHandler跟BeanHandler实现类似。
ArrayHandler源码:
/** *ResultSetHandler
implementation that converts a *ResultSet
into anObject[]
. This class is * thread safe. ** 将ResultSet中的一行记录变成一个Object[]数组,此类是线程安全的 *
* @see org.apache.commons.dbutils.ResultSetHandler */public class ArrayHandler implements ResultSetHandler
MapHandler源码:
/** *ResultSetHandler
implementation that converts the first *ResultSet
row into aMap
. This class is thread * safe. * * @see org.apache.commons.dbutils.ResultSetHandler */public class MapHandler implements ResultSetHandler
ScalarHandler源码:
/** *ResultSetHandler
implementation that converts one *ResultSet
column into an Object. This class is thread safe. * 将结果集中第一行的第一列的值变成一个对象。即是单值的。 * @paramThe type of the scalar * @see org.apache.commons.dbutils.ResultSetHandler */public class ScalarHandler implements ResultSetHandler { /** * The column number to retrieve. */ private final int columnIndex; /** * The column name to retrieve. Either columnName or columnIndex * will be used but never both. */ private final String columnName; /** * Creates a new instance of ScalarHandler. The first column will * be returned from handle()
. * 默认会返回结果集中第一行的第一列的值 */ public ScalarHandler() { this(1, null); } /** * Creates a new instance of ScalarHandler. * * @param columnIndex The index of the column to retrieve from the *ResultSet
. */ public ScalarHandler(int columnIndex) { this(columnIndex, null); } /** * Creates a new instance of ScalarHandler. * * @param columnName The name of the column to retrieve from the *ResultSet
. */ public ScalarHandler(String columnName) { this(1, columnName); } /** Helper constructor * @param columnIndex The index of the column to retrieve from the *ResultSet
. * @param columnName The name of the column to retrieve from the *ResultSet
. */ private ScalarHandler(int columnIndex, String columnName) { this.columnIndex = columnIndex; this.columnName = columnName; } /** * Returns oneResultSet
column as an object via the *ResultSet.getObject()
method that performs type * conversions. * @param rsResultSet
to process. * @return The column ornull
if there are no rows in * theResultSet
. * * @throws SQLException if a database access error occurs * @throws ClassCastException if the class datatype does not match the column type * * @see org.apache.commons.dbutils.ResultSetHandler#handle(java.sql.ResultSet) */ // We assume that the user has picked the correct type to match the column // so getObject will return the appropriate type and the cast will succeed. @SuppressWarnings("unchecked") @Override public T handle(ResultSet rs) throws SQLException { if (rs.next()) { //如果列名为null,那么就使用列码 if (this.columnName == null) { return (T) rs.getObject(this.columnIndex); } else { return (T) rs.getObject(this.columnName); } } else { return null; } }}
ScalarHandler用于获取单值,常用的是计算表的记录数count(*)。下面的代码是测试代码。用的数据库还是之前的。
@Test public void testScalarHandler() throws Exception{ String sql = "select * from t_user where id=?"; Object[] params = new Object[]{3}; ResultSetHandlerrsh = new ScalarHandler (2); System.out.println(queryRunner.query(sql, rsh, params)); }
这几个就是常用的Handler了。平时开发的话,一般是够用了。在查询方面,使用了策略模式。用户可以自定义ResultSetHandler,用户自己来实现对结果集的处理,也可以结合自定义的RowProcessor实现类来一起使用。