From 01ce0bff7fe011e87a43b887052b12cb06c63458 Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Tue, 4 Jul 2023 16:36:34 +0800
Subject: [PATCH 1/5] =?UTF-8?q?refactor:=20=E6=8F=90=E5=8F=96=20CountQuery?=
=?UTF-8?q?Column=20=E7=94=A8=E4=BA=8E=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6?=
=?UTF-8?q?=E4=BD=BF=E7=94=A8=20count=20=E6=9F=A5=E8=AF=A2=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../core/query/CountQueryColumn.java | 41 +++++++++++++++++++
1 file changed, 41 insertions(+)
create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CountQueryColumn.java
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
From d3a2fca176cc64a3e05af8828b5f5647e0b3ec05 Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Tue, 4 Jul 2023 16:36:55 +0800
Subject: [PATCH 2/5] =?UTF-8?q?refactor:=20=E4=BD=BF=E7=94=A8=20CountQuery?=
=?UTF-8?q?Column=20=E5=AF=B9=E8=B1=A1=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/com/mybatisflex/core/query/QueryMethods.java | 8 ++++----
1 file changed, 4 insertions(+), 4 deletions(-)
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));
}
From 6ee7dbb5ef4f594335b12b24aeae28c198d4f175 Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Tue, 4 Jul 2023 16:37:15 +0800
Subject: [PATCH 3/5] =?UTF-8?q?refactor:=20=E5=85=AC=E5=BC=80=20hasDistinc?=
=?UTF-8?q?t=20=E6=96=B9=E6=B3=95=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../src/main/java/com/mybatisflex/core/util/MapperUtil.java | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
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;
}
From 3a82710e22b2415593ec0be1a2e13944c254286c Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Tue, 4 Jul 2023 16:37:33 +0800
Subject: [PATCH 4/5] =?UTF-8?q?refactor:=20=E9=87=8D=E6=9E=84=20selectCoun?=
=?UTF-8?q?tByQuery=20=E6=96=B9=E6=B3=95=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../java/com/mybatisflex/core/BaseMapper.java | 24 +++++++++++++++----
1 file changed, 19 insertions(+), 5 deletions(-)
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
From ecb21420fe81f8a0fdac52369ead5a6f35fddfdf Mon Sep 17 00:00:00 2001
From: Suomm <1474983351@qq.com>
Date: Tue, 4 Jul 2023 16:39:00 +0800
Subject: [PATCH 5/5] =?UTF-8?q?test:=20=E6=B5=8B=E8=AF=95=20selectCountByQ?=
=?UTF-8?q?uery=20=E6=96=B9=E6=B3=95=E3=80=82?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
.../com/mybatisflex/test/entity/Outer.java | 3 ++-
.../com/mybatisflex/test/model/Account.java | 14 ++++++++++++-
.../src/main/resources/application.yml | 2 +-
.../test/mapper/AccountMapperTest.java | 21 ++++++++++++++++++-
4 files changed, 36 insertions(+), 4 deletions(-)
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();