宏观上把握DbUtils

来源:互联网 发布:重庆少儿编程 编辑:程序博客网 时间:2024/06/06 18:48

DbUtils是个一个小巧的JDBC轻量级封装的工具包,其最核心的特性是对结果集的封装,可以直接将查询出来的结果集封装成JavaBean、Object[]、Map<String,Object> 等,这就为我们做了最枯燥乏味、并且最容易出错的一大部分工作。

版本号:1.6

DbUtils 学习记录

DbUtils学习—-DbUtils类
DBUtils学习—-QueryRunner类
DBUtils学习—-ResultSetHandler接口与实现
DBUtils学习—-RowProcessor接口与实现
DBUtils学习—-BeanProcessor类
DBUtils学习—-QueryLoader类


实际编程过程中,我们使用最多的就是:
QueryRunner类:使用可插拔的策略执行SQL查询并处理结果集
该类的几个主要方法:

private int[]   batch(Connection conn, boolean closeConn, String sql, Object[][] params);private <T> T   query(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params);private int     update(Connection conn, boolean closeConn, String sql, Object... params);private <T> T   insert(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params);private <T> T   insertBatch(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object[][] params);

ResultSetHandler<T>接口:将ResultSet转换为对象
ResultSetHandler<T> 的具体实现类则为我们完成相应的转换工作
该类只有一个方法T handle(ResultSet rs) throws SQLException;

ResultSetHandler

它们之间的关系:
这里写图片描述

BeanListHandler 为什么不继承AbstractListHandler而是直接实现ResultSetHandler 接口?
这是我学习DbUtils的疑问。

Apache官方给出的解释:
https://issues.apache.org/jira/browse/DBUTILS-37
https://issues.apache.org/jira/browse/DBUTILS-59

public <T> T toBean(ResultSet rs, Class<T> type) throws SQLException {        PropertyDescriptor[] props = this.propertyDescriptors(type);        ResultSetMetaData rsmd = rs.getMetaData();        int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);        return this.createBean(rs, type, props, columnToProperty);}public <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException {        List<T> results = new ArrayList<T>();        if (!rs.next()) {            return results;        }        PropertyDescriptor[] props = this.propertyDescriptors(type);        ResultSetMetaData rsmd = rs.getMetaData();        int[] columnToProperty = this.mapColumnsToProperties(rsmd, props);        do {            results.add(this.createBean(rs, type, props, columnToProperty));        } while (rs.next());        return results;}

it was too slow to call the method mapColumnsToProperties for each row returned by the result set.
That’s also why there is a toBeanList method in the RowProcessor interface.

public interface ResultSetHandler<T> {    T handle(ResultSet rs) throws SQLException;}

ResultSetHandler<T> 接口只有一个方法,用户可以自定义实现该方法完成ResultSet 结果集到相应对象的转换;或者,使用DbUtils为我们提供的上述实现类。

ResultSetHandler<T> 的实现类在完成转换操作时,涉及另一个接口:RowProcessor,顾名思义,行处理器。

public interface RowProcessor {    Object[] toArray(ResultSet rs) throws SQLException;    <T> T toBean(ResultSet rs, Class<T> type) throws SQLException;    Map<String, Object> toMap(ResultSet rs) throws SQLException;    <T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException;}

RowProcessor 接口有四个方法,分别完成结果集到Object[]JavaBeanMap<String, Object>List<JavaBean> 的转换。

toArray与toMap的实现在该接口的实现类BasicRowProcessor 中;而toBean与toBeanList的实现依赖另一个类BeanProcessor 以及它的子类GenerousBeanProcessor

这里写图片描述

以上所有类的关系图:
这里写图片描述


参考:
http://peiquan.blog.51cto.com/7518552/1420159
http://www.cnblogs.com/myit/p/4269165.html
http://peiquan.blog.51cto.com/7518552/1427588