!107 优化 selectCountByQuery 方法

Merge pull request !107 from 王帅/main
This commit is contained in:
Michael Yang 2023-07-04 09:08:34 +00:00 committed by Gitee
commit 20c02ee92b
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
8 changed files with 101 additions and 14 deletions

View File

@ -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<T> {
default long selectCountByQuery(QueryWrapper queryWrapper) {
List<QueryColumn> selectColumns = CPI.getSelectColumns(queryWrapper);
try {
List<Object> 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<Object> objects = selectObjectListByQuery(queryWrapper);
return MapperUtil.getLongNumber(objects);
} finally {
//fixed https://github.com/mybatis-flex/mybatis-flex/issues/49

View File

@ -0,0 +1,41 @@
/*
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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);
}
}

View File

@ -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 <T> FunctionQueryColumn count(LambdaGetter<T> column) {
return new FunctionQueryColumn(COUNT, LambdaUtil.getQueryColumn(column));
return new CountQueryColumn(LambdaUtil.getQueryColumn(column));
}

View File

@ -80,7 +80,7 @@ public class MapperUtil {
return clone;
}
private static boolean hasDistinct(List<QueryColumn> selectColumns) {
public static boolean hasDistinct(List<QueryColumn> selectColumns) {
if (CollectionUtil.isEmpty(selectColumns)) {
return false;
}

View File

@ -55,7 +55,8 @@ public class Outer extends IdEntity<Integer> {
@Override
public String toString() {
return "Outer{" +
"name='" + name + '\'' +
"id='" + id + '\'' +
", name='" + name + '\'' +
", inner=" + inner +
'}';
}

View File

@ -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<String, Long, String> {
//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<String, Long, String> {
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<String, Long, String> {
", userName='" + userName + '\'' +
", age=" + age +
", birthday=" + birthday +
", roles=" + roles +
", isDelete=" + isDelete +
// ", roles=" + roles +
'}';
}
}

View File

@ -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

View File

@ -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();