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 5faa75f14..3b1041541 100644
--- a/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java
+++ b/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java
@@ -673,7 +673,22 @@ public abstract class AbstractDb implements Serializable {
/**
* 结果的条目数
- *
+ * @param sql sql构造器
+ * @return 复合条件的结果数
+ * @throws SQLException SQL执行异常
+ */
+ public long count(SqlBuilder sql) throws SQLException {
+ Connection conn = null;
+ try {
+ conn = this.getConnection();
+ return runner.count(conn, sql.build(),sql.getParamValueArray());
+ } finally {
+ this.closeConnection(conn);
+ }
+ }
+
+ /**
+ * 结果的条目数
* @param selectSql 查询SQL语句
* @return 复合条件的结果数
* @throws SQLException SQL执行异常
@@ -687,6 +702,22 @@ public abstract class AbstractDb implements Serializable {
this.closeConnection(conn);
}
}
+ /**
+ * 结果的条目数
+ * @param selectSql 查询SQL语句
+ * @param params 查询参数
+ * @return 复合条件的结果数
+ * @throws SQLException SQL执行异常
+ */
+ public long count(CharSequence selectSql,Object ...params) throws SQLException {
+ Connection conn = null;
+ try {
+ conn = this.getConnection();
+ return runner.count(conn, selectSql,params);
+ } finally {
+ this.closeConnection(conn);
+ }
+ }
/**
* 分页查询
@@ -816,6 +847,44 @@ public abstract class AbstractDb implements Serializable {
}
}
+ /**
+ * 分页查询
+ * @param 结果对象类型
+ * @param sql SQL构建器,可以使用{@link SqlBuilder#of(CharSequence)} 包装普通SQL
+ * @param page 分页对象
+ * @param rsh 结果集处理对象
+ * @param params 参数
+ * @return 结果对象
+ * @throws SQLException SQL执行异常
+ */
+ public T page(CharSequence sql, Page page, RsHandler rsh,Object ...params) throws SQLException {
+ Connection conn = null;
+ try {
+ conn = this.getConnection();
+ return runner.page(conn, SqlBuilder.of(sql).addParams(params), page, rsh);
+ } finally {
+ this.closeConnection(conn);
+ }
+ }
+
+ /**
+ * 分页查询
+ * @param sql SQL构建器
+ * @param page 分页对象
+ * @param rsh 结果集处理对象
+ * @return: 结果对象
+ * @throws SQLException SQL执行异常
+ */
+ public T page(SqlBuilder sql, Page page, RsHandler rsh) throws SQLException {
+ Connection conn = null;
+ try {
+ conn = this.getConnection();
+ return runner.page(conn, sql, page, rsh);
+ } finally {
+ this.closeConnection(conn);
+ }
+ }
+
/**
* 分页查询
*
@@ -835,6 +904,24 @@ public abstract class AbstractDb implements Serializable {
}
}
+ /**
+ * 分页查询
+ * @param sql SQL语句字符串
+ * @param page 分页对象
+ * @return 结果对象
+ * @throws SQLException SQL执行异常
+ * @since 5.5.3
+ */
+ public PageResult page(CharSequence sql, Page page ,Object ...params) throws SQLException {
+ Connection conn = null;
+ try {
+ conn = this.getConnection();
+ return runner.page(conn, SqlBuilder.of(sql).addParams(params), page);
+ } finally {
+ this.closeConnection(conn);
+ }
+ }
+
/**
* 分页查询
* 查询条件为多个key value对表示,默认key = value,如果使用其它条件可以使用:where.put("key", " > 1"),value也可以传Condition对象,key被忽略
diff --git a/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java b/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java
index 961bfaafa..3f221c002 100644
--- a/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java
+++ b/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java
@@ -289,6 +289,19 @@ public class SqlConnRunner extends DialectRunner {
* @throws SQLException SQL异常
*/
public long count(Connection conn, CharSequence selectSql) throws SQLException {
+ return this.count(conn,selectSql,null);
+ }
+
+ /**
+ * 获取查询结果总数,生成类似于 SELECT count(1) from (sql) as _count
+ *
+ * @param conn 数据库连接对象
+ * @param selectSql 查询语句
+ * @param params 查询参数
+ * @return 结果数
+ * @throws SQLException SQL异常
+ */
+ public long count(Connection conn, CharSequence selectSql,Object ...params) throws SQLException {
Assert.notBlank(selectSql, "Select SQL must be not blank!");
final int orderByIndex = StrUtil.indexOfIgnoreCase(selectSql, " order by");
if(orderByIndex > 0){
@@ -298,7 +311,9 @@ public class SqlConnRunner extends DialectRunner {
SqlBuilder sqlBuilder = SqlBuilder.of(selectSql)
.insertPreFragment("SELECT count(1) from(")
// issue#I3IJ8X@Gitee,在子查询时需设置单独别名,此处为了防止和用户的表名冲突,使用自定义的较长别名
- .append(") as _hutool_alias_count_");
+ .append(") as _hutool_alias_count_")
+ // 添加参数
+ .addParams(params);
return page(conn, sqlBuilder, null, new NumberHandler()).intValue();
}