add param support for count and page

This commit is contained in:
Looly 2021-05-22 14:52:22 +08:00
parent 7a006515a5
commit 74a1cb2c68
3 changed files with 23 additions and 62 deletions

View File

@ -12,6 +12,7 @@
* 【cache 】 CacheObj默认方法改为protectedissue#I3RIEI@Gitee
* 【core 】 FileUtil.isEmpty不存在时返回trueissue#1582@Github
* 【core 】 PhoneUtil增加中国澳门和中国台湾手机号校检方法pr#331@Gitee
* 【db 】 分页查询自定义sql查询添加参数pr#332@Gitee
### 🐞Bug修复
* 【core 】 修复XmlUtil中omitXmlDeclaration参数无效问题issue#1581@Github

View File

@ -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语句
* @return 复合条件的结果数
* @throws SQLException SQL执行异常
*/
public long count(CharSequence selectSql) 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执行异常
* @since 5.6.6
*/
public long count(CharSequence selectSql,Object ...params) throws SQLException {
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);
}
@ -833,31 +821,12 @@ public abstract class AbstractDb implements Serializable {
* @param sql SQL构建器可以使用{@link SqlBuilder#of(CharSequence)} 包装普通SQL
* @param page 分页对象
* @param rsh 结果集处理对象
* @return 结果对象
* @throws SQLException SQL执行异常
* @since 5.5.3
*/
public <T> T page(CharSequence sql, Page page, RsHandler<T> rsh) throws SQLException {
Connection conn = null;
try {
conn = this.getConnection();
return runner.page(conn, SqlBuilder.of(sql), page, rsh);
} finally {
this.closeConnection(conn);
}
}
/**
* 分页查询<br>
* @param <T> 结果对象类型
* @param sql SQL构建器可以使用{@link SqlBuilder#of(CharSequence)} 包装普通SQL
* @param page 分页对象
* @param rsh 结果集处理对象
* @param params 参数
* @return 结果对象
* @throws SQLException SQL执行异常
* @since 5.6.6
*/
public <T> T page(CharSequence sql, Page page, RsHandler<T> rsh,Object ...params) throws SQLException {
public <T> T page(CharSequence sql, Page page, RsHandler<T> 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> T page(SqlBuilder sql, Page page, RsHandler<T> 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<Entity> page(CharSequence sql, Page page ,Object ...params) throws SQLException {
public PageResult<Entity> page(CharSequence sql, Page page, Object... params) throws SQLException {
Connection conn = null;
try {
conn = this.getConnection();

View File

@ -280,18 +280,6 @@ public class SqlConnRunner extends DialectRunner {
return findAll(conn, Entity.create(tableName).set(field, values));
}
/**
* 获取查询结果总数生成类似于 SELECT count(1) from (sql) as _count
*
* @param conn 数据库连接对象
* @param selectSql 查询语句
* @return 结果数
* @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
*
@ -300,11 +288,12 @@ public class SqlConnRunner extends DialectRunner {
* @param params 查询参数
* @return 结果数
* @throws SQLException SQL异常
* @since 5.6.6
*/
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);
}