DbUtils component学习使用(jdbc封装工具)
介绍
The Commons DbUtils library is a small set of classes designed to make
working with JDBC easier. JDBC resource cleanup code is mundane,
error prone work so these classes abstract out all of the cleanup
tasks from your code leaving you with what you really wanted to do
with JDBC in the first place: query and update data.
Some of the advantages of using DbUtils are:
1、No possibility for resource leaks. (不存在资源泄漏风险)
Correct JDBC coding isn't difficult but it is time-consuming and
tedious. This often leads to connection leaks that may be difficult to track down.
2、Cleaner, clearer persistence code. (简短整洁的代码块)
The amount of code needed to persist data in a database is
drastically reduced. The remaining code clearly expresses your intention
without being cluttered with resource cleanup.
3、Automatically populate JavaBean properties from ResultSets. (对于结果集自动映射)
You don't need to manually copy column values into bean instances
by calling setter methods. Each row of the ResultSet can be
represented by one fully populated bean instance.
**备注:**DbUtils是为简化jdbc编写样板代码而出现的(比如结果集映射和连接对象资源的创建和释放)
DbUtils核心类和接口
1、QueryRunner(dbutils的最核心类,是一个线程安全的类)
通常都是初始化的时候,传入一个数据源对象进行初始化.如果我们自己要控制事务的话,
那么我们就不传数据源只传Connection对象给它我们自己控制。
2、ResultSetHandler(结果集映射处理接口,通常用于查询场景使用)
Implementations of this interface convert ResultSets into other objects.
只定义了一个方法
/**
* Turn the ResultSet into an Object.
*
* @param rs The ResultSet to handle. It has not been touched before
* being passed to this * method.
* @return An Object initialized with ResultSet data.
* It is legal for implementations to return null if theResultSet contained 0 rows.
*/
T handle(ResultSet rs) throws SQLException;
最常用的四个具体实现对象:
BeanHandler//查询返回单个对象
BeanListHandler//查询返回多个对象
ScalarHandler//查询返回某一个字段对象
MapHandler//查询返回Map数据结构
3、RowProcessor(结果映射器行处理器)
/**
* RowProcessor implementations convert
* ResultSet rows into various other objects. Implementations
* can extend BasicRowProcessor to protect themselves
* from changes to this interface.
* @see BasicRowProcessor(参考实现)
*/
public interface RowProcessor {
/**
* Create a JavaBean from the column values in one ResultSet
* row. The <code>ResultSet should be positioned on a valid row before
* passing it to this method. Implementations of this method must not
* alter the row position of the ResultSet.
*/
<T> T toBean(ResultSet rs, Class<T> type) throws SQLException;
<T> List<T> toBeanList(ResultSet rs, Class<T> type) throws SQLException;
Map<String, Object> toMap(ResultSet rs) throws SQLException;
Object[] toArray(ResultSet rs) throws SQLException;
}
备注说明:
其实DBUtils帮我们处理的出参映射(结果集映射),但是没有给我们处理入参映射.
而mybatis则对于入参和出参都有帮我们处理。
QueryRunner核心方法
1、update 操作(插入、删除、更新)
public int update(String sql, Object... params) {
Connection conn = this.prepareConnection();
return this.update(conn, true, sql, params);
}
//备注:更新操作比较简单只需要使用占位符就可以,没有返回值结果集需要处理。
2、query 操作(查询操作核心)
public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh) {
return this.<T>query(conn, false, sql, rsh, (Object[]) null);
}
//备注:查询操纵相对更新操纵更复杂一下,需要先确定返回结果集对应的处理器。
//结果集处理器BeanHandler、BeanListHandler、ScalarHandler、MapHandler
3、insert(插入操作并返回主键方法)
public <T> T insert(String sql, ResultSetHandler<T> rsh, Object... params){
return insert(this.prepareConnection(), true, sql, rsh, params);
}
//新增记录并返回主键
4、batch operate(批量操作)
public int[] batch(String sql, Object[][] params) throws SQLException {
Connection conn = this.prepareConnection();
return this.batch(conn, true, sql, params);
}
//通常批量操作应用场景有限
public <T> T insertBatch(String sql, ResultSetHandler<T> rsh, Object[][] params) {
return insertBatch(this.prepareConnection(), true, sql, rsh, params);
}
//通常批量操作应用场景有限
QueryRunner类说明
QueryRunner是一个线程安全类,所以只需要初始化一次就可以了。初始化QueryRunner有两种方式,一种是传入数据源到构造器中一种是不传入数据源到构造器中。如果要进行事务的管理的话,最好就不要传入数据源,直接获得Connection对象来处理,或者直接使用jdbc来操作。如果和spring 框架集成的话,那么数据源就会交给IOC容器来管理,事务也应该要交给spring 容器来管理。
**备注:**DbUtils还是使用了java反射机制来实现的,所以学好反射机制很重要。
研究apache下的commons下的开源小组件很有益处
参考
http://commons.apache.org/proper/commons-dbutils/index.html
http://gao-xianglong.iteye.com/blog/2166444