增删改查,想必是每个程序员都会遇到的数据库操作。 作为java程序员来说,同样如此,而且操作的手段可多了。 比如 JDBC,Hiberante, Ibaties等等。
JDBC 太啰嗦了,现在鲜有人写,Hiberante 太复杂, Ibaties居于2者之间。 有没有更简单的? 无需配置,轻量级,编写简单,易学易用?我想 Dbutils 就是。
官方网站: http://commons.apache.org/dbutils/
对 DBUtils 的感觉,优点与不足,看我一一道来:
1. 让我爱上DBUtils的原因是因为它很小,涉及面不多,会jdbc就会它,学习成本几乎为0,仿照官网的例子上手只要5分钟,简单才是王道。
2. 没有那么多异常处理的烦恼,当年学JDBC给我的第一映像就是一层又一层的try…catch….
3. 包装结果集,你有我也有,让大量的包装实现(将结果集转换成List,Array,Map,javabean….)代码化为乌有。代码看看谁比谁清爽。而且可以自定义。ibaties还需要配置
4. 性能,用什么操作数据库最快:JDBC。 而DBUtils,就是JDBC的简单封装。
5. 关连查询,尤其是一对多。其实Hiberante和ibaties也是分2步骤查询的。优势也没多大
6. 插入数据主键返回,这点在实现上不足。我记得ibaties也是在执行完成插入之后,又执行了“SELECT @@IDENTITY AS ID” 的操作。同样,这里也可以在执行完成后,执行SELECT LAST_INSERT_ID()获得自增主键。这里或者干脆自己写一个,怎么写?等一会就列出来。
* 插入数据库,返回自动增长的主键
*
* @param sql -
* 执行的sql语句
* @return 主键
*/
public int insertForKey ( String sql, Object [ ] params ) {
int key = 0 ;
try {
PreparedStatement statement = connection. prepareStatement (sql, Statement. RETURN_GENERATED_KEYS ) ;
ParameterMetaData pmd = statement. getParameterMetaData ( ) ;
if (params. length < pmd. getParameterCount ( ) ) {
throw new SQLException ( "Too many parameters: expected " + pmd. getParameterCount ( ) + ", was given " + params. length ) ;
}
for ( int i = 0 ; i < params. length ; i ++ ) {
statement. setObject (i + 1, params [i ] ) ;
}
statement. executeUpdate ( ) ;
ResultSet resultSet = statement. getGeneratedKeys ( ) ;
if (resultSet. next ( ) ) {
key = resultSet. getInt ( 1 ) ;
}
}
catch ( SQLException e ) {
e. printStackTrace ( ) ;
}
return key ;
}
7. 最大不足
1. 事务处理。这个一般放在service层,ibaties和hibernate都自己实现和封装了。现在要不自己实现,要不取它山之石。这个地方提供的也就是原始的jdbc事务方式,当然这个也不能归责于dbutils,毕竟,它仅仅是说对jdbc的封装
2. 和Hibernate这种全自动的orm mapping来说,其中sql是自己写的,如果一个库有50个表,一个表中有50个字段。 难道insert的时候要自己一个一个的拼装column吗。(当然这里推荐通过反射的方式帮自己实现自动生成sql的方法)
项目运用:
目前用它写过2个小项目,感觉非常的不错。没遇到什么技术上不能实现的瓶颈。
基础封装分享DbUtils can be used to build a DAO framework though 这里和大家分享一下:
private Class <T > clazz ;
@Resource (name = "dataSource" )
private DataSource datasource ;
protected static final Logger logger = LoggerFactory. getLogger ( "com.device.grant.dao" ) ;
@SuppressWarnings ( "unchecked" )
public BaseDao ( ) {
this. clazz = (Class <T > ) ( (ParameterizedType ) getClass ( ). getGenericSuperclass ( ) ). getActualTypeArguments ( ) [ 0 ] ;
}
/**
* 查询并转换成 List 集合列表
*
* @param sql -
* 执行SQL
*
* @param params -
* SQL 必备参数
* @return List<T> - 返回集合列表,没有则返回空集合
*/
public List <T > queryList ( String sql, Object [ ] params ) {
Connection connection = null ;
List <T > value = null ;
try {
connection = datasource. getConnection ( ) ;
QueryRunner run = new QueryRunner ( ) ;
value = (List <T > ) run. query (connection, sql, new BeanListHandler <T > (clazz ), params ) ;
} catch ( Exception e ) {
logger. error ( "queryList - the SQL is " + sql + ",params is " + params, e ) ;
value = Collections. emptyList ( ) ;
} finally {
DbUtils. closeQuietly (connection ) ;
}
return value ;
}
/**
* 获取单个 javaBean 对象
*
* @param sql -
* 执行SQL脚本
*
* @param params -
* 执行脚本参数
* @return T - 查询的对象
*/
public T get ( String sql, Object [ ] params ) {
Connection connection = null ;
T object = null ;
try {
connection = datasource. getConnection ( ) ;
QueryRunner run = new QueryRunner ( ) ;
object = run. query (connection, sql, new BeanHandler <T > (clazz ), params ) ;
} catch ( SQLException e ) {
logger. error ( "get - the sql is " + sql + ", params is " + params, e ) ;
} finally {
DbUtils. closeQuietly (connection ) ;
}
return object ;
}
/**
* 统计当前操作的记录数
*
* @param sql -
* 统计SQL
* @param params -
* 参数
* @return int - 返回的行数, -1 表示异常
*/
public int count ( String sql, Object [ ] params ) {
Connection connection = null ;
int count = - 1 ;
try {
connection = datasource. getConnection ( ) ;
QueryRunner run = new QueryRunner ( ) ;
count = run. query (connection, sql, new ResultSetHandler <Integer > ( ) {
public Integer handle ( ResultSet rs ) throws SQLException {
rs. next ( ) ;
return rs. getInt ( 1 ) ;
}
}, params ) ;
} catch ( Exception e ) {
logger. error ( "count - the sql is " + sql + ", params is " + params, e ) ;
} finally {
DbUtils. closeQuietly (connection ) ;
}
return count ;
}
/**
* 修改
*
* @param sql -
* SQL 修改命令
* @param params -
* 修改执行参数
*
* @return boolean - 是否执行成功
*/
public boolean update ( String sql, Object [ ] params ) {
return deleteOrUpdateOrInsert (sql, params ) ;
}
/**
* 删除
*
* @param sql -
* SQL 删除命令
* @param params -
* 修改执行参数
*
* @return boolean - 是否执行成功
*/
public boolean delete ( String sql, Object [ ] params ) {
return deleteOrUpdateOrInsert (sql, params ) ;
}
/**
* 删除
*
* @param sql -
* SQL 删除命令
* @param params -
* 修改执行参数
*
* @return boolean - 是否执行成功
*/
public boolean insert ( String sql, Object [ ] params ) {
return deleteOrUpdateOrInsert (sql, params ) ;
}
/**
* 修改或删除或新增执行
*
* @param sql -
* SQL 命令脚本
* @param params -
* 参数
* @return boolean - 执行结果
*/
private boolean deleteOrUpdateOrInsert ( String sql, Object [ ] params ) {
Connection connection = null ;
boolean flag = false ;
try {
connection = datasource. getConnection ( ) ;
QueryRunner run = new QueryRunner ( ) ;
if (run. update (connection, sql, params ) > 0 ) {
flag = true ;
}
} catch ( Throwable e ) {
logger. error ( "deleteOrUpdateOrInsert- the sql is " + sql + ", params is " + params, e ) ;
} finally {
DbUtils. closeQuietly (connection ) ;
}
return flag ;
}