From dd29bde837e2595bea1d9381361638fb96f9f0dd Mon Sep 17 00:00:00 2001 From: zbl <651824604@qq.com> Date: Fri, 21 May 2021 14:11:26 +0800 Subject: [PATCH] =?UTF-8?q?hutool-db=E6=A8=A1=E5=9D=97=201.page=20?= =?UTF-8?q?=E4=BD=BF=E7=94=A8=E8=87=AA=E5=AE=9A=E4=B9=89sql=E6=97=B6?= =?UTF-8?q?=EF=BC=8C=E6=9C=89=E6=97=B6=E9=9C=80=E8=A6=81=E4=BC=A0=E9=80=92?= =?UTF-8?q?=E5=8F=82=E6=95=B0,=E4=B8=BA=E6=AD=A4=E6=B7=BB=E5=8A=A0?= =?UTF-8?q?=E6=96=B9=E6=B3=95=202.count=20=E4=BD=BF=E7=94=A8=E8=87=AA?= =?UTF-8?q?=E5=AE=9A=E4=B9=89sql=E6=97=B6=EF=BC=8C=E6=9C=89=E6=97=B6?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E4=BC=A0=E9=80=92=E5=8F=82=E6=95=B0,?= =?UTF-8?q?=E4=B8=BA=E6=AD=A4=E6=B7=BB=E5=8A=A0=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/cn/hutool/db/AbstractDb.java | 89 ++++++++++++++++++- .../main/java/cn/hutool/db/SqlConnRunner.java | 17 +++- 2 files changed, 104 insertions(+), 2 deletions(-) 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(); }