mirror of
https://gitee.com/dromara/hutool.git
synced 2025-07-15 23:13:33 +08:00
fix count bug
This commit is contained in:
parent
7dc5873d9e
commit
d26feae36f
@ -15,6 +15,7 @@
|
|||||||
* 【extra 】 Sftp增加download重载(issue#I3VBSL@Gitee)
|
* 【extra 】 Sftp增加download重载(issue#I3VBSL@Gitee)
|
||||||
|
|
||||||
### 🐞Bug修复
|
### 🐞Bug修复
|
||||||
|
* 【db 】 修复count方法丢失参数问题(issue#I3VBSL@Gitee)
|
||||||
|
|
||||||
-------------------------------------------------------------------------------------------------------------
|
-------------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
@ -682,7 +682,7 @@ public abstract class AbstractDb implements Serializable {
|
|||||||
Connection conn = null;
|
Connection conn = null;
|
||||||
try {
|
try {
|
||||||
conn = this.getConnection();
|
conn = this.getConnection();
|
||||||
return runner.count(conn, sql.build(), sql.getParamValueArray());
|
return runner.count(conn, sql);
|
||||||
} finally {
|
} finally {
|
||||||
this.closeConnection(conn);
|
this.closeConnection(conn);
|
||||||
}
|
}
|
||||||
|
@ -280,6 +280,19 @@ public class SqlConnRunner extends DialectRunner {
|
|||||||
return findAll(conn, Entity.create(tableName).set(field, values));
|
return findAll(conn, Entity.create(tableName).set(field, values));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取查询结果总数,生成类似于 SELECT count(1) from (sql) as _count
|
||||||
|
*
|
||||||
|
* @param conn 数据库连接对象
|
||||||
|
* @param sqlBuilder SQL构建器,包括SQL和参数
|
||||||
|
* @return 结果数
|
||||||
|
* @throws SQLException SQL异常
|
||||||
|
* @since 5.7.0
|
||||||
|
*/
|
||||||
|
public long count(Connection conn, SqlBuilder sqlBuilder) throws SQLException {
|
||||||
|
return count(conn, sqlBuilder.build(), sqlBuilder.getParamValueArray());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取查询结果总数,生成类似于 SELECT count(1) from (sql) as _count
|
* 获取查询结果总数,生成类似于 SELECT count(1) from (sql) as _count
|
||||||
*
|
*
|
||||||
@ -337,7 +350,7 @@ public class SqlConnRunner extends DialectRunner {
|
|||||||
*/
|
*/
|
||||||
public PageResult<Entity> page(Connection conn, SqlBuilder sqlBuilder, Page page) throws SQLException {
|
public PageResult<Entity> page(Connection conn, SqlBuilder sqlBuilder, Page page) throws SQLException {
|
||||||
final PageResultHandler pageResultHandler = new PageResultHandler(
|
final PageResultHandler pageResultHandler = new PageResultHandler(
|
||||||
new PageResult<>(page.getPageNumber(), page.getPageSize(), (int) count(conn, sqlBuilder.build())),
|
new PageResult<>(page.getPageNumber(), page.getPageSize(), (int) count(conn, sqlBuilder)),
|
||||||
this.caseInsensitive);
|
this.caseInsensitive);
|
||||||
return page(conn, sqlBuilder, page, pageResultHandler);
|
return page(conn, sqlBuilder, page, pageResultHandler);
|
||||||
}
|
}
|
||||||
|
@ -50,6 +50,17 @@ public class DbTest {
|
|||||||
Assert.assertEquals(1, page1.size());
|
Assert.assertEquals(1, page1.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void pageWithParamsTest() throws SQLException {
|
||||||
|
String sql = "select * from user where name = ?";
|
||||||
|
PageResult<Entity> result = Db.use().page(
|
||||||
|
sql, Page.of(0, 3), "张三");
|
||||||
|
|
||||||
|
Assert.assertEquals(2, result.getTotal());
|
||||||
|
Assert.assertEquals(1, result.getTotalPage());
|
||||||
|
Assert.assertEquals(2, result.size());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void countTest() throws SQLException {
|
public void countTest() throws SQLException {
|
||||||
final long count = Db.use().count("select * from user");
|
final long count = Db.use().count("select * from user");
|
||||||
|
Loading…
Reference in New Issue
Block a user