mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add method
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.db;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.lang.func.Func1;
|
||||
import cn.hutool.db.dialect.Dialect;
|
||||
import cn.hutool.db.handler.BeanListHandler;
|
||||
import cn.hutool.db.handler.EntityHandler;
|
||||
@@ -20,6 +21,7 @@ import cn.hutool.db.sql.Wrapper;
|
||||
import javax.sql.DataSource;
|
||||
import java.io.Serializable;
|
||||
import java.sql.Connection;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
@@ -194,6 +196,27 @@ public abstract class AbstractDb implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行自定义的{@link PreparedStatement},结果使用{@link RsHandler}处理<br>
|
||||
* 此方法主要用于自定义场景,如游标查询等
|
||||
*
|
||||
* @param <T> 结果集需要处理的对象类型
|
||||
* @param statementFunc 自定义{@link PreparedStatement}创建函数
|
||||
* @param rsh 结果集处理对象
|
||||
* @return 结果对象
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public <T> T query(Func1<Connection, PreparedStatement> statementFunc, RsHandler<T> rsh) throws SQLException {
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = this.getConnection();
|
||||
return SqlExecutor.query(conn, statementFunc, rsh);
|
||||
} finally {
|
||||
this.closeConnection(conn);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行非查询语句<br>
|
||||
* 语句包括 插入、更新、删除
|
||||
|
@@ -1,6 +1,7 @@
|
||||
package cn.hutool.db.sql;
|
||||
|
||||
import cn.hutool.core.collection.ArrayIter;
|
||||
import cn.hutool.core.lang.func.Func1;
|
||||
import cn.hutool.db.DbUtil;
|
||||
import cn.hutool.db.StatementUtil;
|
||||
import cn.hutool.db.handler.RsHandler;
|
||||
@@ -239,6 +240,22 @@ public class SqlExecutor {
|
||||
return query(conn, namedSql.getSql(), rsh, namedSql.getParams());
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行查询语句<br>
|
||||
* 此方法不会关闭Connection
|
||||
*
|
||||
* @param <T> 处理结果类型
|
||||
* @param conn 数据库连接对象
|
||||
* @param sqlBuilder SQL构建器,包含参数
|
||||
* @param rsh 结果集处理对象
|
||||
* @return 结果对象
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 5.5.3
|
||||
*/
|
||||
public static <T> T query(Connection conn, SqlBuilder sqlBuilder, RsHandler<T> rsh) throws SQLException {
|
||||
return query(conn, sqlBuilder.build(), rsh, sqlBuilder.getParamValueArray());
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行查询语句<br>
|
||||
* 此方法不会关闭Connection
|
||||
@@ -262,19 +279,30 @@ public class SqlExecutor {
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行查询语句<br>
|
||||
* 此方法不会关闭Connection
|
||||
* 执行自定义的{@link PreparedStatement},结果使用{@link RsHandler}处理<br>
|
||||
* 此方法主要用于自定义场景,如游标查询等
|
||||
*
|
||||
* @param <T> 处理结果类型
|
||||
* @param conn 数据库连接对象
|
||||
* @param sqlBuilder SQL构建器,包含参数
|
||||
* @param rsh 结果集处理对象
|
||||
* @return 结果对象
|
||||
* @param statementFunc 自定义{@link PreparedStatement}创建函数
|
||||
* @param rsh 自定义结果集处理
|
||||
* @return 结果
|
||||
* @throws SQLException SQL执行异常
|
||||
* @since 5.5.3
|
||||
* @since 5.7.17
|
||||
*/
|
||||
public static <T> T query(Connection conn, SqlBuilder sqlBuilder, RsHandler<T> rsh) throws SQLException {
|
||||
return query(conn, sqlBuilder.build(), rsh, sqlBuilder.getParamValueArray());
|
||||
public static <T> T query(Connection conn, Func1<Connection, PreparedStatement> statementFunc, RsHandler<T> rsh) throws SQLException {
|
||||
PreparedStatement ps = null;
|
||||
try {
|
||||
ps = statementFunc.call(conn);
|
||||
return executeQuery(ps, rsh);
|
||||
} catch (Exception e) {
|
||||
if(e instanceof SQLException){
|
||||
throw (SQLException) e;
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
} finally {
|
||||
DbUtil.close(ps);
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------------------------------------------------------- Execute With PreparedStatement
|
||||
|
Reference in New Issue
Block a user