From 7e4b778f79d525259ab3c7ae313b67f8aad807f0 Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Wed, 16 Oct 2024 17:55:33 +0800 Subject: [PATCH] feat: join support wrapper --- .../wrapper/JoinAbstractLambdaWrapper.java | 31 +- .../yulichang/wrapper/MPJLambdaWrapper.java | 50 +- .../wrapper/interfaces/QueryJoin.java | 513 +++++++++++++++++- .../yulichang/test/join/unit/JoinTest.java | 234 ++++++++ .../test/join/unit/SelectSubTest.java | 44 +- .../yulichang/test/join/unit/UnionTest.java | 23 +- 6 files changed, 825 insertions(+), 70 deletions(-) create mode 100644 mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/JoinTest.java diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractLambdaWrapper.java index eea063f..083f3c3 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractLambdaWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractLambdaWrapper.java @@ -14,6 +14,7 @@ import com.github.yulichang.config.enums.LogicDelTypeEnum; import com.github.yulichang.toolkit.*; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.wrapper.enums.PrefixEnum; +import com.github.yulichang.wrapper.interfaces.MConsumer; import com.github.yulichang.wrapper.interfaces.MFunction; import com.github.yulichang.wrapper.interfaces.QueryJoin; import com.github.yulichang.wrapper.segments.PageInfo; @@ -177,6 +178,12 @@ public abstract class JoinAbstractLambdaWrapper Children join(String keyWord, Class clazz, String tableAlias, BiConsumer, Children> consumer) { + public Children join(String keyWord, Class clazz, MConsumer> table, String tableAlias, BiConsumer, Children> consumer) { + String tabName; + if (table != null) { + MPJLambdaWrapper tableWrapper = new MPJLambdaWrapper(null, clazz, SharedString.emptyString(), + paramNameSeq, paramNameValuePairs, new MergeSegments(), new SharedString(this.paramAlias + .getStringValue()), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), + new TableList(), null, null, null, null, ifExists) { + }; + tableWrapper.tableList.setAlias(ConfigProperties.tableAlias); + tableWrapper.tableList.setRootClass(clazz); + + table.accept(tableWrapper); + if (MPJStringUtils.isBlank(tableWrapper.getSqlSelect())) { + tableWrapper.selectAll(); + } + tabName = "(" + WrapperUtils.buildUnionSqlByWrapper(clazz, tableWrapper) + ")"; + } else { + TableInfo info = TableHelper.getAssert(clazz); + tabName = info.getTableName(); + } Integer oldIndex = this.getIndex(); int newIndex = tableIndex; - TableInfo info = TableHelper.getAssert(clazz); - Children instance = instance(newIndex, keyWord, clazz, info.getTableName()); + Children instance = instance(newIndex, keyWord, clazz, tabName); instance.isOn = true; instance.isMain = false; onWrappers.add(instance); 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 3c7b12e..e061063 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 @@ -1,13 +1,14 @@ package com.github.yulichang.wrapper; import com.baomidou.mybatisplus.core.conditions.SharedString; -import com.baomidou.mybatisplus.core.conditions.Wrapper; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; 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.*; +import com.github.yulichang.toolkit.TableList; +import com.github.yulichang.toolkit.WrapperUtils; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum; import com.github.yulichang.wrapper.interfaces.*; @@ -54,17 +55,6 @@ public class MPJLambdaWrapper extends JoinAbstractLambdaWrapper> wrapperMap; - private List> fill; /** @@ -273,7 +263,6 @@ public class MPJLambdaWrapper extends JoinAbstractLambdaWrapper WrapperUtils.buildSubSqlByWrapper(clazz, wrapper, alias), hasAlias, this.alias, alias)); return typedThis; @@ -288,8 +277,13 @@ public class MPJLambdaWrapper extends JoinAbstractLambdaWrapper MPJLambdaWrapper union(Class clazz, Consumer> consumer) { - MPJLambdaWrapper unionWrapper = JoinWrappers.lambda(clazz); - addCustomWrapper(unionWrapper); + MPJLambdaWrapper unionWrapper = new MPJLambdaWrapper(null, clazz, SharedString.emptyString(), + paramNameSeq, paramNameValuePairs, new MergeSegments(), new SharedString(this.paramAlias + .getStringValue()), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), + new TableList(), null, null, null, null, ifExists) { + }; + unionWrapper.tableList.setAlias(ConfigProperties.tableAlias); + unionWrapper.tableList.setRootClass(clazz); consumer.accept(unionWrapper); String sb = " UNION " + WrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper); @@ -326,8 +320,13 @@ public class MPJLambdaWrapper extends JoinAbstractLambdaWrapper MPJLambdaWrapper unionAll(Class clazz, Consumer> consumer) { - MPJLambdaWrapper unionWrapper = JoinWrappers.lambda(clazz); - addCustomWrapper(unionWrapper); + MPJLambdaWrapper unionWrapper = new MPJLambdaWrapper(null, clazz, SharedString.emptyString(), + paramNameSeq, paramNameValuePairs, new MergeSegments(), new SharedString(this.paramAlias + .getStringValue()), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), + new TableList(), null, null, null, null, ifExists) { + }; + unionWrapper.tableList.setAlias(ConfigProperties.tableAlias); + unionWrapper.tableList.setRootClass(clazz); consumer.accept(unionWrapper); String sb = " UNION ALL " + WrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper); @@ -339,19 +338,6 @@ public class MPJLambdaWrapper extends JoinAbstractLambdaWrapper wrapper) { - if (Objects.isNull(wrapperIndex)) { - wrapperIndex = new AtomicInteger(0); - } - int index = wrapperIndex.incrementAndGet(); - if (Objects.isNull(wrapperMap)) { - wrapperMap = new HashMap<>(); - } - String key = "ew" + index; - wrapper.setParamAlias(getParamAlias() + ".wrapperMap." + key); - wrapperMap.put(key, wrapper); - } - /** * 查询条件 SQL 片段 */ @@ -426,8 +412,6 @@ public class MPJLambdaWrapper extends JoinAbstractLambdaWrapper extends MPJBaseJoin, StringJoin { /** @@ -34,6 +34,7 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 条件 */ default Children leftJoin(Class clazz, SFunction left, String rightAlias, SFunction right) { + return join(Constant.LEFT_JOIN, clazz, left, rightAlias, right); } @@ -46,6 +47,7 @@ public interface QueryJoin extends MPJBaseJoin, String * @param function 条件` */ default Children leftJoin(Class clazz, MFunction> function) { + return join(Constant.LEFT_JOIN, clazz, function); } @@ -57,6 +59,7 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 条件 */ default Children leftJoin(Class clazz, SFunction left, SFunction right, MFunction ext) { + return join(Constant.LEFT_JOIN, clazz, left, right, ext); } @@ -69,6 +72,7 @@ public interface QueryJoin extends MPJBaseJoin, String * @param consumer 条件 */ default Children leftJoin(Class clazz, BiConsumer, Children> consumer) { + return join(Constant.LEFT_JOIN, clazz, consumer); } @@ -80,6 +84,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); } @@ -92,6 +97,7 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 条件 */ default Children leftJoin(Class clazz, String alias, SFunction left, String rightAlias, SFunction right) { + return join(Constant.LEFT_JOIN, clazz, alias, left, rightAlias, right); } @@ -104,6 +110,7 @@ public interface QueryJoin extends MPJBaseJoin, String * @param function 条件 */ default Children leftJoin(Class clazz, String alias, MFunction> function) { + return join(Constant.LEFT_JOIN, clazz, alias, function); } @@ -115,6 +122,7 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 条件 */ default Children leftJoin(Class clazz, String alias, SFunction left, SFunction right, MFunction ext) { + return join(Constant.LEFT_JOIN, clazz, alias, left, right, ext); } @@ -127,21 +135,149 @@ public interface QueryJoin extends MPJBaseJoin, String * @param consumer 条件 */ default Children leftJoin(Class clazz, String alias, BiConsumer, Children> consumer) { + return join(Constant.LEFT_JOIN, clazz, alias, consumer); } + /** + * left join + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, SFunction left, SFunction right) { + + return join(Constant.LEFT_JOIN, clazz, table, left, right); + } + + /** + * left join + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param rightAlias 条件字段别名 + * @param right 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, SFunction left, String rightAlias, SFunction right) { + + return join(Constant.LEFT_JOIN, clazz, table, left, rightAlias, right); + } + + /** + * left join 多条件 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param function 条件` + */ + default Children leftJoin(Class clazz, MConsumer> table, MFunction> function) { + + return join(Constant.LEFT_JOIN, clazz, table, function); + } + + /** + * left join + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, SFunction left, SFunction right, MFunction ext) { + + return join(Constant.LEFT_JOIN, clazz, table, left, right, ext); + } + + /** + * left join 多条件 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param consumer 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, BiConsumer, Children> consumer) { + + return join(Constant.LEFT_JOIN, clazz, table, consumer); + } + + /** + * left join + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, String alias, SFunction left, SFunction right) { + + return join(Constant.LEFT_JOIN, clazz, table, alias, left, right); + } + + /** + * left join + * + * @param clazz 关联的实体类 + * @param rightAlias 条件字段别名 + * @param left 条件 + * @param right 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, String alias, SFunction left, String rightAlias, SFunction right) { + + return join(Constant.LEFT_JOIN, clazz, table, alias, left, rightAlias, right); + } + + /** + * left join 多条件 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param function 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, String alias, MFunction> function) { + + return join(Constant.LEFT_JOIN, clazz, table, alias, function); + } + + /** + * left join + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, String alias, SFunction left, SFunction right, MFunction ext) { + + return join(Constant.LEFT_JOIN, clazz, table, alias, left, right, ext); + } + + /** + * left join 多条件 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param consumer 条件 + */ + default Children leftJoin(Class clazz, MConsumer> table, String alias, BiConsumer, Children> consumer) { + + return join(Constant.LEFT_JOIN, clazz, table, alias, consumer); + } + /** * ignore 参考 left join */ default Children rightJoin(Class clazz, SFunction left, SFunction right) { - return join(Constant.RIGHT_JOIN, clazz, left, right); + return join(Constant.RIGHT_JOIN, clazz, left, right); } /** * ignore 参考 left join */ default Children rightJoin(Class clazz, SFunction left, String rightAlias, SFunction right) { + return join(Constant.RIGHT_JOIN, clazz, left, rightAlias, right); } @@ -149,6 +285,7 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children rightJoin(Class clazz, MFunction> function) { + return join(Constant.RIGHT_JOIN, clazz, function); } @@ -156,20 +293,31 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children rightJoin(Class clazz, SFunction left, SFunction right, MFunction ext) { + return join(Constant.RIGHT_JOIN, clazz, left, right, ext); } /** * ignore 参考 left join */ - default Children rightJoin(Class clazz, BiConsumer, Children> consumer) { + default Children rightJoin(Class clazz, BiConsumer, Children> consumer) { + return join(Constant.RIGHT_JOIN, clazz, consumer); } + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, String alias, SFunction left, SFunction right) { + + return join(Constant.RIGHT_JOIN, clazz, alias, left, right); + } + /** * ignore 参考 left join */ default Children rightJoin(Class clazz, String alias, SFunction left, String rightAlias, SFunction right) { + return join(Constant.RIGHT_JOIN, clazz, alias, left, rightAlias, right); } @@ -177,6 +325,7 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children rightJoin(Class clazz, String alias, MFunction> function) { + return join(Constant.RIGHT_JOIN, clazz, alias, function); } @@ -184,21 +333,105 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children rightJoin(Class clazz, String alias, SFunction left, SFunction right, MFunction ext) { + return join(Constant.RIGHT_JOIN, clazz, alias, left, right, ext); } /** * ignore 参考 left join */ - default Children rightJoin(Class clazz, String alias, BiConsumer, Children> consumer) { + default Children rightJoin(Class clazz, String alias, BiConsumer, Children> consumer) { + return join(Constant.RIGHT_JOIN, clazz, alias, consumer); } + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, SFunction left, SFunction right) { + + return join(Constant.RIGHT_JOIN, clazz, table, left, right); + + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, SFunction left, String rightAlias, SFunction right) { + + return join(Constant.RIGHT_JOIN, clazz, table, left, rightAlias, right); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, MFunction> function) { + + return join(Constant.RIGHT_JOIN, clazz, table, function); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, SFunction left, SFunction right, MFunction ext) { + + return join(Constant.RIGHT_JOIN, clazz, table, left, right, ext); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, BiConsumer, Children> consumer) { + + return join(Constant.RIGHT_JOIN, clazz, table, consumer); + } + + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, String alias, SFunction left, SFunction right) { + + return join(Constant.RIGHT_JOIN, clazz, table, alias, left, right); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, String alias, SFunction left, String rightAlias, SFunction right) { + + return join(Constant.RIGHT_JOIN, clazz, table, alias, left, rightAlias, right); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, String alias, MFunction> function) { + + return join(Constant.RIGHT_JOIN, clazz, table, alias, function); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, String alias, SFunction left, SFunction right, MFunction ext) { + + return join(Constant.RIGHT_JOIN, clazz, table, alias, left, right, ext); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, MConsumer> table, String alias, BiConsumer, Children> consumer) { + + return join(Constant.RIGHT_JOIN, clazz, table, alias, consumer); + } /** * ignore 参考 left join */ default Children innerJoin(Class clazz, SFunction left, SFunction right) { + return join(Constant.INNER_JOIN, clazz, left, right); } @@ -206,6 +439,7 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children innerJoin(Class clazz, SFunction left, String rightAlias, SFunction right) { + return join(Constant.INNER_JOIN, clazz, left, rightAlias, right); } @@ -213,6 +447,7 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children innerJoin(Class clazz, MFunction> function) { + return join(Constant.INNER_JOIN, clazz, function); } @@ -220,6 +455,7 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children innerJoin(Class clazz, SFunction left, SFunction right, MFunction ext) { + return join(Constant.INNER_JOIN, clazz, left, right, ext); } @@ -227,6 +463,7 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children innerJoin(Class clazz, BiConsumer, Children> consumer) { + return join(Constant.INNER_JOIN, clazz, consumer); } @@ -235,13 +472,23 @@ 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, left, right); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, String alias, SFunction left, String rightAlias, SFunction right) { + + return join(Constant.INNER_JOIN, clazz, alias, left, rightAlias, right); } /** * ignore 参考 left join */ default Children innerJoin(Class clazz, String alias, MFunction> function) { + return join(Constant.INNER_JOIN, clazz, alias, function); } @@ -249,23 +496,99 @@ public interface QueryJoin extends MPJBaseJoin, String * ignore 参考 left join */ default Children innerJoin(Class clazz, String alias, SFunction left, SFunction right, MFunction ext) { - return join(Constant.INNER_JOIN, clazz, alias, left, right, ext); - } - /** - * innerjoin两个表支持别名 参考 left join - */ - default Children innerJoin(Class clazz, String alias, SFunction left, String rightAlias, SFunction right) { - return join(Constant.INNER_JOIN, clazz, alias, left, rightAlias, right); + return join(Constant.INNER_JOIN, clazz, alias, left, right, ext); } /** * ignore 参考 left join */ default Children innerJoin(Class clazz, String alias, BiConsumer, Children> consumer) { + return join(Constant.INNER_JOIN, clazz, alias, consumer); } + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, SFunction left, SFunction right) { + + return join(Constant.INNER_JOIN, clazz, table, left, right); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, SFunction left, String rightAlias, SFunction right) { + + return join(Constant.INNER_JOIN, clazz, table, left, rightAlias, right); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, MFunction> function) { + + return join(Constant.INNER_JOIN, clazz, table, function); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, SFunction left, SFunction right, MFunction ext) { + + return join(Constant.INNER_JOIN, clazz, table, left, right, ext); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, BiConsumer, Children> consumer) { + + return join(Constant.INNER_JOIN, clazz, table, consumer); + } + + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, String alias, SFunction left, SFunction right) { + + return join(Constant.INNER_JOIN, clazz, table, alias, left, right); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, String alias, SFunction left, String rightAlias, SFunction right) { + + return join(Constant.INNER_JOIN, clazz, table, alias, left, rightAlias, right); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, String alias, MFunction> function) { + + return join(Constant.INNER_JOIN, clazz, table, alias, function); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, String alias, SFunction left, SFunction right, MFunction ext) { + + return join(Constant.INNER_JOIN, clazz, table, alias, left, right, ext); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, MConsumer> table, String alias, BiConsumer, Children> consumer) { + + return join(Constant.INNER_JOIN, clazz, table, alias, consumer); + } + /** * ignore 参考 left join */ @@ -348,7 +671,8 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 扩展 用于关联表的 select 和 where */ default Children join(String keyWord, Class clazz, SFunction left, SFunction right) { - return join(keyWord, clazz, on -> on.eq(left, right)); + + return join(keyWord, clazz, null, null, (on, e) -> on.eq(left, right)); } /** @@ -362,7 +686,8 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 扩展 用于关联表的 select 和 where */ default Children join(String keyWord, Class clazz, SFunction left, String rightAlias, SFunction right) { - return join(keyWord, clazz, on -> on.eq(left, rightAlias, right)); + + return join(keyWord, clazz, null, null, (on, e) -> on.eq(left, rightAlias, right)); } /** @@ -374,7 +699,8 @@ public interface QueryJoin extends MPJBaseJoin, String * @param function 条件 */ default Children join(String keyWord, Class clazz, MFunction> function) { - return join(keyWord, clazz, (on, e) -> function.apply(on)); + + return join(keyWord, clazz, null, null, (on, e) -> function.apply(on)); } /** @@ -385,12 +711,21 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 条件 */ default Children join(String keyWord, Class clazz, SFunction left, SFunction right, MFunction ext) { - return join(keyWord, clazz, (on, e) -> { + + return join(keyWord, clazz, null, null, (on, e) -> { on.eq(left, right); ext.apply(e); }); } + /** + * 内部使用, 不建议直接调用 + */ + default Children join(String keyWord, Class clazz, BiConsumer, Children> consumer) { + + return join(keyWord, clazz, null, null, consumer); + } + /** * 自定义连表关键词 * 调用此方法 keyword 前后需要带空格 比如 " LEFT JOIN " " RIGHT JOIN " @@ -403,7 +738,8 @@ 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, null, alias, (on, e) -> on.eq(left, right)); } /** @@ -418,7 +754,8 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 扩展 用于关联表的 select 和 where */ default Children join(String keyWord, Class clazz, String alias, SFunction left, String rightAlias, SFunction right) { - return join(keyWord, clazz, alias, on -> on.eq(left, rightAlias, right)); + + return join(keyWord, clazz, null, alias, (on, e) -> on.eq(left, rightAlias, right)); } /** @@ -430,7 +767,8 @@ public interface QueryJoin extends MPJBaseJoin, String * @param function 条件 */ default Children join(String keyWord, Class clazz, String alias, MFunction> function) { - return join(keyWord, clazz, alias, (on, e) -> function.apply(on)); + + return join(keyWord, clazz, null, alias, (on, e) -> function.apply(on)); } /** @@ -441,7 +779,8 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 条件 */ default Children join(String keyWord, Class clazz, String alias, SFunction left, SFunction right, MFunction ext) { - return join(keyWord, clazz, alias, (on, e) -> { + + return join(keyWord, clazz, null, alias, (on, e) -> { on.eq(left, right); ext.apply(e); }); @@ -450,12 +789,140 @@ public interface QueryJoin extends MPJBaseJoin, String /** * 内部使用, 不建议直接调用 */ - default Children join(String keyWord, Class clazz, BiConsumer, Children> consumer) { - return join(keyWord, clazz, null, consumer); + default Children join(String keyWord, Class clazz, String alias, BiConsumer, Children> consumer) { + + return join(keyWord, clazz, null, alias, consumer); + } + + /** + * 自定义连表关键词 + * 调用此方法 keyword 前后需要带空格 比如 " LEFT JOIN " " RIGHT JOIN " + *

+ * 查询基类 可以直接调用此方法实现以上所有功能 + * + * @param keyWord 连表关键字 + * @param clazz 连表实体类 + * @param left 关联条件 + * @param right 扩展 用于关联表的 select 和 where + */ + default Children join(String keyWord, Class clazz, MConsumer> table, SFunction left, SFunction right) { + + return join(keyWord, clazz, table, null, (on, e) -> on.eq(left, right)); + } + + /** + * 自定义连表关键词 + *

+ * 查询基类 可以直接调用此方法实现以上所有功能 + * + * @param keyWord 连表关键字 + * @param clazz 连表实体类 + * @param left 关联条件 + * @param right 扩展 用于关联表的 select 和 where + */ + default Children join(String keyWord, Class clazz, MConsumer> table, SFunction left, String rightAlias, SFunction right) { + + return join(keyWord, clazz, table, null, (on, e) -> on.eq(left, rightAlias, right)); + } + + /** + * 自定义连表关键词 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param function 条件 + */ + default Children join(String keyWord, Class clazz, MConsumer> table, MFunction> function) { + + return join(keyWord, clazz, table, null, (on, e) -> function.apply(on)); + } + + /** + * 自定义连表关键词 + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children join(String keyWord, Class clazz, MConsumer> table, SFunction left, SFunction right, MFunction ext) { + + return join(keyWord, clazz, table, null, (on, e) -> { + on.eq(left, right); + ext.apply(e); + }); } /** * 内部使用, 不建议直接调用 */ - Children join(String keyWord, Class clazz, String alias, BiConsumer, Children> consumer); + default Children join(String keyWord, Class clazz, MConsumer> table, BiConsumer, Children> consumer) { + + return join(keyWord, clazz, table, null, consumer); + } + + /** + * 自定义连表关键词 + * 调用此方法 keyword 前后需要带空格 比如 " LEFT JOIN " " RIGHT JOIN " + *

+ * 查询基类 可以直接调用此方法实现以上所有功能 + * + * @param keyWord 连表关键字 + * @param clazz 连表实体类 + * @param left 关联条件 + * @param right 扩展 用于关联表的 select 和 where + */ + default Children join(String keyWord, Class clazz, MConsumer> table, String alias, SFunction left, SFunction right) { + + return join(keyWord, clazz, table, alias, (on, e) -> on.eq(left, right)); + } + + /** + * 自定义连表关键词 + * 调用此方法 keyword 前后需要带空格 比如 " LEFT JOIN " " RIGHT JOIN " + *

+ * 查询基类 可以直接调用此方法实现以上所有功能 + * + * @param keyWord 连表关键字 + * @param clazz 连表实体类 + * @param left 关联条件 + * @param right 扩展 用于关联表的 select 和 where + */ + default Children join(String keyWord, Class clazz, MConsumer> table, String alias, SFunction left, String rightAlias, SFunction right) { + + return join(keyWord, clazz, table, alias, (on, e) -> on.eq(left, rightAlias, right)); + } + + /** + * 自定义连表关键词 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param function 条件 + */ + default Children join(String keyWord, Class clazz, MConsumer> table, String alias, MFunction> function) { + + return join(keyWord, clazz, table, alias, (on, e) -> function.apply(on)); + } + + /** + * 自定义连表关键词 + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children join(String keyWord, Class clazz, MConsumer> table, String alias, SFunction left, SFunction right, MFunction ext) { + + return join(keyWord, clazz, table, alias, (on, e) -> { + on.eq(left, right); + ext.apply(e); + }); + } + + /** + * 内部使用, 不建议直接调用 + */ + Children join(String keyWord, Class clazz, MConsumer> table, String alias, BiConsumer, Children> consumer); } diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/JoinTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/JoinTest.java new file mode 100644 index 0000000..d17d748 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/JoinTest.java @@ -0,0 +1,234 @@ +package com.github.yulichang.test.join.unit; + +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +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.util.Reset; +import com.github.yulichang.test.util.ThreadLocalUtils; +import com.github.yulichang.toolkit.JoinWrappers; +import com.github.yulichang.wrapper.JoinAbstractLambdaWrapper; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import com.github.yulichang.wrapper.interfaces.MConsumer; +import com.github.yulichang.wrapper.interfaces.MFunction; +import com.github.yulichang.wrapper.interfaces.QueryJoin; +import org.apache.ibatis.logging.nologging.NoLoggingImpl; +import org.apache.ibatis.logging.stdout.StdOutImpl; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.Arrays; +import java.util.Objects; +import java.util.Set; +import java.util.TreeSet; +import java.util.function.BiConsumer; +import java.util.stream.Collectors; + +@SpringBootTest +public class JoinTest { + + + @BeforeEach + void setUp() { + Reset.reset(); + } + + @Test + void joinTest() { + ThreadLocalUtils.set(""" + SELECT t.id, + t.pid, + t.`name`, + t.`json`, + t.sex, + t.head_img, + t.create_time, + t.address_id, + t.address_id2, + t.del, + t.create_by, + t.update_by + FROM `user` t + LEFT JOIN + (SELECT + tt.id, + tt.user_id, + tt.area_id, + tt.tel, + tt.address, + tt.del + FROM address tt + LEFT JOIN + (SELECT + t.id, + t.province, + t.city, + t.area, + t.postcode, + t.del + FROM area t + WHERE t.del = false + AND (t.id >= ?)) t1 + ON (t1.id = tt.area_id) + WHERE tt.del = false + AND t1.del = false + AND (tt.id >= ?)) t1 ON (t1.user_id = t.id) + WHERE t.del = false + AND t1.del = false + AND (t1.id <= ?) + """); + JoinWrappers.lambda(UserDO.class) + .selectAll() + .leftJoin(AddressDO.class, t -> t + .setAlias("tt") + .selectAll() + .leftJoin(AreaDO.class, tt -> tt + .selectAll() + .ge(AreaDO::getId, -1), AreaDO::getId, AddressDO::getAreaId) + .ge(AddressDO::getId, 0), AddressDO::getUserId, UserDO::getId) + .le(AddressDO::getId, 10000) + .list(UserDTO.class); + } + + + @Test + @SuppressWarnings("deprecation") + void joinTest1() { + //remove log + TableInfoHelper.getTableInfo(UserDO.class).getConfiguration().setLogImpl(NoLoggingImpl.class); + //@formatter:off + getWrapper(w -> w.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.leftJoin(AddressDO.class, AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.leftJoin(AddressDO.class, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.leftJoin(AddressDO.class, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.leftJoin(AddressDO.class, "a", AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.leftJoin(AddressDO.class, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.leftJoin(AddressDO.class, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.leftJoin(AddressDO.class, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.leftJoin(AddressDO.class, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, "a", AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.leftJoin(AddressDO.class, t -> {}, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + getWrapper(w -> w.rightJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.rightJoin(AddressDO.class, AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.rightJoin(AddressDO.class, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.rightJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.rightJoin(AddressDO.class, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.rightJoin(AddressDO.class, "a", AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.rightJoin(AddressDO.class, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.rightJoin(AddressDO.class, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.rightJoin(AddressDO.class, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.rightJoin(AddressDO.class, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, "a", AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.rightJoin(AddressDO.class, t -> {}, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + getWrapper(w -> w.innerJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.innerJoin(AddressDO.class, AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.innerJoin(AddressDO.class, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.innerJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.innerJoin(AddressDO.class, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.innerJoin(AddressDO.class, "a", AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.innerJoin(AddressDO.class, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.innerJoin(AddressDO.class, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.innerJoin(AddressDO.class, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.innerJoin(AddressDO.class, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, "a", AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.innerJoin(AddressDO.class, t -> {}, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + getWrapperF(w -> w.fullJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); + getWrapperF(w -> w.fullJoin(AddressDO.class, AddressDO::getUserId, "t", UserDO::getId)); + getWrapperF(w -> w.fullJoin(AddressDO.class, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapperF(w -> w.fullJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapperF(w -> w.fullJoin(AddressDO.class, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapperF(w -> w.fullJoin(AddressDO.class, "a", AddressDO::getUserId, UserDO::getId)); + getWrapperF(w -> w.fullJoin(AddressDO.class, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapperF(w -> w.fullJoin(AddressDO.class, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapperF(w -> w.fullJoin(AddressDO.class, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapperF(w -> w.fullJoin(AddressDO.class, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + getWrapper(w -> w.join("left join", AddressDO.class, AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.join("left join", AddressDO.class, AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.join("left join", AddressDO.class, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.join("left join", AddressDO.class, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.join("left join", AddressDO.class, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.join("left join", AddressDO.class, "a", AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.join("left join", AddressDO.class, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.join("left join", AddressDO.class, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.join("left join", AddressDO.class, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.join("left join", AddressDO.class, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, (on, ext) -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, "a", AddressDO::getUserId, UserDO::getId)); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, "b", AddressDO::getUserId, "t", UserDO::getId)); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, "c", on -> on.eq(AddressDO::getUserId, UserDO::getId))); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, "d", AddressDO::getUserId, UserDO::getId, ext -> ext)); + getWrapper(w -> w.join("left join", AddressDO.class, t -> {}, "e", (on, e) -> on.eq(AddressDO::getUserId, UserDO::getId))); + + //@formatter:on + assert set.size() == 90; + //reset log + TableInfoHelper.getTableInfo(UserDO.class).getConfiguration().setLogImpl(StdOutImpl.class); + } + + private final Set set = new TreeSet<>(); + + private MPJLambdaWrapper w() { + return new MPJLambdaWrapper<>(UserDO.class) { + @Override + public MPJLambdaWrapper join(String keyWord, Class clazz, MConsumer> table, String tableAlias, + BiConsumer, MPJLambdaWrapper> consumer) { + String line = Arrays.stream(Thread.getAllStackTraces().get(Thread.currentThread())) + .filter(f -> Objects.equals(QueryJoin.class.getName(), f.getClassName())) + .map(f -> f.getLineNumber() + "") + .collect(Collectors.joining("-")); + if (!set.contains(line)) { + set.add(line); + return super.join(keyWord, clazz, table, tableAlias, consumer); + } + throw new RuntimeException("repeat line number " + line); + } + }; + } + + private void getWrapper(MFunction> consumer) { + consumer.apply(w().selectAll()).list(UserDTO.class); + } + + private void getWrapperF(MFunction> consumer) { + consumer.apply(w().selectAll()); + } +} diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/SelectSubTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/SelectSubTest.java index f28c8e7..28b7208 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/SelectSubTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/SelectSubTest.java @@ -28,9 +28,33 @@ public class SelectSubTest { */ @Test void sub() { - ThreadLocalUtils.set("SELECT (SELECT st.id FROM `user` st WHERE st.del = false AND (st.id = t.id AND st.id = ?) LIMIT 1) AS id, (SELECT st.id FROM `user` st WHERE st.del = false AND (st.id = t.id AND st.id = ?) LIMIT 1) AS name FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del = false AND t1.del = false AND (t.id <= ?)"); + ThreadLocalUtils.set(""" + SELECT + (SELECT + (SELECT st.id + FROM `user` st + WHERE st.del = false + AND (st.id = st.id) LIMIT 1) AS id + FROM `user` st + WHERE st.del = false + AND (st.id = t.id AND st.id = ?) LIMIT 1) AS id, + (SELECT + st.id + FROM `user` st + WHERE st.del = false + AND (st.id = t.id AND st.id = ?) LIMIT 1) AS name + FROM `user` t + LEFT JOIN address t1 ON (t1.user_id = t.id) + WHERE t.del = false + AND t1.del = false + AND (t.id <= ?) + """); MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) - .selectSub(UserDO.class, w -> w.select(UserDO::getId) + .selectSub(UserDO.class, w -> w + .selectSub(UserDO.class, ww -> ww + .select(UserDO::getId) + .eq(UserDO::getId, UserDO::getId) + .last("limit 1"), UserDO::getId) .eq(UserDO::getId, UserDO::getId) .eq(UserDO::getId, 2) .last("limit 1"), UserDO::getId) @@ -42,14 +66,14 @@ public class SelectSubTest { .le(UserDO::getId, 100); wrapper.list(); - ThreadLocalUtils.set("SELECT (SELECT st.id FROM area st WHERE st.del = false AND (st.id = t1.id) LIMIT 1) AS id FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del = false AND t1.del = false AND (t.id <= ?)"); - MPJLambdaWrapper wrapper1 = JoinWrappers.lambda(UserDO.class) - .selectSub(AreaDO.class, w -> w.select(AreaDO::getId) - .eq(AreaDO::getId, AddressDO::getId) - .last("limit 1"), UserDO::getId) - .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) - .le(UserDO::getId, 100); - wrapper1.list(); +// ThreadLocalUtils.set("SELECT (SELECT st.id FROM area st WHERE st.del = false AND (st.id = t1.id) LIMIT 1) AS id FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) WHERE t.del = false AND t1.del = false AND (t.id <= ?)"); +// MPJLambdaWrapper wrapper1 = JoinWrappers.lambda(UserDO.class) +// .selectSub(AreaDO.class, w -> w.select(AreaDO::getId) +// .eq(AreaDO::getId, AddressDO::getId) +// .last("limit 1"), UserDO::getId) +// .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) +// .le(UserDO::getId, 100); +// wrapper1.list(); } @Test diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/UnionTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/UnionTest.java index 6160e36..bf2b1d6 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/UnionTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/UnionTest.java @@ -24,7 +24,28 @@ public class UnionTest { @Test void unionAll1() { - ThreadLocalUtils.set("SELECT t.id FROM `user` t WHERE t.del = false AND (t.id = ?) UNION ALL SELECT t.id FROM address t WHERE (t.id = ?) UNION ALL SELECT (SELECT st.id FROM area st WHERE st.del = false AND (st.id = ? AND (st.id = ?))) AS id FROM area t WHERE t.del = false AND (t.id = ? AND (t.id = ?))"); + ThreadLocalUtils.set(""" + SELECT + t.id + FROM `user` t + WHERE t.del = false + AND (t.id = ?) + UNION ALL + SELECT + t.id + FROM address t + WHERE (t.id = ?) + UNION ALL + SELECT + (SELECT + st.id + FROM area st + WHERE st.del = false + AND (st.id = ? AND (st.id = ?))) AS id + FROM area t + WHERE t.del = false + AND (t.id = ? AND (t.id = ?)) + """); MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) .select(UserDO::getId) .eq(UserDO::getId, 1)