This commit is contained in:
Looly
2022-05-26 12:12:05 +08:00
parent 268f258537
commit 969a017f02
4 changed files with 15 additions and 10 deletions

View File

@@ -1,6 +1,5 @@
package cn.hutool.db.dialect;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import cn.hutool.db.sql.Order;
@@ -134,8 +133,7 @@ public interface Dialect extends Serializable {
* @throws SQLException SQL执行异常
*/
default PreparedStatement psForCount(Connection conn, Query query) throws SQLException {
query.setFields(ListUtil.toList("count(1)"));
return psForFind(conn, query);
return psForCount(conn, SqlBuilder.create().query(query));
}
/**

View File

@@ -70,6 +70,12 @@ public class DbTest {
Assert.assertEquals(4, count);
}
@Test
public void countByQueryTest() throws SQLException {
final long count = Db.use().count(Entity.create("user"));
Assert.assertEquals(4, count);
}
@Test
public void countTest2() throws SQLException {
final long count = Db.use().count("select * from user order by name DESC");