mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
Merge remote-tracking branch 'gitee/main'
# Conflicts: # mybatis-flex-test/mybatis-flex-spring-boot-test/src/test/java/com/mybatisflex/test/common/QueryWrapperTest.java
This commit is contained in:
commit
cd23561a20
@ -978,15 +978,17 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
|
||||
// 可以省略表的引用,直接使用列名
|
||||
// SELECT 1
|
||||
// SELECT id FROM tb_user
|
||||
if (queryTables == null || queryTables.isEmpty() || queryTables.size() == 1) {
|
||||
if (queryTables == null || queryTables.isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
QueryTable consideredTable = queryTables.get(0);
|
||||
|
||||
// 列未指定表名,仅以字符串的形式输入列名
|
||||
// 以查询表中的第一个表为主
|
||||
// SELECT tb_user.id FROM tb_user
|
||||
if (selfTable == null) {
|
||||
return queryTables.get(0);
|
||||
return consideredTable;
|
||||
}
|
||||
|
||||
// 当前表有别名,以别名为主
|
||||
@ -995,7 +997,14 @@ public class QueryColumn implements CloneSupport<QueryColumn>, Conditional<Query
|
||||
return selfTable;
|
||||
}
|
||||
|
||||
QueryTable consideredTable = selfTable;
|
||||
// 当前表没有别名,查询表只有一个
|
||||
// 如果两个表是一样的则可以忽略表的引用
|
||||
// 兼容子查询时,子查询的查询表和父查询没有合并的问题
|
||||
if (queryTables.size() == 1 && Objects.equals(selfTable.name, consideredTable.name)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
consideredTable = selfTable;
|
||||
|
||||
// 当前表存在且没有别名
|
||||
ListIterator<QueryTable> it = queryTables.listIterator(queryTables.size());
|
||||
|
||||
@ -41,7 +41,7 @@ public class SelectQueryColumn extends QueryColumn implements HasParamsColumn {
|
||||
|
||||
@Override
|
||||
protected String toConditionSql(List<QueryTable> queryTables, IDialect dialect) {
|
||||
return dialect.forSelectByQuery(queryWrapper);
|
||||
return WrapperUtil.withBracket(dialect.forSelectByQuery(queryWrapper));
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -69,17 +69,20 @@ public class MapperUtil {
|
||||
.select(count().as("total"))
|
||||
.from(queryWrapper).as("t");
|
||||
}
|
||||
|
||||
public static QueryWrapper rawCountQueryWrapper(QueryWrapper queryWrapper, List<QueryColumn> customCountColumns) {
|
||||
return customCountColumns != null ? QueryWrapper.create()
|
||||
.select(customCountColumns)
|
||||
.from(queryWrapper).as("t") : rawCountQueryWrapper(queryWrapper);
|
||||
}
|
||||
|
||||
/**
|
||||
* 优化 COUNT 查询语句。
|
||||
*/
|
||||
public static QueryWrapper optimizeCountQueryWrapper(QueryWrapper queryWrapper) {
|
||||
return optimizeCountQueryWrapper(queryWrapper, Collections.singletonList(count().as("total")));
|
||||
}
|
||||
|
||||
/**
|
||||
* 优化 COUNT 查询语句。
|
||||
*/
|
||||
@ -108,7 +111,7 @@ public class MapperUtil {
|
||||
// 如果有 distinct、group by、having 等语句则不优化
|
||||
// 这种一旦优化了就会造成 count 语句查询出来的值不对
|
||||
if (hasDistinct(selectColumns) || hasGroupBy(groupByColumns) || havingCondition != null) {
|
||||
return clone;
|
||||
return rawCountQueryWrapper(clone);
|
||||
}
|
||||
// 判断能不能清除 join 语句
|
||||
if (canClearJoins(clone)) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user