diff --git a/CHANGELOG.md b/CHANGELOG.md index 7db9d848d..d7e304bf7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ * 【cache 】 CacheObj默认方法改为protected(issue#I3RIEI@Gitee) * 【core 】 FileUtil.isEmpty不存在时返回true(issue#1582@Github) * 【core 】 PhoneUtil增加中国澳门和中国台湾手机号校检方法(pr#331@Gitee) +* 【db 】 分页查询,自定义sql查询,添加参数(pr#332@Gitee) ### 🐞Bug修复 * 【core 】 修复XmlUtil中omitXmlDeclaration参数无效问题(issue#1581@Github) 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 3b1041541..f7c88c2c6 100644 --- a/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java +++ b/hutool-db/src/main/java/cn/hutool/db/AbstractDb.java @@ -673,6 +673,7 @@ public abstract class AbstractDb implements Serializable { /** * 结果的条目数 + * * @param sql sql构造器 * @return 复合条件的结果数 * @throws SQLException SQL执行异常 @@ -681,7 +682,7 @@ public abstract class AbstractDb implements Serializable { Connection conn = null; try { conn = this.getConnection(); - return runner.count(conn, sql.build(),sql.getParamValueArray()); + return runner.count(conn, sql.build(), sql.getParamValueArray()); } finally { this.closeConnection(conn); } @@ -689,31 +690,18 @@ public abstract class AbstractDb implements Serializable { /** * 结果的条目数 + * * @param selectSql 查询SQL语句 + * @param params 查询参数 * @return 复合条件的结果数 * @throws SQLException SQL执行异常 + * @since 5.6.6 */ - public long count(CharSequence selectSql) throws SQLException { + public long count(CharSequence selectSql, Object... params) throws SQLException { Connection conn = null; try { conn = this.getConnection(); - return runner.count(conn, selectSql); - } finally { - 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); + return runner.count(conn, selectSql, params); } finally { this.closeConnection(conn); } @@ -829,35 +817,16 @@ public abstract class AbstractDb implements Serializable { /** * 分页查询
* - * @param 结果对象类型 - * @param sql SQL构建器,可以使用{@link SqlBuilder#of(CharSequence)} 包装普通SQL - * @param page 分页对象 - * @param rsh 结果集处理对象 + * @param 结果对象类型 + * @param sql SQL构建器,可以使用{@link SqlBuilder#of(CharSequence)} 包装普通SQL + * @param page 分页对象 + * @param rsh 结果集处理对象 + * @param params 参数 * @return 结果对象 * @throws SQLException SQL执行异常 - * @since 5.5.3 + * @since 5.6.6 */ - public T page(CharSequence sql, Page page, RsHandler rsh) throws SQLException { - Connection conn = null; - try { - conn = this.getConnection(); - return runner.page(conn, SqlBuilder.of(sql), page, rsh); - } finally { - this.closeConnection(conn); - } - } - - /** - * 分页查询
- * @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 { + public T page(CharSequence sql, Page page, RsHandler rsh, Object... params) throws SQLException { Connection conn = null; try { conn = this.getConnection(); @@ -869,10 +838,11 @@ public abstract class AbstractDb implements Serializable { /** * 分页查询 + * * @param sql SQL构建器 * @param page 分页对象 * @param rsh 结果集处理对象 - * @return: 结果对象 + * @return 结果对象 * @throws SQLException SQL执行异常 */ public T page(SqlBuilder sql, Page page, RsHandler rsh) throws SQLException { @@ -906,13 +876,14 @@ 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 { + public PageResult page(CharSequence sql, Page page, Object... params) throws SQLException { Connection conn = null; try { conn = this.getConnection(); 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 3f221c002..ee63db2e7 100644 --- a/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java +++ b/hutool-db/src/main/java/cn/hutool/db/SqlConnRunner.java @@ -285,26 +285,15 @@ public class SqlConnRunner extends DialectRunner { * * @param conn 数据库连接对象 * @param selectSql 查询语句 + * @param params 查询参数 * @return 结果数 * @throws SQLException SQL异常 + * @since 5.6.6 */ - 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 { + 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){ + if (orderByIndex > 0) { selectSql = StrUtil.subPre(selectSql, orderByIndex); }