This commit is contained in:
Looly 2022-05-01 00:02:15 +08:00
parent 4613d47c84
commit 514bb54ea5
3 changed files with 4 additions and 22 deletions

View File

@ -673,21 +673,10 @@ public abstract class AbstractDb<R extends AbstractDb<R>> implements ConnectionH
* @throws DbRuntimeException SQL执行异常 * @throws DbRuntimeException SQL执行异常
*/ */
public long count(final Entity where) throws DbRuntimeException { public long count(final Entity where) throws DbRuntimeException {
return count(where, null);
}
/**
* 结果的条目数
*
* @param where 查询条件
* @return 复合条件的结果数
* @throws DbRuntimeException SQL执行异常
*/
public long count(final Entity where, final Page page) throws DbRuntimeException {
Connection conn = null; Connection conn = null;
try { try {
conn = this.getConnection(); conn = this.getConnection();
return runner.count(conn, Query.of(where).setPage(page)); return runner.count(conn, Query.of(where));
} finally { } finally {
this.closeConnection(conn); this.closeConnection(conn);
} }

View File

@ -316,7 +316,9 @@ public class DialectRunner implements Serializable {
public PageResult<Entity> page(final Connection conn, final Query query) throws DbRuntimeException { public PageResult<Entity> page(final Connection conn, final Query query) throws DbRuntimeException {
final Page page = query.getPage(); final Page page = query.getPage();
final PageResultHandler pageResultHandler = new PageResultHandler( final PageResultHandler pageResultHandler = new PageResultHandler(
new PageResult<>(page.getPageNumber(), page.getPageSize(), (int) count(conn, query)), new PageResult<>(page.getPageNumber(), page.getPageSize(),
// 分页查询中总数的查询要去掉分页信息
(int) count(conn, query.clone().setPage(null))),
this.caseInsensitive); this.caseInsensitive);
return page(conn, query, pageResultHandler); return page(conn, query, pageResultHandler);
} }

View File

@ -41,15 +41,6 @@ public class DbTest {
Assert.assertEquals(1, page1.size()); Assert.assertEquals(1, page1.size());
} }
@Test
public void pageTest2() {
final long count = Db.of().count(Entity.create("user"), Page.of(1, 3));
Assert.assertEquals(1, count);
final List<Entity> page1 = Db.of().page(Entity.create("user"), Page.of(1, 3));
Assert.assertEquals(1, page1.size());
}
@Test @Test
public void pageBySqlTest() { public void pageBySqlTest() {
final String sql = "select * from user order by name"; final String sql = "select * from user order by name";