mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-04-19 03:01:48 +08:00
hutool-db模块
1.page 使用自定义sql时,有时需要传递参数,为此添加方法 2.count 使用自定义sql时,有时需要传递参数,为此添加方法
This commit is contained in:
parent
e5049baa92
commit
dd29bde837
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询<br>
|
||||
@ -816,6 +847,44 @@ public abstract class AbstractDb implements Serializable {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询<br>
|
||||
* @param <T> 结果对象类型
|
||||
* @param sql SQL构建器,可以使用{@link SqlBuilder#of(CharSequence)} 包装普通SQL
|
||||
* @param page 分页对象
|
||||
* @param rsh 结果集处理对象
|
||||
* @param params 参数
|
||||
* @return 结果对象
|
||||
* @throws SQLException SQL执行异常
|
||||
*/
|
||||
public <T> T page(CharSequence sql, Page page, RsHandler<T> 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> T page(SqlBuilder sql, Page page, RsHandler<T> 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<Entity> 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);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 分页查询<br>
|
||||
* 查询条件为多个key value对表示,默认key = value,如果使用其它条件可以使用:where.put("key", " > 1"),value也可以传Condition对象,key被忽略
|
||||
|
@ -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();
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user