diff --git a/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java b/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java
index 09d406d2e..c850d99a1 100755
--- a/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java
+++ b/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java
@@ -6,6 +6,7 @@ import cn.hutool.db.dialect.Dialect;
import cn.hutool.db.handler.BeanListHandler;
import cn.hutool.db.handler.EntityHandler;
import cn.hutool.db.handler.EntityListHandler;
+import cn.hutool.db.handler.ResultSetUtil;
import cn.hutool.db.handler.NumberHandler;
import cn.hutool.db.handler.RsHandler;
import cn.hutool.db.handler.StringHandler;
@@ -32,9 +33,9 @@ import java.util.Map;
* 通过给定的数据源执行给定SQL或者给定数据源和方言,执行相应的CRUD操作
* 提供抽象方法getConnection和closeConnection,用于自定义数据库连接的打开和关闭
*
- * @author Luxiaolei
+ * @author looly
*/
-public abstract class AbstractDb implements Serializable {
+public abstract class AbstractDb> implements ConnectionHolder, Serializable {
private static final long serialVersionUID = 3858951941916349062L;
protected final DataSource ds;
@@ -46,7 +47,7 @@ public abstract class AbstractDb implements Serializable {
* 是否大小写不敏感(默认大小写不敏感)
*/
protected boolean caseInsensitive = GlobalDbConfig.caseInsensitive;
- protected SqlConnRunner runner;
+ protected DialectRunner runner;
// ------------------------------------------------------- Constructor start
@@ -58,36 +59,20 @@ public abstract class AbstractDb implements Serializable {
*/
public AbstractDb(final DataSource ds, final Dialect dialect) {
this.ds = ds;
- this.runner = new SqlConnRunner(dialect);
+ this.runner = new DialectRunner(dialect);
}
// ------------------------------------------------------- Constructor end
- /**
- * 获得链接。根据实现不同,可以自定义获取连接的方式
- *
- * @return {@link Connection}
- * @throws SQLException 连接获取异常
- */
- public abstract Connection getConnection() throws SQLException;
-
- /**
- * 关闭连接
- * 自定义关闭连接有利于自定义回收连接机制,或者不关闭
- *
- * @param conn 连接 {@link Connection}
- */
- public abstract void closeConnection(Connection conn);
-
/**
* 查询
*
* @param sql 查询语句
* @param params 参数
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
* @since 3.1.1
*/
- public List query(final String sql, final Map params) throws SQLException {
+ public List query(final String sql, final Map params) throws DbRuntimeException {
return query(sql, new EntityListHandler(this.caseInsensitive), params);
}
@@ -97,10 +82,10 @@ public abstract class AbstractDb implements Serializable {
* @param sql 查询语句
* @param params 参数
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
* @since 3.1.1
*/
- public List query(final String sql, final Object... params) throws SQLException {
+ public List query(final String sql, final Object... params) throws DbRuntimeException {
return query(sql, new EntityListHandler(this.caseInsensitive), params);
}
@@ -112,10 +97,10 @@ public abstract class AbstractDb implements Serializable {
* @param beanClass 元素Bean类型
* @param params 参数
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
* @since 3.2.2
*/
- public List query(final String sql, final Class beanClass, final Object... params) throws SQLException {
+ public List query(final String sql, final Class beanClass, final Object... params) throws DbRuntimeException {
return query(sql, new BeanListHandler<>(beanClass), params);
}
@@ -125,9 +110,9 @@ public abstract class AbstractDb implements Serializable {
* @param sql 查询语句
* @param params 参数
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
*/
- public Entity queryOne(final String sql, final Object... params) throws SQLException {
+ public Entity queryOne(final String sql, final Object... params) throws DbRuntimeException {
return query(sql, new EntityHandler(this.caseInsensitive), params);
}
@@ -137,9 +122,9 @@ public abstract class AbstractDb implements Serializable {
* @param sql 查询语句
* @param params 参数
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
*/
- public Number queryNumber(final String sql, final Object... params) throws SQLException {
+ public Number queryNumber(final String sql, final Object... params) throws DbRuntimeException {
return query(sql, new NumberHandler(), params);
}
@@ -149,9 +134,9 @@ public abstract class AbstractDb implements Serializable {
* @param sql 查询语句
* @param params 参数
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
*/
- public String queryString(final String sql, final Object... params) throws SQLException {
+ public String queryString(final String sql, final Object... params) throws DbRuntimeException {
return query(sql, new StringHandler(), params);
}
@@ -163,9 +148,9 @@ public abstract class AbstractDb implements Serializable {
* @param rsh 结果集处理对象
* @param params 参数
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
*/
- public T query(final String sql, final RsHandler rsh, final Object... params) throws SQLException {
+ public T query(final String sql, final RsHandler rsh, final Object... params) throws DbRuntimeException {
Connection conn = null;
try {
conn = this.getConnection();
@@ -183,10 +168,10 @@ public abstract class AbstractDb implements Serializable {
* @param rsh 结果集处理对象
* @param paramMap 参数
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
* @since 5.2.2
*/
- public T query(final String sql, final RsHandler rsh, final Map paramMap) throws SQLException {
+ public T query(final String sql, final RsHandler rsh, final Map paramMap) throws DbRuntimeException {
Connection conn = null;
try {
conn = this.getConnection();
@@ -204,10 +189,10 @@ public abstract class AbstractDb implements Serializable {
* @param statementFunc 自定义{@link PreparedStatement}创建函数
* @param rsh 结果集处理对象
* @return 结果对象
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
* @since 5.7.17
*/
- public T query(final Func1 statementFunc, final RsHandler rsh) throws SQLException {
+ public T query(final Func1 statementFunc, final RsHandler rsh) throws DbRuntimeException {
Connection conn = null;
try {
conn = this.getConnection();
@@ -224,9 +209,9 @@ public abstract class AbstractDb implements Serializable {
* @param sql SQL
* @param params 参数
* @return 影响行数
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
*/
- public int execute(final String sql, final Object... params) throws SQLException {
+ public int execute(final String sql, final Object... params) throws DbRuntimeException {
Connection conn = null;
try {
conn = this.getConnection();
@@ -243,9 +228,9 @@ public abstract class AbstractDb implements Serializable {
* @param sql SQL
* @param params 参数
* @return 主键
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
*/
- public Long executeForGeneratedKey(final String sql, final Object... params) throws SQLException {
+ public Long executeForGeneratedKey(final String sql, final Object... params) throws DbRuntimeException {
Connection conn = null;
try {
conn = this.getConnection();
@@ -261,10 +246,10 @@ public abstract class AbstractDb implements Serializable {
* @param sql SQL
* @param paramsBatch 批量的参数
* @return 每个SQL执行影响的行数
- * @throws SQLException SQL执行异常
+ * @throws DbRuntimeException SQL执行异常
* @since 5.4.2
*/
- public int[] executeBatch(final String sql, final Iterable