简化JDBC操作的框架,Commons DbUtils 1.6 发布
jopen 10年前
Common Dbutils是操作数据库的组件,对传统操作数据库的类进行二次封装,可以把结果集转化成List。
DBUtils包括3个包:
org.apache.commons.dbutils
org.apache.commons.dbutils.handlers
org.apache.commons.dbutils.wrappers
DBUtils封装了对JDBC的操作,简化了JDBC操作,可以少写代码。
// Create a ResultSetHandler implementation to convert the // first row into an Object[]. ResultSetHandler<Object[]> h = new ResultSetHandler<Object[]>() { public Object[] handle(ResultSet rs) throws SQLException { if (!rs.next()) { return null; } ResultSetMetaData meta = rs.getMetaData(); int cols = meta.getColumnCount(); Object[] result = new Object[cols]; for (int i = 0; i < cols; i++) { result[i] = rs.getObject(i + 1); } return result; } }; // Create a QueryRunner that will use connections from // the given DataSource QueryRunner run = new QueryRunner(dataSource); // Execute the query and get the results back from the handler Object[] result = run.query( "SELECT * FROM Person WHERE name=?", h, "John Doe");
2014-07-20发布了1.6版本,这个版本主要是Bug修复和Insert方法添加。详细如下:
类型 | 变化 | By |
---|---|---|
ArrayHandler应该返回一个空数组,当手柄没有数据行时。 Fixes DBUTILS-110. | britter | |
Order of columns not retained in BasicRowProcessor with HashMap Fixes DBUTILS-114. Thanks to Michael Osipov. | britter | |
BeanProcessor not returning nanoseconds Fixes DBUTILS-118. Thanks to Feysal Rujbally, Daniele Cremonini. | britter | |
Add support for conversion of ResultSet strings to enums in the BeanProcessor Fixes DBUTILS-113. Thanks to Graylin Kim, Michael Osipov. | britter | |
In BeanProcessor#isCompatibleType, can Integer.class.isInstance(value) be replaced by value instanceof Integer (etc)? Simplified code by using instanceof. Fixes DBUTILS-85. | sebb | |
DBUtils can't build using JDK 1.7 - DriverProxy needs to implement getParentLogger() Add dynamic invocation. Fixes DBUTILS-106. Thanks to Niall Pemberton. | sebb | |
Create functionality to return auto-generated keys in batches of SQL inserts Fixes DBUTILS-108. Thanks to Micah Huff. | wspeirs | |
Patch QueryLoader to also load from XML properties files Fixes DBUTILS-107. Thanks to PB. | wspeirs | |
Updated the use of getColumnName to try getColumnLabel first Fixes DBUTILS-100. Thanks to xiaofei.xu. | wspeirs | |
Add missing JavaDoc to QueryRunner#insert Fixes DBUTILS-98. Thanks to Moandji Ezana. | simonetripodi | |
Add an Abstract ResultSetHandler implementation in order to reduce redundant 'resultSet' variable invocation Fixes DBUTILS-97. | simonetripodi | |
DbUtils#loadDriver(ClassLoader,String) makes DriverManager throwing "No suitable driver found for jdbc" if ClassLoader is not the System's one Fixes DBUTILS-96. Thanks to yuyf. | simonetripodi | |
Added insert methods to QueryRunner and AsyncQueryRunner that return the generated key. Fixes DBUTILS-87. Thanks to Moandji Ezana. | wspeirs |
项目主页:http://commons.apache.org/proper/commons-dbutils/