宏观上把握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;
它们之间的关系:
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[]
、JavaBean
、Map<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
- 宏观上把握DbUtils
- 宏观上把握SpringMVC框架
- 21_从宏观上把握Hibernate
- 【MyBatis学习01】宏观上把握MyBatis框架
- 【SpringMVC学习01】宏观上把握SpringMVC框架
- 【SpringMVC学习01】宏观上把握SpringMVC框架
- MyBatis学习(一)--宏观上把握MyBatis框架
- 【SpringMVC学习01】宏观上把握SpringMVC框架
- 【MyBatis学习01】宏观上把握MyBatis框架
- 【MyBatis学习01】宏观上把握MyBatis框架
- 【MyBatis学习01】宏观上把握MyBatis框架
- 【MyBatis学习01】宏观上把握MyBatis框架
- 【SpringMVC学习01】宏观上把握SpringMVC框架
- 【SpringMVC学习01】宏观上把握SpringMVC框架
- 【MyBatis学习01】宏观上把握MyBatis框架
- 宏观把握Hadoop生态系统
- C++第一遍宏观把握
- 软件工程之宏观把握
- Html5 移动端 触摸滑动事件
- 时间队列容器,包含类用法
- 第二章作业
- 【面试】秒杀设计
- 2017/8/20
- 宏观上把握DbUtils
- atoi实现
- leveldb
- 引入font字体出现方框框的问题
- 【C#机房重构】七层登录详解
- D
- 单点登录
- POJ 1322 dp
- IGMP:Internet组管理协议