diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java index 3f26d1ec..7f79d93f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/BaseMapper.java @@ -20,10 +20,7 @@ import com.mybatisflex.core.field.FieldQueryBuilder; import com.mybatisflex.core.mybatis.MappedStatementTypes; import com.mybatisflex.core.paginate.Page; import com.mybatisflex.core.provider.EntitySqlProvider; -import com.mybatisflex.core.query.CPI; -import com.mybatisflex.core.query.QueryColumn; -import com.mybatisflex.core.query.QueryCondition; -import com.mybatisflex.core.query.QueryWrapper; +import com.mybatisflex.core.query.*; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.util.*; @@ -641,10 +638,27 @@ public interface BaseMapper { default long selectCountByQuery(QueryWrapper queryWrapper) { List selectColumns = CPI.getSelectColumns(queryWrapper); try { + List objects; if (CollectionUtil.isEmpty(selectColumns)) { + // 未设置 COUNT(...) 列,默认使用 COUNT(*) 查询 queryWrapper.select(count()); + objects = selectObjectListByQuery(queryWrapper); + } else if (selectColumns.get(0) instanceof CountQueryColumn) { + // 自定义 COUNT 函数,COUNT 函数必须在第一列 + // 可以使用 COUNT(1)、COUNT(列名) 代替默认的 COUNT(*) + objects = selectObjectListByQuery(queryWrapper); + } else { + // 查询列中的第一列不是 COUNT 函数 + if (MapperUtil.hasDistinct(selectColumns)) { + // 查询列中包含 DISTINCT 去重 + // 使用子查询 SELECT COUNT(*) FROM (SELECT DISTINCT ...) AS `t` + objects = selectObjectListByQuery(MapperUtil.rawCountQueryWrapper(queryWrapper)); + } else { + // 使用 COUNT(*) 替换所有的查询列 + queryWrapper.select(count()); + objects = selectObjectListByQuery(queryWrapper); + } } - List objects = selectObjectListByQuery(queryWrapper); return MapperUtil.getLongNumber(objects); } finally { //fixed https://github.com/mybatis-flex/mybatis-flex/issues/49 diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CountQueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CountQueryColumn.java new file mode 100644 index 00000000..a111241d --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CountQueryColumn.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.mybatisflex.core.query; + +import static com.mybatisflex.core.constant.FuncName.COUNT; + +/** + * COUNT 查询列。 + * + * @author 王帅 + * @since 2023-07-04 + */ +public class CountQueryColumn extends FunctionQueryColumn { + + public CountQueryColumn() { + super(COUNT, new StringQueryColumn("*")); + } + + public CountQueryColumn(String column) { + super(COUNT, column); + } + + public CountQueryColumn(QueryColumn column) { + super(COUNT, column); + } + +} \ No newline at end of file diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java index 10cc1395..379d1c60 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java @@ -2158,28 +2158,28 @@ public class QueryMethods { * 返回指定列的总行数。 */ public static FunctionQueryColumn count() { - return new FunctionQueryColumn(COUNT, new StringQueryColumn("*")); + return new CountQueryColumn(); } /** * 返回指定列的总行数。 */ public static FunctionQueryColumn count(String column) { - return new FunctionQueryColumn(COUNT, column); + return new CountQueryColumn(column); } /** * 返回指定列的总行数。 */ public static FunctionQueryColumn count(QueryColumn column) { - return new FunctionQueryColumn(COUNT, column); + return new CountQueryColumn(column); } /** * 返回指定列的总行数。 */ public static FunctionQueryColumn count(LambdaGetter column) { - return new FunctionQueryColumn(COUNT, LambdaUtil.getQueryColumn(column)); + return new CountQueryColumn(LambdaUtil.getQueryColumn(column)); } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java index fbf4bc1e..80f9114a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/MapperUtil.java @@ -80,7 +80,7 @@ public class MapperUtil { return clone; } - private static boolean hasDistinct(List selectColumns) { + public static boolean hasDistinct(List selectColumns) { if (CollectionUtil.isEmpty(selectColumns)) { return false; } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/entity/Outer.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/entity/Outer.java index 5a6285a1..df897d29 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/entity/Outer.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/entity/Outer.java @@ -55,7 +55,8 @@ public class Outer extends IdEntity { @Override public String toString() { return "Outer{" + - "name='" + name + '\'' + + "id='" + id + '\'' + + ", name='" + name + '\'' + ", inner=" + inner + '}'; } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java index 96d4b11a..333c721d 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/java/com/mybatisflex/test/model/Account.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.test.model; +import com.mybatisflex.annotation.Column; import com.mybatisflex.annotation.Table; import java.util.Date; @@ -27,6 +28,8 @@ public class Account extends BaseEntity { //private String userName; private Integer age; private Date birthday; + @Column(isLogicDelete = true) + private Boolean isDelete; // private Gender gender; // @@ -70,6 +73,14 @@ public class Account extends BaseEntity { this.birthday = birthday; } + public Boolean getDelete() { + return isDelete; + } + + public void setDelete(Boolean delete) { + isDelete = delete; + } + @Override public String toString() { return "Account{" + @@ -77,7 +88,8 @@ public class Account extends BaseEntity { ", userName='" + userName + '\'' + ", age=" + age + ", birthday=" + birthday + - ", roles=" + roles + + ", isDelete=" + isDelete + +// ", roles=" + roles + '}'; } } diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml index bb41ab6d..c8b88320 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/main/resources/application.yml @@ -7,7 +7,7 @@ spring: # driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/flex_test username: root - password: 123456 + password: 12345678 # driver-class-name: # datasource: # driver-class-name: org.h2.Driver diff --git a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java index 5bf20d67..b20b2e95 100644 --- a/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java +++ b/mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/mapper/AccountMapperTest.java @@ -31,7 +31,6 @@ import java.util.Date; import java.util.List; import static com.mybatisflex.core.query.QueryMethods.column; -import static com.mybatisflex.core.query.QueryMethods.concat; import static com.mybatisflex.test.model.table.AccountTableDef.ACCOUNT; import static com.mybatisflex.test.model.table.RoleTableDef.ROLE; import static com.mybatisflex.test.model.table.UserRoleTableDef.USER_ROLE; @@ -48,6 +47,26 @@ class AccountMapperTest { @Autowired private AccountMapper accountMapper; + @Test + void testCount() { + QueryWrapper queryWrapper = QueryWrapper.create() + .select() + .from(ACCOUNT) + .groupBy(ACCOUNT.AGE); + + long count = accountMapper.selectCountByQuery(queryWrapper); + + Assertions.assertEquals(2, count); + + queryWrapper = QueryWrapper.create() + .select(distinct(ACCOUNT.AGE)) + .from(ACCOUNT); + + count = accountMapper.selectCountByQuery(queryWrapper); + + Assertions.assertEquals(2, count); + } + @Test void testInsert() { Account account = new Account();