Apache Commons DBUtil组件的数据库操作类
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class DBUtils {
private final DataSource dataSource;
private final QueryRunner queryRunner;
private static ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();
private static final Log LOG = LogFactory.getLog(DBUtils.class);
private boolean transactional = false;
public DBUtils() {
dataSource = (DataSource) SpringTool.getBean("dataSource");
queryRunner = new QueryRunner(dataSource);
this.transactional = false;
}
public DBUtils(String dataSourceName) {
dataSource = (DataSource) SpringTool.getBean(dataSourceName);
queryRunner = new QueryRunner(dataSource);
this.transactional = false;
}
public DBUtils(boolean transactional) {
dataSource = (DataSource) SpringTool.getBean("dataSource");
queryRunner = new QueryRunner(dataSource);
this.transactional = transactional;
}
public DBUtils(String dataSourceName, boolean transactional) {
dataSource = (DataSource) SpringTool.getBean(dataSourceName);
queryRunner = new QueryRunner(dataSource);
this.transactional = transactional;
}
public Connection getConnection() {
Connection conn = threadLocal.get();
if (conn == null) {
try {
conn = dataSource.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
threadLocal.set(conn);
}
return conn;
}
public void startTransaction() {
try {
Connection conn = threadLocal.get();
if (conn == null) {
conn = getConnection();
}
conn.setAutoCommit(false);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void rollback() {
try {
Connection conn = threadLocal.get();
if (conn != null) {
conn.rollback();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void commit() {
try {
Connection conn = threadLocal.get();
if (conn != null) {
conn.commit();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void close() {
try {
Connection conn = threadLocal.get();
if (conn != null) {
conn.close();
threadLocal.remove();
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public int update(String sql, Object param) {
return update(sql, new Object[]{param});
}
public int update(String sql, Object... params) {
int affectedRows = 0;
if (transactional) {
Connection conn = getConnection();
startTransaction();
try {
if (params == null) {
affectedRows = queryRunner.update(conn, sql);
} else {
affectedRows = queryRunner.update(conn, sql, params);
}
commit();
} catch (SQLException e) {
rollback();
LOG.error("DBUtils更新数据时错误,更新已回滚。", e);
} finally {
close();
}
} else {
try {
if (params == null) {
affectedRows = queryRunner.update(sql);
} else {
affectedRows = queryRunner.update(sql, params);
}
} catch (SQLException e) {
LOG.error("DBUtils更新数据时错误。", e);
}
}
return affectedRows;
}
public int[] batchUpdate(String sql, Object[]... params) {
int[] affectedRows = new int[0];
if (transactional) {
Connection conn = getConnection();
startTransaction();
try {
affectedRows = queryRunner.batch(conn, sql, params);
commit();
} catch (SQLException e) {
rollback();
LOG.error("DBUtils批量更新数据时错误,更新已回滚。", e);
} finally {
close();
}
} else {
try {
affectedRows = queryRunner.batch(sql, params);
} catch (SQLException e) {
LOG.error("DBUtils批量更新数据时错误。", e);
}
}
return affectedRows;
}
public DBSet find(String sql, Object param) {
return find(sql, new Object[]{param});
}
public DBSet find(String sql, Object... params) {
LOG.info(sql);
DBSet dbset = new DBSet();
if (transactional) {
Connection conn = getConnection();
startTransaction();
List<Map<String, Object>> list = null;
try {
if (params == null) {
list = queryRunner.query(conn, sql, new MapListHandler());
} else {
list = queryRunner.query(conn, sql, new MapListHandler(), params);
}
commit();
} catch (SQLException e) {
rollback();
LOG.error("DBUtils查询数据时错误。(事务启用模式)", e);
} finally {
close();
}
if (list != null) {
dbset.addAll(list);
}
} else {
List<Map<String, Object>> list = null;
try {
if (params == null) {
list = queryRunner.query(sql, new MapListHandler());
} else {
list = queryRunner.query(sql, new MapListHandler(), params);
}
} catch (SQLException e) {
LOG.error("DBUtils查询数据时错误。", e);
}
if (list != null) {
dbset.addAll(list);
}
}
return dbset;
}
public <T> List<T> find(Class<T> entityClass, String sql, Object param) {
return find(entityClass, sql, new Object[]{param});
}
public <T> List<T> find(Class<T> entityClass, String sql, Object... params) {
List<T> list = new ArrayList<T>();
if (transactional) {
Connection conn = getConnection();
startTransaction();
try {
if (params == null) {
list = (List<T>) queryRunner.query(conn, sql, new BeanListHandler(entityClass));
} else {
list = (List<T>) queryRunner.query(conn, sql, new BeanListHandler(entityClass), params);
}
commit();
} catch (SQLException e) {
rollback();
LOG.error("DBUtils查询数据时错误。(事务启用模式)", e);
} finally {
close();
}
} else {
try {
if (params == null) {
list = (List<T>) queryRunner.query(sql, new BeanListHandler(entityClass));
} else {
list = (List<T>) queryRunner.query(sql, new BeanListHandler(entityClass), params);
}
} catch (SQLException e) {
LOG.error("DBUtils查询数据时错误。", e);
}
}
return list;
}
public <T> T findFirst(Class<T> entityClass, String sql, Object param) {
return findFirst(entityClass, sql, new Object[]{param});
}
public <T> T findFirst(Class<T> entityClass, String sql, Object... params) {
Object object = null;
if (transactional) {
Connection conn = getConnection();
startTransaction();
try {
if (params == null) {
object = queryRunner.query(conn, sql, new BeanHandler(entityClass));
} else {
object = queryRunner.query(conn, sql, new BeanHandler(entityClass), params);
}
commit();
} catch (SQLException e) {
rollback();
LOG.error("DBUtils查询数据时错误。(事务启用模式)", e);
} finally {
close();
}
} else {
try {
if (params == null) {
object = queryRunner.query(sql, new BeanHandler(entityClass));
} else {
object = queryRunner.query(sql, new BeanHandler(entityClass), params);
}
} catch (SQLException e) {
LOG.error("DBUtils查询数据时错误。", e);
}
}
return (T) object;
}
}