From 4158b5a3e0e56625cac3a32f574f821a4dc6a9f3 Mon Sep 17 00:00:00 2001
From: yulichang <570810310@qq.com>
Date: Fri, 6 Jan 2023 17:52:44 +0800
Subject: [PATCH] fix order by
---
README.md | 4 +-
mybatis-plus-join-annotation/pom.xml | 4 +-
mybatis-plus-join-boot-starter/pom.xml | 6 +-
mybatis-plus-join-core/pom.xml | 6 +-
.../config/MPJInterceptorConfig.java | 2 +-
.../yulichang/interceptor/MPJInterceptor.java | 5 +-
.../yulichang/toolkit/LogicInfoUtils.java | 2 +-
.../github/yulichang/toolkit/TableList.java | 123 +++++++++-
.../wrapper/MPJAbstractLambdaWrapper.java | 224 +++---------------
.../yulichang/wrapper/MPJAbstractWrapper.java | 102 ++++----
.../yulichang/wrapper/MPJLambdaWrapper.java | 65 ++---
.../yulichang/wrapper/enums/PrefixEnum.java | 5 +
.../yulichang/wrapper/interfaces/Query.java | 33 ++-
.../wrapper/interfaces/QueryJoin.java | 38 +--
.../wrapper/resultmap/MybatisLabel.java | 10 +-
.../yulichang/wrapper/segments/Select.java | 8 +-
.../wrapper/segments/SelectAlias.java | 10 +-
.../wrapper/segments/SelectFunc.java | 18 +-
.../wrapper/segments/SelectLabel.java | 14 +-
.../wrapper/segments/SelectNormal.java | 10 +-
.../wrapper/segments/SelectString.java | 20 +-
mybatis-plus-join-test/pom.xml | 18 +-
.../test-collection/pom.xml | 2 +-
mybatis-plus-join-test/test-join/pom.xml | 2 +-
.../yulichang/test/join/entity/UserDto.java | 26 ++
.../test/join/mapper/UserDTOMapper.java | 9 +
.../src/main/resources/db/schema.sql | 11 +
.../test/join/LambdaWrapperTest.java | 104 +++++---
mybatis-plus-join-test/test-mapping/pom.xml | 2 +-
mybatis-plus-join/pom.xml | 146 +-----------
pom.xml | 141 +----------
31 files changed, 491 insertions(+), 679 deletions(-)
create mode 100644 mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/PrefixEnum.java
create mode 100644 mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserDto.java
create mode 100644 mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserDTOMapper.java
diff --git a/README.md b/README.md
index 55407c6..a1851d5 100644
--- a/README.md
+++ b/README.md
@@ -18,12 +18,12 @@ QQ群:1022221898
com.github.yulichang
mybatis-plus-join-boot-starter
- 1.4.2
+ 1.4.2.2
```
- Gradle
```
- implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.4.2'
+ implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.4.2.2'
```
或者clone代码到本地执行 mvn install, 再引入以上依赖
diff --git a/mybatis-plus-join-annotation/pom.xml b/mybatis-plus-join-annotation/pom.xml
index cc2d8df..770e0ce 100644
--- a/mybatis-plus-join-annotation/pom.xml
+++ b/mybatis-plus-join-annotation/pom.xml
@@ -7,9 +7,9 @@
com.github.yulichang
mybatis-plus-join-root
- 1.4.2
+ 1.4.2.2
- 1.4.2
+ 1.4.2.2
mybatis-plus-join-annotation
mybatis-plus-join-annotation
diff --git a/mybatis-plus-join-boot-starter/pom.xml b/mybatis-plus-join-boot-starter/pom.xml
index 335e1e2..e983f5d 100644
--- a/mybatis-plus-join-boot-starter/pom.xml
+++ b/mybatis-plus-join-boot-starter/pom.xml
@@ -7,9 +7,9 @@
com.github.yulichang
mybatis-plus-join-root
- 1.4.2
+ 1.4.2.2
- 1.4.2
+ 1.4.2.2
mybatis-plus-join-boot-starter
mybatis-plus-join-boot-starter
@@ -47,7 +47,7 @@
com.github.yulichang
mybatis-plus-join-core
- 1.4.2
+ 1.4.2.2
org.springframework.boot
diff --git a/mybatis-plus-join-core/pom.xml b/mybatis-plus-join-core/pom.xml
index 5f86575..deed359 100644
--- a/mybatis-plus-join-core/pom.xml
+++ b/mybatis-plus-join-core/pom.xml
@@ -7,9 +7,9 @@
com.github.yulichang
mybatis-plus-join-root
- 1.4.2
+ 1.4.2.2
- 1.4.2
+ 1.4.2.2
mybatis-plus-join-core
mybatis-plus-join-core
@@ -47,7 +47,7 @@
com.github.yulichang
mybatis-plus-join-annotation
- 1.4.2
+ 1.4.2.2
com.baomidou
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java
index 5c49671..776b636 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java
@@ -29,7 +29,7 @@ public class MPJInterceptorConfig {
//打印banner
System.out.println(" _ _ |_ _ _|_. ___ _ | _ . _ . _ \n" +
"| | |\\/|_)(_| | |_\\ |_)||_|_\\ | (_) | | | \n" +
- " / | / 1.4.2");
+ " / | / 1.4.2.2");
}
}
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java
index 865a097..92e4a2f 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java
@@ -235,11 +235,12 @@ public class MPJInterceptor implements Interceptor {
String columnName = StringUtils.getTargetColumn(r.getSelectNormal().getColumn());
SelectLabel label;
Field field = ofTypeField.get(r.getProperty());
+ String index = mybatisLabel.getIndex();
if (columnSet.contains(columnName)) {
columnName = getColumn(columnSet, columnName, 0);
- label = new SelectLabel(r.getSelectNormal(), mybatisLabel.getIndex(), mybatisLabel.getOfType(), field, columnName);
+ label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, columnName, StringUtils.isNotBlank(index), index);
} else {
- label = new SelectLabel(r.getSelectNormal(), mybatisLabel.getIndex(), mybatisLabel.getOfType(), field);
+ label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, StringUtils.isNotBlank(index), index);
}
columnList.add(label);
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), r.getProperty(), columnName, r.getJavaType());
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java
index 1d2ff7c..ef447ae 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java
@@ -20,7 +20,7 @@ public class LogicInfoUtils implements Constants {
private static final Map, Map> LOGIC_CACHE = new ConcurrentHashMap<>();
- public static String getLogicInfo(String tableIndex, Class> clazz, boolean hasAlias, String alias) {
+ public static String getLogicInfo(Integer tableIndex, Class> clazz, boolean hasAlias, String alias) {
Map absent = LOGIC_CACHE.get(clazz);
if (absent == null) {
absent = new ConcurrentHashMap<>();
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/TableList.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/TableList.java
index c13a579..0b28dc6 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/TableList.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/TableList.java
@@ -1,10 +1,12 @@
package com.github.yulichang.toolkit;
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
import lombok.Data;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
+import java.util.stream.Collectors;
@Data
public class TableList {
@@ -19,6 +21,11 @@ public class TableList {
*/
private Class> rootClass;
+ /**
+ * 主表别名
+ */
+ private String alias;
+
/**
* 关联表
*/
@@ -45,19 +52,127 @@ public class TableList {
}
}
- public String getPrefix(int index) {
- return null;
+ public String getPrefix(Integer index, Class> clazz, boolean isLabel) {
+ if (Objects.isNull(index)) {
+ if (!isLabel) {
+ if (rootClass == clazz) {
+ return alias;
+ }
+ }
+ Node node = getByClassFirst(clazz);
+ if (Objects.isNull(node)) {
+ return alias;
+ }
+ return node.isHasAlias() ? node.getAlias() : (node.getAlias() + node.getIndex());
+ }
+ Node node = getByIndex(index);
+ if (node.getClazz() != clazz) {
+ Node dg = dg(node, clazz);
+ if (Objects.nonNull(dg)) {
+ node = dg;
+ } else {
+ if (rootClass != clazz) {
+ List list = getByClass(clazz);
+ for (int i = list.size() - 1; i >= 0; i--) {
+ Node n = list.get(i);
+ if (n.getClazz() == clazz) {
+ return n.hasAlias ? n.getAlias() : (n.getAlias() + n.getIndex());
+ }
+ }
+ }
+ return alias;
+ }
+ }
+ if (node.hasAlias) {
+ return node.getAlias();
+ } else {
+ return node.getAlias() + node.getIndex();
+ }
}
- public String getPrefixOther(int index) {
- return null;
+ public String getPrefixOther(Integer index, Class> clazz) {
+ if (Objects.isNull(index)) {
+ List list = getByClass(clazz);
+ if (list.size() == 1 && list.get(0).getClazz() == clazz) {
+ return alias;
+ } else if (list.size() > 1) {
+ for (Node n : list) {
+ if (n.getClazz() == clazz) {
+ return n.isHasAlias() ? n.getAlias() : (n.getAlias() + n.getIndex());
+ }
+ }
+ return alias;
+ } else {
+ return alias;
+ }
+ }
+ Node node = getByIndex(index);
+ Node dg = dg(node, node.getClazz());
+ if (Objects.nonNull(dg)) {
+ return dg.hasAlias ? dg.alias : (dg.alias + dg.getIndex());
+ }
+ if (Objects.equals(rootClass, node.getClazz())) {
+ return alias;
+ } else {
+ List list = getByClass(node.getClazz());
+ if (list.size() == 1) {
+ Node n = list.get(0);
+ if (n.getClazz() == node.getClazz()) {
+ return alias;
+ } else {
+ return n.isHasAlias() ? n.getAlias() : (n.getAlias() + n.getIndex());
+ }
+ } else if (list.size() > 1) {
+ for (Node n : list) {
+ if (n.getClazz() != node.getClazz()) {
+ return n.isHasAlias() ? n.getAlias() : (n.getAlias() + n.getIndex());
+ }
+ }
+ return alias;
+ } else {
+ return alias;
+ }
+ }
}
+ private Node dg(Node node, Class> clazz) {
+ if (Objects.isNull(node.getPIndex())) {
+ return null;
+ } else {
+ Node pNode = getByIndex(node.getPIndex());
+ if (pNode.getClazz() == clazz) {
+ return pNode;
+ }
+ return dg(pNode, clazz);
+ }
+ }
+
+ public String getPrefixByClass(Class> clazz) {
+ Node node = getByClassFirst(clazz);
+ if (Objects.isNull(node)) {
+ return alias;
+ } else {
+ return node.hasAlias ? node.getAlias() : (node.getAlias() + StringPool.DOT + node.getIndex());
+ }
+ }
private Node getByIndex(int index) {
return all.stream().filter(i -> i.getIndex() == index).findFirst().orElse(null);
}
+ private Node getByClassFirst(Class> clazz) {
+ return all.stream().filter(i -> i.getClazz() == clazz).findFirst().orElse(null);
+ }
+
+ private List getByClass(Class> clazz) {
+ return all.stream().filter(i -> i.getClazz() == clazz).collect(Collectors.toList());
+ }
+
+ public void clear() {
+ this.all.clear();
+ this.child.clear();
+ }
+
@Data
public static class Node {
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java
index 9d6c125..4322cdf 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java
@@ -6,17 +6,16 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.toolkit.LambdaUtils;
+import com.github.yulichang.toolkit.TableList;
import com.github.yulichang.toolkit.support.ColumnCache;
-import com.github.yulichang.wrapper.segments.Select;
+import com.github.yulichang.wrapper.enums.PrefixEnum;
import com.github.yulichang.wrapper.segments.SelectCache;
-import lombok.AllArgsConstructor;
-import lombok.Data;
-import lombok.EqualsAndHashCode;
import lombok.Getter;
-import java.util.*;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
-import java.util.stream.Stream;
import static java.util.stream.Collectors.joining;
@@ -42,27 +41,22 @@ public abstract class MPJAbstractLambdaWrapper String columnToString(String index, int node, X column, boolean isJoin, Class> parent) {
- return columnToString(index, node, (SFunction, ?>) column, isJoin, parent);
+ protected String columnToString(Integer index, X column, boolean isJoin, PrefixEnum prefixEnum) {
+ return columnToString(index, (SFunction, ?>) column, isJoin, prefixEnum);
}
@Override
@SafeVarargs
- protected final String columnsToString(String index, int node, boolean isJoin, Class> parent, X... columns) {
- return Arrays.stream(columns).map(i -> columnToString(index, node, (SFunction, ?>) i, isJoin, parent)).collect(joining(StringPool.COMMA));
+ protected final String columnsToString(Integer index, boolean isJoin, PrefixEnum prefixEnum, X... columns) {
+ return Arrays.stream(columns).map(i -> columnToString(index, (SFunction, ?>) i, isJoin, prefixEnum)).collect(joining(StringPool.COMMA));
}
- protected String columnToString(String index, int node, SFunction, ?> column, boolean isJoin, Class> parent) {
+ protected String columnToString(Integer index, SFunction, ?> column, boolean isJoin, PrefixEnum prefixEnum) {
Class> entityClass = LambdaUtils.getEntityClass(column);
- return getDefault(index, node, entityClass, isJoin, parent) + StringPool.DOT +
- getCache(column).getTagColumn();
+ return getDefault(index, entityClass, isJoin, prefixEnum) + StringPool.DOT + getCache(column).getTagColumn();
}
protected SelectCache getCache(SFunction, ?> fn) {
@@ -74,184 +68,26 @@ public abstract class MPJAbstractLambdaWrapper clazz, boolean isJoin, Class> parent) {
- //外层where条件
- if (Objects.isNull(index)) {
- if (!isJoin && Objects.equals(clazz, getEntityClass())) {
- return this.alias;
- }
- //正序
- Table table = tableList.getPositive(clazz);
- return table.hasAlias ? table.alias : (table.alias + (Objects.isNull(table.index) ? StringPool.EMPTY : table.index));
- }
- Table table = tableList.get(clazz, index);
- if (table.hasAlias) {
- return table.alias;
- }
- if (Objects.nonNull(table.getIndex())) {
- if (isJoin && (Objects.equals(clazz, getEntityClass()) || Objects.equals(parent, clazz))) {
- if (node == -1) {
- return table.alias;
- } else if (node == 0) {
- //除自己以外的倒序第一个
- Table t = tableList.getOrElse(clazz, index);
- if (Objects.isNull(t.getIndex())) {
- return t.alias;
- }
- return t.alias + t.getIndex();
- } else {
- return table.alias + node;
- }
- }
- return table.alias + table.getIndex();
- }
- return table.alias;
- }
-
- protected String getDefaultSelect(String index, Class> clazz, Select s) {
- if (s.isLabel()) {
- if (Objects.nonNull(s.getIndex())) {
- return s.getIndex();
- } else {
- Table table = tableList.get(s.getClazz());
- return Objects.isNull(table.index) ? StringPool.EMPTY : table.index;
- }
- }
- //外层select
- if (Objects.isNull(index)) {
- if (Objects.equals(clazz, getEntityClass())) {
- return StringPool.EMPTY;
- }
- //正序
- Table table = tableList.getPositive(clazz);
- return Objects.isNull(table.index) ? StringPool.EMPTY : table.index;
- }
- Table table = tableList.get(clazz, index);
- if (Objects.nonNull(table.getIndex())) {
- return table.getIndex();
- }
- return StringPool.EMPTY;
- }
-
- public static class TableList {
-
- private final Table DEFAULT_TABLE;
-
- public TableList(Class> clazz, String index, String alias) {
- DEFAULT_TABLE = new Table(clazz, index, false, alias);
- }
-
- private final List list = new ArrayList<>();
-
- public void add(Class> clazz, String index, boolean hasAlias, String alias) {
- this.list.add(new Table(clazz, index, hasAlias, alias));
- }
-
- public Table get(Class> clazz) {
- if (list.isEmpty()) {
- return DEFAULT_TABLE;
- }
- for (int i = list.size() - 1; i >= 0; i--) {
- Table t = list.get(i);
- if (clazz == t.clazz) {
- return t;
- }
- }
- return DEFAULT_TABLE;
- }
-
-
- public Table get(Class> clazz, String index) {
- if (Objects.isNull(index)) {
- return get(clazz);
- }
- if (list.isEmpty()) {
- return DEFAULT_TABLE;
- }
- //倒序
- for (int i = list.size() - 1; i >= 0; i--) {
- Table t = list.get(i);
- if (clazz == t.clazz && Objects.equals(index, t.getIndex())) {
- return t;
- }
- }
- return get(clazz);
- }
-
- @SuppressWarnings("unused")
- public Table getPositive(Class> clazz, String index) {
- if (Objects.isNull(index)) {
- return get(clazz);
- }
- if (list.isEmpty()) {
- return DEFAULT_TABLE;
- }
- for (Table t : list) {
- if (clazz == t.clazz && Objects.equals(index, t.getIndex())) {
- return t;
- }
- }
- return getPositive(clazz);
- }
-
- public Table getPositive(Class> clazz) {
- if (list.isEmpty()) {
- return DEFAULT_TABLE;
- }
- for (Table t : list) {
- if (clazz == t.clazz) {
- return t;
- }
- }
- return DEFAULT_TABLE;
- }
-
- public Stream stream() {
- return list.stream();
- }
-
- public void clear() {
- list.clear();
- }
-
- public boolean isEmpty() {
- return list.isEmpty();
- }
-
- public Table getOrElse(Class> clazz, String index) {
- if (Objects.isNull(index)) {
- return get(clazz);
- }
- //倒序
- for (int i = list.size() - 1; i >= 0; i--) {
- Table t = list.get(i);
- if (clazz == t.clazz) {
- if (Objects.equals(index, t.getIndex())) {
- continue;
- }
- if (Integer.parseInt(t.getIndex()) < Integer.parseInt(index)) {
- return t;
- }
- }
- }
- return DEFAULT_TABLE;
+ protected String getDefault(Integer index, Class> clazz, boolean isJoin, PrefixEnum prefixEnum) {
+ if (prefixEnum == PrefixEnum.ON_FIRST) {
+ return tableList.getPrefix(index, clazz, false);
+ } else if (prefixEnum == PrefixEnum.ON_SECOND) {
+ return isJoin ? tableList.getPrefixOther(index, clazz) : tableList.getPrefix(index, clazz, false);
+ } else if (prefixEnum == PrefixEnum.CD_FIRST) {
+ return tableList.getPrefix(index, clazz, false);
+ } else if (prefixEnum == PrefixEnum.CD_SECOND) {
+ return isJoin ? tableList.getPrefixOther(index, clazz) :
+ tableList.getPrefix(index, clazz, false);
+ } else if (prefixEnum == PrefixEnum.CD_ON_FIRST) {
+ return tableList.getPrefix(index, clazz, false);
+ } else if (prefixEnum == PrefixEnum.CD_ON_SECOND) {
+ return isJoin ? tableList.getPrefixOther(index, clazz) :
+ tableList.getPrefix(index, clazz, false);
+ } else {
+ return tableList.getAlias();
}
}
- @Data
- @EqualsAndHashCode
- @AllArgsConstructor
- public static class Table {
- private final Class> clazz;
-
- private final String index;
-
- private boolean hasAlias;
-
- private final String alias;
- }
-
-
/**
* 必要的初始化
*/
@@ -262,7 +98,7 @@ public abstract class MPJAbstractLambdaWrapper, Nested, Join, Func, OnCompare,
CompareStr, FuncStr {
- protected static final Node ROOT_NODE = new Node(null, 0, null);
-
/**
* 占位符
*/
@@ -46,7 +46,7 @@ public abstract class MPJAbstractWrapper joinClass;
+ /**
+ * 主表wrapper
+ */
+ protected boolean isMain = true;
+ /**
+ * 是否是OnWrapper
+ */
+ protected boolean isNo = false;
/**
- * 寻路
+ * 关联的表
*/
- protected Node node;
+ protected TableList tableList;
@Override
public T getEntity() {
@@ -113,6 +121,9 @@ public abstract class MPJAbstractWrapper i.setEntityClass(entityClass));
this.entityClass = entityClass;
}
+ if (tableList != null) {
+ tableList.setRootClass(entityClass);
+ }
return typedThis;
}
@@ -349,7 +360,7 @@ public abstract class MPJAbstractWrapper Children groupBy(boolean condition, List> columns) {
return maybeDo(condition, () -> {
if (CollectionUtils.isNotEmpty(columns)) {
- String one = (StringPool.COMMA + columnsToString(index, getByClass(node, joinClass), false, parentClass(node), columns));
+ String one = (StringPool.COMMA + columnsToString(index, false, isNo ? PrefixEnum.ON_FIRST : PrefixEnum.CD_FIRST, columns));
final String finalOne = one;
appendSqlSegments(GROUP_BY, () -> finalOne);
}
@@ -359,9 +370,9 @@ public abstract class MPJAbstractWrapper Children groupBy(boolean condition, SFunction column, SFunction... columns) {
return maybeDo(condition, () -> {
- String one = columnToString(index, getByClass(node, joinClass), column, false, parentClass(node));
+ String one = columnToString(index, column, false, isNo ? PrefixEnum.ON_FIRST : PrefixEnum.CD_FIRST);
if (ArrayUtils.isNotEmpty(columns)) {
- one += (StringPool.COMMA + columnsToString(index, getByClass(node, joinClass), false, parentClass(node), columns));
+ one += (StringPool.COMMA + columnsToString(index, false, isNo ? PrefixEnum.ON_FIRST : PrefixEnum.CD_FIRST, columns));
}
final String finalOne = one;
appendSqlSegments(GROUP_BY, () -> finalOne);
@@ -467,8 +478,13 @@ public abstract class MPJAbstractWrapper Children addCondition(boolean condition, SFunction column, SqlKeyword sqlKeyword, SFunction val) {
+ Class c = LambdaUtils.getEntityClass(column);
+ Class v = LambdaUtils.getEntityClass(val);
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(index, column, false), sqlKeyword,
- columnToSqlSegment(index, val, true)));
+ isNo ?
+ columnToSqlSegmentS(index, val, v == c && v == joinClass) :
+ columnToSqlSegmentS(index, val, v == c)
+ ));
}
protected Children addCondition(boolean condition, String column, SqlKeyword sqlKeyword, Object val) {
@@ -643,8 +659,24 @@ public abstract class MPJAbstractWrapper ISqlSegment columnToSqlSegment(String index, SFunction column, boolean isJoin) {
- return () -> columnToString(index, getByClass(node, joinClass), column, isJoin, parentClass(node));
+ protected final ISqlSegment columnToSqlSegment(Integer index, SFunction column, boolean isJoin) {
+ PrefixEnum prefixEnum;
+ if (isMain) {
+ prefixEnum = isNo ? PrefixEnum.ON_FIRST /* 理论上不可能有这种情况 */ : PrefixEnum.CD_FIRST;
+ } else {
+ prefixEnum = isNo ? PrefixEnum.ON_FIRST : PrefixEnum.CD_ON_FIRST;
+ }
+ return () -> columnToString(index, column, isJoin, isNo ? PrefixEnum.ON_FIRST : PrefixEnum.CD_FIRST);
+ }
+
+ protected final ISqlSegment columnToSqlSegmentS(Integer index, SFunction column, boolean isJoin) {
+ PrefixEnum prefixEnum;
+ if (isMain) {
+ prefixEnum = isNo ? PrefixEnum.ON_SECOND /* 理论上不可能有这种情况 */ : PrefixEnum.CD_SECOND;
+ } else {
+ prefixEnum = isNo ? PrefixEnum.ON_SECOND : PrefixEnum.CD_ON_SECOND;
+ }
+ return () -> columnToString(index, column, isJoin, prefixEnum);
}
protected final ISqlSegment columnToSqlSegment(String column) {
@@ -654,7 +686,7 @@ public abstract class MPJAbstractWrapper String columnToString(String index, int node, X column, boolean isJoin, Class> parent) {
+ protected String columnToString(Integer index, X column, boolean isJoin, PrefixEnum prefixEnum) {
return (String) column;
}
@@ -676,8 +708,8 @@ public abstract class MPJAbstractWrapper String columnsToString(String index, int node, boolean isJoin, Class> parent, X... columns) {
- return Arrays.stream(columns).map(i -> this.columnToString(index, node, i, isJoin, parent)).collect(joining(StringPool.COMMA));
+ protected String columnsToString(Integer index, boolean isJoin, PrefixEnum prefixEnum, X... columns) {
+ return Arrays.stream(columns).map(i -> this.columnToString(index, i, isJoin, prefixEnum)).collect(joining(StringPool.COMMA));
}
@Override
@@ -727,46 +759,6 @@ public abstract class MPJAbstractWrapper clazz;
-
- private int index;
-
- private Node parent;
- }
-
-
- private int getByClass(Node node, Class> joinClass) {
- if (joinClass == null) {
- return 0;
- }
- if (node.parent != null) {
- return dg(node.parent, joinClass);
- }
- return 0;
- }
-
- private Class> parentClass(Node node) {
- if (node == null || node.parent == null) {
- return null;
- }
- return node.parent.clazz;
- }
-
- private int dg(Node node, Class> joinClass) {
-
- if (node.clazz != null && node.clazz == joinClass) {
- return node.index;
- }
- if (node.parent == null) {
- return joinClass == getEntityClass() ? -1 : 0;
- }
- return getByClass(node.parent, joinClass);
- }
-
/* ****************************************** **/
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java
index a862f21..76322dc 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java
@@ -6,10 +6,8 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.config.ConfigProperties;
-import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.LambdaUtils;
-import com.github.yulichang.toolkit.LogicInfoUtils;
-import com.github.yulichang.toolkit.TableHelper;
+import com.github.yulichang.toolkit.*;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.wrapper.interfaces.Query;
import com.github.yulichang.wrapper.interfaces.QueryJoin;
@@ -52,6 +50,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper clazz) {
super.initNeed();
setEntityClass(clazz);
+ tableList.setRootClass(clazz);
}
/**
@@ -96,6 +96,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
Map paramNameValuePairs, MergeSegments mergeSegments,
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
- TableList tableList, String index, String keyWord, Class> joinClass, Node node) {
+ TableList tableList, Integer index, String keyWord, Class> joinClass) {
super.setEntity(entity);
super.setEntityClass(entityClass);
this.paramNameSeq = paramNameSeq;
@@ -128,7 +131,6 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper cacheMap = ColumnCache.getMapField(aClass);
for (SFunction s : columns) {
SelectCache cache = cacheMap.get(LambdaUtils.getName(s));
- getSelectColum().add(new SelectNormal(cache, index));
+ getSelectColum().add(new SelectNormal(cache, index, hasAlias, alias));
}
}
return typedThis;
@@ -188,17 +190,17 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper[] args = i.getArgs();
if (Objects.isNull(args) || args.length == 0) {
@@ -206,11 +208,10 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper {
Class> entityClass = LambdaUtils.getEntityClass(arg);
- Table table = tableList.getPositive(entityClass);
- Assert.notNull(table, "table not find by class <%s>", entityClass.getSimpleName());
+ String prefixByClass = tableList.getPrefixByClass(entityClass);
Map mapField = ColumnCache.getMapField(entityClass);
SelectCache cache = mapField.get(LambdaUtils.getName(arg));
- return tableList.get(cache.getClazz()).getAlias() + (Objects.isNull(table.getIndex()) ? StringPool.EMPTY : table.getIndex()) + StringPool.DOT + cache.getColumn();
+ return prefixByClass + StringPool.DOT + cache.getColumn();
}).toArray()) + Constant.AS + i.getAlias();
}
} else {
@@ -238,7 +239,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper instance() {
- return instance(index, null, null, this.node);
+ return instance(index, null, null);
}
- protected MPJLambdaWrapper instance(String index, String keyWord, Class> joinClass, Node node) {
+ protected MPJLambdaWrapper instance(Integer index, String keyWord, Class> joinClass) {
return new MPJLambdaWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(),
- this.tableList, index, keyWord, joinClass, node);
+ this.tableList, index, keyWord, joinClass);
}
@Override
@@ -322,10 +323,10 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper LogicInfoUtils.getLogicInfo(t.getIndex(),
+ return tableList.getAll().stream().map(t -> LogicInfoUtils.getLogicInfo(t.getIndex(),
t.getClazz(), t.isHasAlias(), t.getAlias())).collect(Collectors.joining(StringPool.SPACE));
}
return StringPool.EMPTY;
@@ -343,24 +344,26 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper join(String keyWord, Class clazz, String tableAlias, BiConsumer, MPJLambdaWrapper> consumer) {
- String oldIndex = this.getIndex();
- String newIndex = String.valueOf(tableIndex);
- Node n = Objects.isNull(oldIndex) ? new Node(clazz, tableIndex, ROOT_NODE) : new Node(clazz, tableIndex, this.node);
- MPJLambdaWrapper instance = instance(newIndex, keyWord, clazz, n);
- this.node = n;
+ Integer oldIndex = this.getIndex();
+ int newIndex = tableIndex;
+ MPJLambdaWrapper instance = instance(newIndex, keyWord, clazz);
+ instance.isNo = true;
onWrappers.add(instance);
if (StringUtils.isBlank(tableAlias)) {
- tableList.add(clazz, newIndex, false, ConfigProperties.tableAlias);
+ tableList.put(oldIndex, clazz, false, ConfigProperties.tableAlias, newIndex);
instance.alias = ConfigProperties.tableAlias;
instance.hasAlias = false;
- tableIndex++;
} else {
- tableList.add(clazz, null, true, tableAlias);
+ tableList.put(oldIndex, clazz, true, tableAlias, newIndex);
instance.alias = tableAlias;
instance.hasAlias = true;
}
+ tableIndex++;
this.index = newIndex;
+ boolean isM = this.isMain;
+ this.isMain = false;
consumer.accept(instance, typedThis);
+ this.isMain = isM;
this.index = oldIndex;
return typedThis;
}
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/PrefixEnum.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/PrefixEnum.java
new file mode 100644
index 0000000..f251603
--- /dev/null
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/PrefixEnum.java
@@ -0,0 +1,5 @@
+package com.github.yulichang.wrapper.enums;
+
+public enum PrefixEnum {
+ SELECT, ON_FIRST, ON_SECOND, CD_ON_FIRST, CD_ON_SECOND, CD_FIRST, CD_SECOND
+}
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java
index 7fbcef9..63eaeb6 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java
@@ -35,7 +35,11 @@ public interface Query extends Serializable {
Children getChildren();
- String getIndex();
+ Integer getIndex();
+
+ boolean isHasAlias();
+
+ String getAlias();
/**
@@ -54,7 +58,7 @@ public interface Query extends Serializable {
Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName());
Map cacheMap = ColumnCache.getMapField(entityClass);
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
- i -> getSelectColum().add(new SelectNormal(cacheMap.get(i.getProperty()), getIndex())));
+ i -> getSelectColum().add(new SelectNormal(cacheMap.get(i.getProperty()), getIndex(), isHasAlias(), getAlias())));
return getChildren();
}
@@ -68,7 +72,7 @@ public interface Query extends Serializable {
* @param columns 列
*/
default Children select(String... columns) {
- getSelectColum().addAll(Arrays.stream(columns).map(SelectString::new).collect(Collectors.toList()));
+ getSelectColum().addAll(Arrays.stream(columns).map(i -> new SelectString(i, isHasAlias(), getAlias())).collect(Collectors.toList()));
return getChildren();
}
@@ -78,7 +82,7 @@ public interface Query extends Serializable {
* @param column 列
*/
default Children select(String column, SFunction alias) {
- getSelectColum().add(new SelectString(column + Constants.AS + LambdaUtils.getName(alias)));
+ getSelectColum().add(new SelectString(column + Constants.AS + LambdaUtils.getName(alias), isHasAlias(), getAlias()));
return getChildren();
}
@@ -90,7 +94,9 @@ public interface Query extends Serializable {
default Children select(String index, SFunction column, SFunction alias) {
Map cacheMap = ColumnCache.getMapField(LambdaUtils.getEntityClass(column));
SelectCache cache = cacheMap.get(LambdaUtils.getName(column));
- getSelectColum().add(new SelectString(index + Constants.DOT + cache.getColumn() + Constants.AS + LambdaUtils.getName(alias)));
+ getSelectColum().add(new SelectString(
+ index + Constants.DOT + cache.getColumn() + Constants.AS + LambdaUtils.getName(alias),
+ isHasAlias(), getAlias()));
return getChildren();
}
@@ -109,7 +115,7 @@ public interface Query extends Serializable {
Map fieldMap = MPJReflectionKit.getFieldMap(tag);
for (SelectCache cache : normalList) {
if (fieldMap.containsKey(cache.getColumProperty())) {
- getSelectColum().add(new SelectNormal(cache, getIndex()));
+ getSelectColum().add(new SelectNormal(cache, getIndex(), isHasAlias(), getAlias()));
}
}
return getChildren();
@@ -128,8 +134,7 @@ public interface Query extends Serializable {
default Children selectAs(SFunction column, String alias) {
Class> aClass = LambdaUtils.getEntityClass(column);
Map cacheMap = ColumnCache.getMapField(aClass);
- String index = getIndex();
- getSelectColum().add(new SelectAlias(cacheMap.get(LambdaUtils.getName(column)), getIndex(), alias));
+ getSelectColum().add(new SelectAlias(cacheMap.get(LambdaUtils.getName(column)), getIndex(), alias, isHasAlias(), getAlias()));
return getChildren();
}
@@ -139,7 +144,7 @@ public interface Query extends Serializable {
*/
default Children selectAll(Class> clazz) {
getSelectColum().addAll(ColumnCache.getListField(clazz).stream().map(i ->
- new SelectNormal(i, getIndex())).collect(Collectors.toList()));
+ new SelectNormal(i, getIndex(), isHasAlias(), getAlias())).collect(Collectors.toList()));
return getChildren();
}
@@ -161,14 +166,14 @@ public interface Query extends Serializable {
* @param alias 别名
*/
default Children selectFunc(BaseFuncEnum funcEnum, Object column, String alias) {
- getSelectColum().add(new SelectFunc(alias, getIndex(), funcEnum, column.toString()));
+ getSelectColum().add(new SelectFunc(alias, getIndex(), funcEnum, column.toString(), isHasAlias(), getAlias()));
return getChildren();
}
default Children selectFunc(BaseFuncEnum funcEnum, SFunction column, String alias) {
Class> aClass = LambdaUtils.getEntityClass(column);
Map cacheMap = ColumnCache.getMapField(aClass);
- getSelectColum().add(new SelectFunc(cacheMap.get(LambdaUtils.getName(column)), getIndex(), alias, funcEnum));
+ getSelectColum().add(new SelectFunc(cacheMap.get(LambdaUtils.getName(column)), getIndex(), alias, funcEnum, isHasAlias(), getAlias()));
return getChildren();
}
@@ -186,12 +191,14 @@ public interface Query extends Serializable {
default Children selectFunc(String sql, Function[]> column, String alias) {
- getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(new SelectFunc.Func())));
+ getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(new SelectFunc.Func()),
+ isHasAlias(), getAlias()));
return getChildren();
}
default Children selectFunc(String sql, Function[]> column, SFunction alias) {
- getSelectColum().add(new SelectFunc(LambdaUtils.getName(alias), getIndex(), () -> sql, column.apply(new SelectFunc.Func())));
+ getSelectColum().add(new SelectFunc(LambdaUtils.getName(alias), getIndex(), () -> sql,
+ column.apply(new SelectFunc.Func()), isHasAlias(), getAlias()));
return getChildren();
}
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java
index d933773..8d09e45 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java
@@ -69,7 +69,7 @@ public interface QueryJoin extends MPJBaseJoin, String
* @param right 条件
*/
default Children leftJoin(Class clazz, String alias, SFunction left, SFunction right) {
- return join(Constant.LEFT_JOIN, clazz,alias, left, right);
+ return join(Constant.LEFT_JOIN, clazz, alias, left, right);
}
/**
@@ -81,7 +81,7 @@ public interface QueryJoin extends MPJBaseJoin, String
* @param function 条件
*/
default Children leftJoin(Class clazz, String alias, WrapperFunction> function) {
- return join(Constant.LEFT_JOIN, clazz,alias, function);
+ return join(Constant.LEFT_JOIN, clazz, alias, function);
}
/**
@@ -92,7 +92,7 @@ public interface QueryJoin extends MPJBaseJoin, String
* @param right 条件
*/
default Children leftJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) {
- return join(Constant.LEFT_JOIN, clazz,alias, left, right, ext);
+ return join(Constant.LEFT_JOIN, clazz, alias, left, right, ext);
}
/**
@@ -104,7 +104,7 @@ public interface QueryJoin extends MPJBaseJoin, String
* @param consumer 条件
*/
default Children leftJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) {
- return join(Constant.LEFT_JOIN, clazz,alias, consumer);
+ return join(Constant.LEFT_JOIN, clazz, alias, consumer);
}
/**
@@ -139,28 +139,28 @@ public interface QueryJoin extends MPJBaseJoin, String
* ignore 参考 left join
*/
default Children rightJoin(Class clazz, String alias, SFunction left, SFunction right) {
- return join(Constant.RIGHT_JOIN, clazz,alias, left, right);
+ return join(Constant.RIGHT_JOIN, clazz, alias, left, right);
}
/**
* ignore 参考 left join
*/
default Children rightJoin(Class clazz, String alias, WrapperFunction> function) {
- return join(Constant.RIGHT_JOIN, clazz,alias, function);
+ return join(Constant.RIGHT_JOIN, clazz, alias, function);
}
/**
* ignore 参考 left join
*/
default Children rightJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) {
- return join(Constant.RIGHT_JOIN, clazz,alias, left, right, ext);
+ return join(Constant.RIGHT_JOIN, clazz, alias, left, right, ext);
}
/**
* ignore 参考 left join
*/
default Children rightJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) {
- return join(Constant.RIGHT_JOIN, clazz,alias, consumer);
+ return join(Constant.RIGHT_JOIN, clazz, alias, consumer);
}
@@ -197,28 +197,28 @@ public interface QueryJoin extends MPJBaseJoin, String
* ignore 参考 left join
*/
default Children innerJoin(Class clazz, String alias, SFunction left, SFunction right) {
- return join(Constant.INNER_JOIN, clazz,alias, on -> on.eq(left, right));
+ return join(Constant.INNER_JOIN, clazz, alias, on -> on.eq(left, right));
}
/**
* ignore 参考 left join
*/
default Children innerJoin(Class clazz, String alias, WrapperFunction> function) {
- return join(Constant.INNER_JOIN, clazz,alias, function);
+ return join(Constant.INNER_JOIN, clazz, alias, function);
}
/**
* ignore 参考 left join
*/
default Children innerJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) {
- return join(Constant.INNER_JOIN, clazz,alias, left, right, ext);
+ return join(Constant.INNER_JOIN, clazz, alias, left, right, ext);
}
/**
* ignore 参考 left join
*/
default Children innerJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) {
- return join(Constant.INNER_JOIN, clazz,alias, consumer);
+ return join(Constant.INNER_JOIN, clazz, alias, consumer);
}
/**
@@ -253,28 +253,28 @@ public interface QueryJoin extends MPJBaseJoin, String
* ignore 参考 left join
*/
default Children fullJoin(Class clazz, String alias, SFunction left, SFunction right) {
- return join(Constant.FULL_JOIN, clazz,alias, left, right);
+ return join(Constant.FULL_JOIN, clazz, alias, left, right);
}
/**
* ignore 参考 left join
*/
default Children fullJoin(Class clazz, String alias, WrapperFunction> function) {
- return join(Constant.FULL_JOIN, clazz,alias, function);
+ return join(Constant.FULL_JOIN, clazz, alias, function);
}
/**
* ignore 参考 left join
*/
default Children fullJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) {
- return join(Constant.FULL_JOIN, clazz,alias, left, right, ext);
+ return join(Constant.FULL_JOIN, clazz, alias, left, right, ext);
}
/**
* ignore 参考 left join
*/
default Children fullJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) {
- return join(Constant.FULL_JOIN, clazz,alias, consumer);
+ return join(Constant.FULL_JOIN, clazz, alias, consumer);
}
/**
@@ -330,7 +330,7 @@ public interface QueryJoin extends MPJBaseJoin, String
* @param right 扩展 用于关联表的 select 和 where
*/
default Children join(String keyWord, Class clazz, String alias, SFunction left, SFunction right) {
- return join(keyWord, clazz,alias, on -> on.eq(left, right));
+ return join(keyWord, clazz, alias, on -> on.eq(left, right));
}
/**
@@ -342,7 +342,7 @@ public interface QueryJoin extends MPJBaseJoin, String
* @param function 条件
*/
default Children join(String keyWord, Class clazz, String alias, WrapperFunction> function) {
- return join(keyWord, clazz,alias, (on, e) -> function.apply(on));
+ return join(keyWord, clazz, alias, (on, e) -> function.apply(on));
}
/**
@@ -353,7 +353,7 @@ public interface QueryJoin extends MPJBaseJoin, String
* @param right 条件
*/
default Children join(String keyWord, Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) {
- return join(keyWord, clazz,alias, (on, e) -> {
+ return join(keyWord, clazz, alias, (on, e) -> {
on.eq(left, right);
ext.apply(e);
});
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java
index c73e849..e9152f6 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java
@@ -168,15 +168,14 @@ public class MybatisLabel {
/**
* 嵌套
*/
- public Builder association(Integer index, Class child, SFunction dtoField) {
+ public Builder association(String index, Class child, SFunction dtoField) {
Class dtoClass = LambdaUtils.getEntityClass(dtoField);
Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
String dtoFieldName = LambdaUtils.getName(dtoField);
Field field = fieldMap.get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder builder;
- builder = new MybatisLabel.Builder<>(Objects.isNull(index) ? null : index.toString(),
- dtoFieldName, child, field.getType(), (Class) field.getType(), true);
+ builder = new MybatisLabel.Builder<>(index, dtoFieldName, child, field.getType(), (Class) field.getType(), true);
mybatisLabel.mybatisLabels.add(builder.build());
return this;
}
@@ -189,14 +188,13 @@ public class MybatisLabel {
/**
* 嵌套
*/
- public Builder association(Integer index, Class child, SFunction dtoField,
+ public Builder association(String index, Class child, SFunction dtoField,
MFunc> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
- MybatisLabel.Builder builder = new MybatisLabel.Builder<>(Objects.isNull(index) ? null : index.toString(),
- dtoFieldName, child, field.getType(), (Class) field.getType(), false);
+ MybatisLabel.Builder builder = new MybatisLabel.Builder<>(index, dtoFieldName, child, field.getType(), (Class) field.getType(), false);
mybatisLabel.mybatisLabels.add(collection.apply(builder).build());
return this;
}
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java
index 51408f5..bc31e0d 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java
@@ -15,7 +15,11 @@ public interface Select {
Class> getClazz();
- String getIndex();
+ Integer getIndex();
+
+ boolean isHasTableAlias();
+
+ String getTableAlias();
boolean isPk();
@@ -39,7 +43,7 @@ public interface Select {
boolean isFunc();
- SFunction,?>[] getArgs();
+ SFunction, ?>[] getArgs();
BaseFuncEnum getFunc();
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java
index 92b982c..a25588a 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java
@@ -18,17 +18,23 @@ public class SelectAlias implements Select {
private final SelectCache cache;
- private final String index;
+ private final Integer index;
private final boolean hasAlias;
private final String alias;
- public SelectAlias(SelectCache cache, String index, String alias) {
+ private final boolean hasTableAlias;
+
+ private final String tableAlias;
+
+ public SelectAlias(SelectCache cache, Integer index, String alias, boolean hasTableAlias, String tableAlias) {
this.cache = cache;
this.index = index;
this.hasAlias = true;
this.alias = alias;
+ this.hasTableAlias = hasTableAlias;
+ this.tableAlias = tableAlias;
}
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java
index f6e2bde..b2f23a0 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java
@@ -18,7 +18,7 @@ import java.util.Objects;
@Getter
public class SelectFunc implements Select {
- private final String index;
+ private final Integer index;
private final SelectCache cache;
@@ -34,8 +34,12 @@ public class SelectFunc implements Select {
private final BaseFuncEnum func;
+ private final boolean hasTableAlias;
- public SelectFunc(SelectCache cache, String index, String alias, BaseFuncEnum func) {
+ private final String tableAlias;
+
+
+ public SelectFunc(SelectCache cache, Integer index, String alias, BaseFuncEnum func, boolean hasTableAlias, String tableAlias) {
this.index = index;
this.cache = cache;
this.column = cache.getColumn();
@@ -44,9 +48,11 @@ public class SelectFunc implements Select {
this.alias = alias;
this.isFunc = true;
this.func = func;
+ this.hasTableAlias = hasTableAlias;
+ this.tableAlias = tableAlias;
}
- public SelectFunc(String alias, String index, BaseFuncEnum func, String column) {
+ public SelectFunc(String alias, Integer index, BaseFuncEnum func, String column, boolean hasTableAlias, String tableAlias) {
this.index = index;
this.column = column;
this.args = null;
@@ -55,9 +61,11 @@ public class SelectFunc implements Select {
this.alias = alias;
this.isFunc = true;
this.func = func;
+ this.hasTableAlias = hasTableAlias;
+ this.tableAlias = tableAlias;
}
- public SelectFunc(String alias, String index, BaseFuncEnum func, SFunction, ?>[] args) {
+ public SelectFunc(String alias, Integer index, BaseFuncEnum func, SFunction, ?>[] args, boolean hasTableAlias, String tableAlias) {
this.index = index;
this.column = null;
this.args = args;
@@ -66,6 +74,8 @@ public class SelectFunc implements Select {
this.alias = alias;
this.isFunc = true;
this.func = func;
+ this.hasTableAlias = hasTableAlias;
+ this.tableAlias = tableAlias;
}
@Override
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java
index ab1b084..2a97a38 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java
@@ -18,7 +18,7 @@ import java.lang.reflect.Field;
@Getter
public class SelectLabel implements Select {
- private final String index;
+ private final Integer index;
private final SelectCache cache;
@@ -30,22 +30,30 @@ public class SelectLabel implements Select {
private final String alias;
- public SelectLabel(SelectCache cache, String index, Class> tagClass, Field tagField) {
+ private final boolean hasTableAlias;
+
+ private final String tableAlias;
+
+ public SelectLabel(SelectCache cache, Integer index, Class> tagClass, Field tagField, boolean hasTableAlias, String tableAlias) {
this.cache = cache;
this.index = index;
this.tagClass = tagClass;
this.tagField = tagField;
this.hasAlias = false;
this.alias = null;
+ this.hasTableAlias = hasTableAlias;
+ this.tableAlias = tableAlias;
}
- public SelectLabel(SelectCache cache, String index, Class> tagClass, Field tagField, String column) {
+ public SelectLabel(SelectCache cache, Integer index, Class> tagClass, Field tagField, String column, boolean hasTableAlias, String tableAlias) {
this.cache = cache;
this.index = index;
this.tagClass = tagClass;
this.tagField = tagField;
this.hasAlias = true;
this.alias = column;
+ this.hasTableAlias = hasTableAlias;
+ this.tableAlias = tableAlias;
}
@Override
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java
index ac1644e..02646c1 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java
@@ -15,13 +15,19 @@ import org.apache.ibatis.type.TypeHandler;
@Getter
public class SelectNormal implements Select {
- private final String index;
+ private final Integer index;
private final SelectCache cache;
- public SelectNormal(SelectCache cache, String index) {
+ private final boolean hasTableAlias;
+
+ private final String tableAlias;
+
+ public SelectNormal(SelectCache cache, Integer index, boolean hasTableAlias, String tableAlias) {
this.cache = cache;
this.index = index;
+ this.hasTableAlias = hasTableAlias;
+ this.tableAlias = tableAlias;
}
diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java
index 99855ea..45b07eb 100644
--- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java
+++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java
@@ -14,8 +14,14 @@ import org.apache.ibatis.type.TypeHandler;
public class SelectString implements Select {
private final String column;
- public SelectString(String column) {
+ private final boolean hasTableAlias;
+
+ private final String tableAlias;
+
+ public SelectString(String column, boolean hasTableAlias, String tableAlias) {
this.column = column;
+ this.hasTableAlias = hasTableAlias;
+ this.tableAlias = tableAlias;
}
@Override
@@ -24,10 +30,20 @@ public class SelectString implements Select {
}
@Override
- public String getIndex() {
+ public Integer getIndex() {
return null;
}
+ @Override
+ public boolean isHasTableAlias() {
+ return this.hasTableAlias;
+ }
+
+ @Override
+ public String getTableAlias() {
+ return this.tableAlias;
+ }
+
@Override
public boolean isPk() {
return false;
diff --git a/mybatis-plus-join-test/pom.xml b/mybatis-plus-join-test/pom.xml
index 52d049e..1b981e8 100644
--- a/mybatis-plus-join-test/pom.xml
+++ b/mybatis-plus-join-test/pom.xml
@@ -7,11 +7,11 @@
com.github.yulichang
mybatis-plus-join-root
- 1.3.11
+ 1.4.2.2
mybatis-plus-join-test
pom
- 1.3.11
+ 1.4.2.2
mybatis-plus-join-test
@@ -85,14 +85,14 @@
com.github.yulichang
mybatis-plus-join-boot-starter
- 1.3.11
+ 1.4.2.2
-
-
-
-
-
+
+
+
+
+
com.fasterxml.jackson.core
jackson-databind
@@ -101,7 +101,7 @@
com.baomidou
mybatis-plus-boot-starter
- 3.5.2
+ 3.4.2
org.springframework
diff --git a/mybatis-plus-join-test/test-collection/pom.xml b/mybatis-plus-join-test/test-collection/pom.xml
index 0c30c90..0e383f3 100644
--- a/mybatis-plus-join-test/test-collection/pom.xml
+++ b/mybatis-plus-join-test/test-collection/pom.xml
@@ -5,7 +5,7 @@
com.github.yulichang
mybatis-plus-join-test
- 1.3.11
+ 1.4.2.2
4.0.0
diff --git a/mybatis-plus-join-test/test-join/pom.xml b/mybatis-plus-join-test/test-join/pom.xml
index 18901c7..7b36b83 100644
--- a/mybatis-plus-join-test/test-join/pom.xml
+++ b/mybatis-plus-join-test/test-join/pom.xml
@@ -5,7 +5,7 @@
com.github.yulichang
mybatis-plus-join-test
- 1.3.11
+ 1.4.2.2
4.0.0
diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserDto.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserDto.java
new file mode 100644
index 0000000..7304b63
--- /dev/null
+++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserDto.java
@@ -0,0 +1,26 @@
+package com.github.yulichang.test.join.entity;
+
+import com.baomidou.mybatisplus.annotation.TableField;
+import com.baomidou.mybatisplus.annotation.TableId;
+import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
+
+@Data
+@TableName("user_dto")
+public class UserDto {
+
+ @TableId
+ private Integer id;
+
+ private Integer userId;
+
+ private Integer createBy;
+
+ @TableField(exist = false)
+ private String createName;
+
+ private Integer updateBy;
+
+ @TableField(exist = false)
+ private String updateName;
+}
diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserDTOMapper.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserDTOMapper.java
new file mode 100644
index 0000000..5426d6a
--- /dev/null
+++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserDTOMapper.java
@@ -0,0 +1,9 @@
+package com.github.yulichang.test.join.mapper;
+
+import com.github.yulichang.base.MPJBaseMapper;
+import com.github.yulichang.test.join.entity.UserDto;
+import org.apache.ibatis.annotations.Mapper;
+
+@Mapper
+public interface UserDTOMapper extends MPJBaseMapper {
+}
diff --git a/mybatis-plus-join-test/test-join/src/main/resources/db/schema.sql b/mybatis-plus-join-test/test-join/src/main/resources/db/schema.sql
index e14e58f..abbeb2c 100644
--- a/mybatis-plus-join-test/test-join/src/main/resources/db/schema.sql
+++ b/mybatis-plus-join-test/test-join/src/main/resources/db/schema.sql
@@ -44,4 +44,15 @@ create table address
tel varchar(255) null,
address varchar(255) null,
del bit
+);
+
+create table user_dto
+(
+ id int auto_increment
+ primary key,
+ user_id int not null,
+ create_by int not null,
+ update_by int not null,
+ version int not null,
+ del bit null
);
\ No newline at end of file
diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java
index c9f1bf0..8d1b26b 100644
--- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java
+++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java
@@ -8,13 +8,15 @@ import com.github.yulichang.test.join.dto.UserDTO;
import com.github.yulichang.test.join.entity.AddressDO;
import com.github.yulichang.test.join.entity.AreaDO;
import com.github.yulichang.test.join.entity.UserDO;
+import com.github.yulichang.test.join.entity.UserDto;
+import com.github.yulichang.test.join.mapper.UserDTOMapper;
import com.github.yulichang.test.join.mapper.UserMapper;
import com.github.yulichang.toolkit.MPJWrappers;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.junit.jupiter.api.Test;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
-import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
@@ -30,9 +32,12 @@ import java.util.Map;
@SuppressWarnings("unused")
@SpringBootTest
class LambdaWrapperTest {
- @Resource
+ @Autowired
private UserMapper userMapper;
+ @Autowired
+ private UserDTOMapper userDTOMapper;
+
@Test
void testJoin() {
MPJLambdaWrapper wrapper = new MPJLambdaWrapper()
@@ -54,8 +59,8 @@ class LambdaWrapperTest {
.selectAll(UserDO.class)
.selectCollection(AddressDO.class, UserDTO::getAddressList, addr -> addr
.association(AreaDO.class, AddressDTO::getArea))
- .leftJoin(AddressDO::getUserId, UserDO::getId)
- .leftJoin(AreaDO::getId, AddressDO::getAreaId)
+ .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
+ .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId)
.orderByDesc(UserDO::getId);
List list = userMapper.selectJoinList(UserDTO.class, wrapper);
@@ -92,20 +97,49 @@ class LambdaWrapperTest {
@Test
void testMSCache() {
-// MPJLambdaWrapper wrapper = new MPJLambdaWrapper()
-// .selectAll(UserDO.class)
-// .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
-// .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId);
-// List list = userMapper.selectJoinList(UserDTO.class, wrapper);
+// PageHelper.startPage(1, 10);
+ MPJLambdaWrapper wrapper = new MPJLambdaWrapper()
+ .selectAll(UserDO.class)
+ .setEntity(new UserDO() {{
+ setId(1);
+ }})
+// .eq(UserDO::getId,1)
+ .orderByAsc(UserDO::getId, UserDO::getName);
- MPJLambdaWrapper wrapper1 = new MPJLambdaWrapper()
- .select(UserDO::getId)
- .selectAs(UserDO::getJson, UserDTO::getArea)
- .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)
- .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId);
- List list1 = userMapper.selectJoinList(UserDTO.class, wrapper1);
+ System.out.println(wrapper.getSqlSegment());
+ System.out.println(wrapper.nonEmptyOfNormal());
+ System.out.println(wrapper.isEmptyOfNormal());
+ System.out.println("-----------------");
+ System.out.println(wrapper.nonEmptyOfWhere());
+ System.out.println(wrapper.isEmptyOfWhere());
- assert list1.get(0).getArea() != null;
+ List list = userMapper.selectList(wrapper);
+// list.forEach(System.out::println);
+ }
+
+ @Test
+ void testMSCachee() {
+ MPJLambdaWrapper wrapper = new MPJLambdaWrapper("tt")
+ .selectAll(UserDto.class)
+ .leftJoin(UserDO.class, "ua", UserDO::getId, UserDto::getUserId, ext -> ext
+ .select(UserDO::getId))
+ .leftJoin(UserDO.class, "ub", UserDO::getId, UserDto::getCreateBy, ext -> ext
+ .select(UserDO::getImg))
+ .leftJoin(UserDO.class, "uc", UserDO::getId, UserDto::getUpdateBy);
+ userDTOMapper.selectJoinList(UserDto.class, wrapper);
+ System.out.println(1);
+
+ MPJLambdaWrapper w = new MPJLambdaWrapper("tt")
+ .selectAll(UserDO.class)
+ .leftJoin(UserDO.class, "ua", UserDO::getId, UserDO::getPid, ext -> ext
+ .select(UserDO::getId)
+ .eq(UserDO::getImg, UserDO::getName))
+ .leftJoin(UserDO.class, "ub", UserDO::getId, UserDO::getCreateBy, ext -> ext
+ .select(UserDO::getImg))
+ .leftJoin(UserDO.class, "uc", UserDO::getId, UserDO::getUpdateBy)
+ .eq(UserDO::getId, UserDO::getId);
+ userMapper.selectJoinList(UserDO.class, w);
+ System.out.println(1);
}
/**
@@ -113,26 +147,28 @@ class LambdaWrapperTest {
*/
@Test
void testInner() {
-// //自连接
-// MPJLambdaWrapper wrapper = new MPJLambdaWrapper()
-// .disableSubLogicDel()//关闭副表逻辑删除
-//// .disableLogicDel()//关闭主表逻辑删除
-// .selectAll(UserDO.class)
-// .selectCollection(UserDO.class, UserDO::getChildren)
-// .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId);
-// List