java dbutils 模板_DbutilsTemplate模拟Spring JdbcTemplate

package com.amd.eas.ulsd.dppm.loader.core;

import java.sql.Connection;

import java.sql.SQLException;

import javax.sql.DataSource;

import org.apache.commons.dbutils.QueryRunner;

import org.apache.commons.dbutils.ResultSetHandler;

import org.springframework.dao.DataAccessException;

import org.springframework.jdbc.datasource.DataSourceUtils;

import org.springframework.util.Assert;

public class DbutilsTemplate {

/**

* This class is thread safe

*/

private QueryRunner queryRunner = new QueryRunner(true);

private DataSource dataSource;

public DbutilsTemplate(DataSource dataSource) {

this.dataSource = dataSource;

}

public QueryRunner getQueryRunner() {

return this.queryRunner;

}

public DataSource getDataSource() {

return this.dataSource;

}

/**

* Execute an SQL SELECT query without any replacement parameters.

*

* @param The type of object that the handler returns

* @param sql The query to execute.

* @param rsh The handler that converts the results into an object.

* @return The object returned by the handler.

* @throws DataAccessException if a database access error occurs

*/

public T query(final String sql, final ResultSetHandler rsh)

throws DataAccessException {

class ExecuteCallback implements DbutilsCallback {

@Override

public T execute(Connection connection) throws SQLException {

return getQueryRunner().query(connection, sql, rsh);

}

}

return execute(new ExecuteCallback());

}

/**

* Execute an SQL SELECT query with replacement parameters.

*

* @param The type of object that the handler returns

* @param sql The query to execute.

* @param rsh The handler that converts the results into an object.

* @param params The replacement parameters.

* @return The object returned by the handler.

* @throws DataAccessException if a database access error occurs

*/

public T query(final String sql, final ResultSetHandler rsh,

final Object... params) throws DataAccessException {

class ExecuteCallback implements DbutilsCallback {

@Override

public T execute(Connection connection) throws SQLException {

return getQueryRunner().query(connection, sql, rsh, params);

}

}

return execute(new ExecuteCallback());

}

/**

* Execute a batch of SQL INSERT, UPDATE, or DELETE queries with replacement parameters.

*

* @param sql The SQL to execute.

* @param params An array of query replacement parameters. Each row in

* this array is one set of batch replacement values.

* @return The number of rows updated per statement.

* @throws DataAccessException if a database access error occurs

*/

public int[] batch(final String sql, final Object[][] params)

throws DataAccessException {

class ExecuteCallback implements DbutilsCallback {

@Override

public int[] execute(Connection connection) throws SQLException {

return getQueryRunner().batch(connection, sql, params);

}

}

return execute(new ExecuteCallback());

}

/**

* Execute an SQL INSERT query without any replacement parameters.

*

* @param The type of object that the handler returns

* @param sql The SQL to execute.

* @param rsh The handler used to create the result object from

* the ResultSet of auto-generated keys.

* @return An object generated by the handler.

* @throws DataAccessException if a database access error occurs

*/

public T insert(final String sql, final ResultSetHandler rsh)

throws DataAccessException {

class ExecuteCallback implements DbutilsCallback {

@Override

public T execute(Connection connection) throws SQLException {

return getQueryRunner().insert(connection, sql, rsh,

(Object[]) null);

}

}

return execute(new ExecuteCallback());

}

/**

* Execute an SQL INSERT query with replacement parameters.

*

* @param The type of object that the handler returns

* @param sql The SQL to execute.

* @param rsh The handler used to create the result object from

* the ResultSet of auto-generated keys.

* @return An object generated by the handler.

* @throws DataAccessException if a database access error occurs

*/

public T insert(final String sql, final ResultSetHandler rsh,

final Object... params) throws DataAccessException {

class ExecuteCallback implements DbutilsCallback {

@Override

public T execute(Connection connection) throws SQLException {

return getQueryRunner().insert(connection, sql, rsh, params);

}

}

return execute(new ExecuteCallback());

}

/**

* Executes the given batch of INSERT SQL statements with replacement parameters.

*

* @param The type of object that the handler returns

* @param sql The SQL to execute.

* @param rsh The handler used to create the result object from

* the ResultSet of auto-generated keys.

* @param params The query replacement parameters.

* @return The result generated by the handler.

* @throws DataAccessException if a database access error occurs

*/

public T insertBatch(final String sql, final ResultSetHandler rsh,

final Object[][] params) throws DataAccessException {

class ExecuteCallback implements DbutilsCallback {

@Override

public T execute(Connection connection) throws SQLException {

return getQueryRunner().insertBatch(connection, sql, rsh, params);

}

}

return execute(new ExecuteCallback());

}

/**

* Execute an SQL INSERT, UPDATE, or DELETE query without any replacement parameters.

*

* @param sql The SQL to execute.

* @return The number of rows updated.

* @throws DataAccessException if a database access error occurs

*/

public int update(final String sql) throws DataAccessException {

class ExecuteCallback implements DbutilsCallback {

@Override

public Integer execute(Connection connection) throws SQLException {

return getQueryRunner().update(connection, sql);

}

}

return execute(new ExecuteCallback());

}

/**

* Execute an SQL INSERT, UPDATE, or DELETE query with replacement parameters.

*

* @param sql The SQL to execute.

* @param params The query replacement parameters.

* @return The number of rows updated.

* @throws DataAccessException if a database access error occurs

*/

public int update(final String sql, final Object... params)

throws DataAccessException {

class ExecuteCallback implements DbutilsCallback {

@Override

public Integer execute(Connection connection) throws SQLException {

return getQueryRunner().update(connection, sql, params);

}

}

return execute(new ExecuteCallback());

}

/**

* Execute a JDBC(dbutils) data access operation, implemented as callback

* action working on a JDBC Connection. This allows for implementing

* arbitrary data access operations, within Spring's managed JDBC

* environment: that is, participating in Spring-managed transactions and

* converting JDBC SQLExceptions into Spring's DataAccessException

* hierarchy.

*

* The callback action can return a result object, for example a domain

* object or a collection of domain objects.

*

* @param action

* @return

* @throws DataAccessException

*/

private T execute(DbutilsCallback action) throws DataAccessException {

Assert.notNull(action, "Callback object must not be null");

Connection connection = DataSourceUtils.getConnection(getDataSource());

try {

return action.execute(connection);

} catch (SQLException e) {

DataSourceUtils.releaseConnection(connection, getDataSource());

throw new DbutilsDataAccessException("execute error", e.getCause());

} finally {

DataSourceUtils.releaseConnection(connection, getDataSource());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值