From aa0f9e87bf66e99eebde7fa9af2b3e29a9bc81ee Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Fri, 17 Nov 2023 23:41:14 +0800 Subject: [PATCH] =?UTF-8?q?fix=20selectSub=E5=A4=9A=E6=AC=A1=E8=B0=83?= =?UTF-8?q?=E7=94=A8=E6=8A=A5=E9=94=99=E5=92=8C=E4=BC=98=E5=8C=96union?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yulichang/kt/KtAbstractWrapper.java | 18 +++- .../github/yulichang/kt/KtLambdaWrapper.java | 97 ++++++++++++++----- .../yulichang/toolkit/KtWrapperUtils.java | 7 +- .../yulichang/wrapper/MPJAbstractWrapper.java | 1 - .../yulichang/wrapper/MPJLambdaWrapper.java | 86 ++++++++++++---- .../test/join/LambdaWrapperTest.java | 65 ------------- .../yulichang/test/join/m/SelectSubTest.java | 51 ++++++++++ .../yulichang/test/join/m/UnionTest.java | 85 ++++++++++++++++ .../github/yulichang/test/kt/KtApplication.kt | 0 .../yulichang/test/kt/dto/AddressDTO.kt | 0 .../github/yulichang/test/kt/dto/AreaDTO.kt | 12 +++ .../github/yulichang/test/kt/dto/UserDTO.kt | 0 .../yulichang/test/kt/entity/AddressDO.kt | 0 .../test/kt/entity/AddressGeneric.kt | 0 .../github/yulichang/test/kt/entity/AreaDO.kt | 2 +- .../com/github/yulichang/test/kt/entity/ID.kt | 0 .../yulichang/test/kt/entity/OrderDO.kt | 0 .../github/yulichang/test/kt/entity/UserDO.kt | 0 .../yulichang/test/kt/entity/UserDto.kt | 0 .../yulichang/test/kt/entity/UserTTT.kt | 0 .../com/github/yulichang/test/kt/enums/Sex.kt | 0 .../yulichang/test/kt/mapper/AddressMapper.kt | 0 .../yulichang/test/kt/mapper/AreaMapper.kt | 0 .../yulichang/test/kt/mapper/MyBaseMapper.kt | 0 .../yulichang/test/kt/mapper/OrderMapper.kt | 0 .../yulichang/test/kt/mapper/UserDTOMapper.kt | 0 .../yulichang/test/kt/mapper/UserMapper.kt | 0 .../yulichang/test/kt/service/UserService.kt | 0 .../test/kt/service/impl/UserServiceImpl.kt | 0 .../github/yulichang/test/kt/dto/AreaDTO.kt | 12 --- .../yulichang/test/kt/LambdaWrapperTest.kt | 71 ++------------ .../yulichang/test/kt/m/CustomWrapperTest.kt | 57 +++++++++++ .../yulichang/test/kt/m/FieldNameTest.kt | 41 ++++++++ .../yulichang/test/kt/m/SelectSubTest.kt | 53 ++++++++++ .../github/yulichang/test/kt/m/UnionTest.kt | 83 ++++++++++++++++ 35 files changed, 554 insertions(+), 187 deletions(-) create mode 100644 mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/SelectSubTest.java create mode 100644 mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/UnionTest.java rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/KtApplication.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/dto/AddressDTO.kt (100%) create mode 100644 mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/dto/AreaDTO.kt rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/dto/UserDTO.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/entity/AddressDO.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/entity/AddressGeneric.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/entity/AreaDO.kt (93%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/entity/ID.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/entity/OrderDO.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/entity/UserDO.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/entity/UserDto.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/entity/UserTTT.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/enums/Sex.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/mapper/AddressMapper.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/mapper/AreaMapper.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/mapper/MyBaseMapper.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/mapper/OrderMapper.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/mapper/UserDTOMapper.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/mapper/UserMapper.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/service/UserService.kt (100%) rename mybatis-plus-join-test/test-kotlin/src/main/{kotlin => java}/com/github/yulichang/test/kt/service/impl/UserServiceImpl.kt (100%) delete mode 100644 mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/dto/AreaDTO.kt rename mybatis-plus-join-test/test-kotlin/src/test/{ => java}/com/github/yulichang/test/kt/LambdaWrapperTest.kt (94%) create mode 100644 mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/CustomWrapperTest.kt create mode 100644 mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/FieldNameTest.kt create mode 100644 mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/SelectSubTest.kt create mode 100644 mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/UnionTest.kt diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/KtAbstractWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/KtAbstractWrapper.java index 2c0f733..f4299ff 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/KtAbstractWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/KtAbstractWrapper.java @@ -16,6 +16,7 @@ import com.github.yulichang.kt.interfaces.Func; import com.github.yulichang.kt.interfaces.OnCompare; import com.github.yulichang.toolkit.KtUtils; import com.github.yulichang.toolkit.MPJSqlInjectionUtils; +import com.github.yulichang.toolkit.Ref; import com.github.yulichang.toolkit.TableList; import com.github.yulichang.toolkit.sql.SqlScriptUtils; import com.github.yulichang.wrapper.enums.PrefixEnum; @@ -29,6 +30,7 @@ import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiPredicate; import java.util.function.Consumer; +import java.util.function.Supplier; import static com.baomidou.mybatisplus.core.enums.SqlKeyword.*; import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY; @@ -146,6 +148,21 @@ public abstract class KtAbstractWrapper C toChildren(Ref children) { + return (C) this; + } + + /** + * 转为子类,方便自定义继承扩展 + * 需要子类自定义字段 + */ + public C toChildren(Supplier s) { + return (C) this; + } + /** * 开启检查 SQL 注入 */ @@ -670,7 +687,6 @@ public abstract class KtAbstractWrapper extends KtAbstractLambdaWrapper> implements Query>, QueryLabel>, Chain, SelectWrapper> { @@ -205,11 +202,12 @@ public class KtLambdaWrapper extends KtAbstractLambdaWrapper selectSub(Class clazz, String st, Consumer> consumer, KProperty alias) { - KtLambdaWrapper wrapper = new KtLambdaWrapper(null, clazz, SharedString.emptyString(), paramNameSeq, paramNameValuePairs, - new MergeSegments(), SharedString.emptyString(), this.paramAlias, SharedString.emptyString(), SharedString.emptyString(), - new TableList(), null, null, null, null) { + public KtLambdaWrapper selectSub(Class clazz, String st, + Consumer> consumer, KProperty alias) { + KtLambdaWrapper wrapper = new KtLambdaWrapper(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) { }; wrapper.tableList.setAlias(st); wrapper.tableList.setRootClass(clazz); @@ -218,22 +216,29 @@ public class KtLambdaWrapper extends KtAbstractLambdaWrapper KtWrapperUtils.buildSubSqlByWrapper(clazz, wrapper, alias.getName()), hasAlias, this.alias)); + this.selectColumns.add(new SelectSub(() -> KtWrapperUtils.buildSubSqlByWrapper( + clazz, wrapper, alias.getName()), hasAlias, this.alias)); return typedThis; } /** * union + *

+ * 推荐使用 union(Class clazz, Consumer> consumer) + * 例: wrapper.union(UserDO.class, union -> union.selectAll(UserDO.class)) + * + * @see #union(Class, Consumer) + * @deprecated union 不支持子查询 */ - @SuppressWarnings("UnusedReturnValue") + @Deprecated + @SuppressWarnings("DeprecatedIsStillUsed") public final KtLambdaWrapper union(KtLambdaWrapper... wrappers) { StringBuilder sb = new StringBuilder(); for (KtLambdaWrapper wrapper : wrappers) { addCustomWrapper(wrapper); Class entityClass = wrapper.getEntityClass(); Assert.notNull(entityClass, "请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); - sb.append(" UNION ") - .append(KtWrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); + sb.append(" UNION ").append(KtWrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); } if (Objects.isNull(unionSql)) { unionSql = SharedString.emptyString(); @@ -243,17 +248,45 @@ public class KtLambdaWrapper extends KtAbstractLambdaWrapper + * 例: wrapper.union(UserDO.class, union -> union.selectAll(UserDO.class)) + * + * @param clazz union语句的主表类型 + * @since 1.4.7.3 */ - @SafeVarargs - public final KtLambdaWrapper unionAll(KtLambdaWrapper... wrappers) { + public KtLambdaWrapper union(Class clazz, Consumer> consumer) { + KtLambdaWrapper unionWrapper = KtWrappers.query(clazz); + addCustomWrapper(unionWrapper); + consumer.accept(unionWrapper); + + String sb = " UNION " + KtWrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper); + + if (Objects.isNull(unionSql)) { + unionSql = SharedString.emptyString(); + } + unionSql.setStringValue(unionSql.getStringValue() + sb); + return typedThis; + } + + /** + * union + *

+ * 推荐使用 unionAll(Class clazz, Consumer> consumer) + * 例: wrapper.unionAll(UserDO.class, union -> union.selectAll(UserDO.class)) + * + * @see #unionAll(Class, Consumer) + * @deprecated union 不支持子查询 + */ + @Deprecated + @SuppressWarnings("DeprecatedIsStillUsed") + public final KtLambdaWrapper unionAll(KtLambdaWrapper... wrappers) { StringBuilder sb = new StringBuilder(); for (KtLambdaWrapper wrapper : wrappers) { addCustomWrapper(wrapper); Class entityClass = wrapper.getEntityClass(); Assert.notNull(entityClass, "请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); - sb.append(" UNION ALL ") - .append(KtWrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); + sb.append(" UNION ALL ").append(KtWrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); } if (Objects.isNull(unionSql)) { unionSql = SharedString.emptyString(); @@ -262,7 +295,28 @@ public class KtLambdaWrapper extends KtAbstractLambdaWrapper + * 例: wrapper.unionAll(UserDO.class, union -> union.selectAll(UserDO.class)) + * + * @param clazz union语句的主表类型 + * @since 1.4.7.3 + */ + public KtLambdaWrapper unionAll(Class clazz, Consumer> consumer) { + KtLambdaWrapper unionWrapper = KtWrappers.query(clazz); + addCustomWrapper(unionWrapper); + consumer.accept(unionWrapper); + + String sb = " UNION ALL " + KtWrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper); + + if (Objects.isNull(unionSql)) { + unionSql = SharedString.emptyString(); + } + unionSql.setStringValue(unionSql.getStringValue() + sb); + return typedThis; + } + private void addCustomWrapper(KtLambdaWrapper wrapper) { if (Objects.isNull(wrapperIndex)) { wrapperIndex = new AtomicInteger(0); @@ -280,7 +334,6 @@ public class KtLambdaWrapper extends KtAbstractLambdaWrapper { @@ -349,7 +402,7 @@ public class KtLambdaWrapper extends KtAbstractLambdaWrapper instance(Integer index, String keyWord, Class joinClass, String tableName) { return new KtLambdaWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs, - new MergeSegments(), SharedString.emptyString(), this.paramAlias, SharedString.emptyString(), SharedString.emptyString(), + new MergeSegments(), this.paramAlias, SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), this.tableList, index, keyWord, joinClass, tableName); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/KtWrapperUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/KtWrapperUtils.java index 4a3a96b..242fb92 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/KtWrapperUtils.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/KtWrapperUtils.java @@ -16,9 +16,9 @@ import java.util.Optional; * @author yulichang * @since 1.4.6 */ +@SuppressWarnings("DuplicatedCode") public class KtWrapperUtils { - @SuppressWarnings("DuplicatedCode") public static String buildSubSqlByWrapper(Class clazz, KtLambdaWrapper wrapper, String alias) { TableInfo tableInfo = TableHelper.get(clazz); Asserts.hasTable(tableInfo, clazz); @@ -53,7 +53,6 @@ public class KtWrapperUtils { alias); } - @SuppressWarnings("DuplicatedCode") public static String buildUnionSqlByWrapper(Class clazz, KtLambdaWrapper wrapper) { TableInfo tableInfo = TableHelper.get(clazz); Asserts.hasTable(tableInfo, clazz); @@ -94,7 +93,6 @@ public class KtWrapperUtils { return SqlScriptUtils.safeParam(paramStr, null); } - @SuppressWarnings("DuplicatedCode") private static String getEntitySql(TableInfo tableInfo, KtLambdaWrapper wrapper) { Object obj = wrapper.getEntity(); if (Objects.isNull(obj)) { @@ -126,6 +124,9 @@ public class KtWrapperUtils { } private static String mainLogic(boolean hasWhere, Class clazz, KtLambdaWrapper wrapper) { + if (!wrapper.getLogicSql()) { + return StringPool.EMPTY; + } String info = LogicInfoUtils.getLogicInfo(null, clazz, true, wrapper.getAlias()); if (StringUtils.isNotBlank(info)) { if (hasWhere) { diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java index 84a669b..442978d 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java @@ -700,7 +700,6 @@ public abstract class MPJAbstractWrapper extends MPJAbstractLambdaWrapper> implements Query>, QueryLabel>, Chain, SelectWrapper> { @@ -205,10 +203,10 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectSub(Class clazz, String st, Consumer> consumer, SFunction alias) { - MPJLambdaWrapper wrapper = new MPJLambdaWrapper(null, clazz, SharedString.emptyString(), paramNameSeq, paramNameValuePairs, - new MergeSegments(), this.paramAlias, SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), + MPJLambdaWrapper wrapper = 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) { }; wrapper.tableList.setAlias(st); @@ -224,16 +222,22 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper + * 推荐使用 union(Class clazz, Consumer> consumer) + * 例: wrapper.union(UserDO.class, union -> union.selectAll(UserDO.class)) + * + * @see #union(Class, Consumer) + * @deprecated union 不支持子查询 */ - @SuppressWarnings("UnusedReturnValue") + @Deprecated + @SuppressWarnings({"UnusedReturnValue", "DeprecatedIsStillUsed"}) public final MPJLambdaWrapper union(MPJLambdaWrapper... wrappers) { StringBuilder sb = new StringBuilder(); for (MPJLambdaWrapper wrapper : wrappers) { addCustomWrapper(wrapper); Class entityClass = wrapper.getEntityClass(); Assert.notNull(entityClass, "请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); - sb.append(" UNION ") - .append(WrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); + sb.append(" UNION ").append(WrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); } if (Objects.isNull(unionSql)) { unionSql = SharedString.emptyString(); @@ -243,17 +247,45 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper + * 例: wrapper.union(UserDO.class, union -> union.selectAll(UserDO.class)) + * + * @param clazz union语句的主表类型 + * @since 1.4.7.3 */ - @SuppressWarnings("UnusedReturnValue") - public final MPJLambdaWrapper unionAll(MPJLambdaWrapper... wrappers) { + public MPJLambdaWrapper union(Class clazz, Consumer> consumer) { + MPJLambdaWrapper unionWrapper = JoinWrappers.lambda(clazz); + addCustomWrapper(unionWrapper); + consumer.accept(unionWrapper); + + String sb = " UNION " + WrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper); + + if (Objects.isNull(unionSql)) { + unionSql = SharedString.emptyString(); + } + unionSql.setStringValue(unionSql.getStringValue() + sb); + return typedThis; + } + + /** + * union + *

+ * 推荐使用 unionAll(Class clazz, Consumer> consumer) + * 例: wrapper.unionAll(UserDO.class, union -> union.selectAll(UserDO.class)) + * + * @see #unionAll(Class, Consumer) + * @deprecated union 不支持子查询 + */ + @Deprecated + @SuppressWarnings("DeprecatedIsStillUsed") + public MPJLambdaWrapper unionAll(MPJLambdaWrapper... wrappers) { StringBuilder sb = new StringBuilder(); for (MPJLambdaWrapper wrapper : wrappers) { addCustomWrapper(wrapper); Class entityClass = wrapper.getEntityClass(); Assert.notNull(entityClass, "请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); - sb.append(" UNION ALL ") - .append(WrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); + sb.append(" UNION ALL ").append(WrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); } if (Objects.isNull(unionSql)) { unionSql = SharedString.emptyString(); @@ -262,7 +294,28 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper + * 例: wrapper.unionAll(UserDO.class, union -> union.selectAll(UserDO.class)) + * + * @param clazz union语句的主表类型 + * @since 1.4.7.3 + */ + public MPJLambdaWrapper unionAll(Class clazz, Consumer> consumer) { + MPJLambdaWrapper unionWrapper = JoinWrappers.lambda(clazz); + addCustomWrapper(unionWrapper); + consumer.accept(unionWrapper); + + String sb = " UNION ALL " + WrapperUtils.buildUnionSqlByWrapper(clazz, unionWrapper); + + if (Objects.isNull(unionSql)) { + unionSql = SharedString.emptyString(); + } + unionSql.setStringValue(unionSql.getStringValue() + sb); + return typedThis; + } + private void addCustomWrapper(MPJLambdaWrapper wrapper) { if (Objects.isNull(wrapperIndex)) { wrapperIndex = new AtomicInteger(0); @@ -280,7 +333,6 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper { 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 752716b..c6cbb4e 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 @@ -1074,30 +1074,6 @@ class LambdaWrapperTest { } } - /** - * select 子查询 - */ - @Test - void sub() { - ThreadLocalUtils.set("SELECT (SELECT st.id FROM `user` st WHERE st.del = false AND (st.id = t.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 wrapper = JoinWrappers.lambda(UserDO.class) - .selectSub(UserDO.class, w -> w.select(UserDO::getId) - .eq(UserDO::getId, UserDO::getId) - .last("limit 1"), UserDO::getId) - .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) - .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(); - } - /** * select 子查询 */ @@ -1111,45 +1087,4 @@ class LambdaWrapperTest { .orderByDesc("t.id"); wrapper.list(); } - - /** - * select 子查询 - */ - @Test - void union() { - 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 WHERE t.del=false AND (t.id = ?) UNION 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 WHERE (t.`name` = ? AND (t.`name` = ?)) UNION 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 WHERE t.del=false AND (t.pid = ?)"); - MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) - .selectAll(UserDO.class) - .eq(UserDO::getId, 1); - MPJLambdaWrapper wrapper1 = JoinWrappers.lambda(UserDO.class) - .selectAll(UserDO.class) - .disableLogicDel() - .eq(UserDO::getName, "张三 2") - .and(w -> w.eq(UserDO::getName, "张三 2")); - MPJLambdaWrapper wrapper2 = JoinWrappers.lambda(UserDO.class) - .selectAll(UserDO.class) - .eq(UserDO::getPid, 2); - wrapper.union(wrapper1, wrapper2); - List list = wrapper.list(); - - assert list.size() == 7; - } - - @Test - void unionAll() { - ThreadLocalUtils.set("SELECT t.id FROM `user` t WHERE t.del = false AND (t.id = ?) UNION ALL SELECT t.id FROM address t UNION ALL SELECT t.id FROM area t WHERE t.del = false AND (t.id = ?)"); - MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) - .select(UserDO::getId) - .eq(UserDO::getId, 1); - MPJLambdaWrapper wrapper1 = JoinWrappers.lambda(AddressDO.class) - .select(AddressDO::getId) - .disableLogicDel(); - MPJLambdaWrapper wrapper2 = JoinWrappers.lambda(AreaDO.class) - .select(AreaDO::getId) - .eq(AreaDO::getId, 2); - wrapper.unionAll(wrapper1, wrapper2); - List list = wrapper.list(); - - assert list.size() == 23 && list.get(0).getId() != null; - } } diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/SelectSubTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/SelectSubTest.java new file mode 100644 index 0000000..6eaef3e --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/SelectSubTest.java @@ -0,0 +1,51 @@ +package com.github.yulichang.test.join.m; + +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.MPJLambdaWrapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class SelectSubTest { + + @BeforeEach + void setUp() { + Reset.reset(); + } + + + /** + * select 子查询 + */ + @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 <= ?)"); + MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) + .selectSub(UserDO.class, w -> w.select(UserDO::getId) + .eq(UserDO::getId, UserDO::getId) + .eq(UserDO::getId, 2) + .last("limit 1"), UserDO::getId) + .selectSub(UserDO.class, w -> w.select(UserDO::getId) + .eq(UserDO::getId, UserDO::getId) + .eq(UserDO::getId, 3) + .last("limit 1"), UserDO::getName) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .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(); + } +} diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/UnionTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/UnionTest.java new file mode 100644 index 0000000..cc968aa --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/m/UnionTest.java @@ -0,0 +1,85 @@ +package com.github.yulichang.test.join.m; + +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.MPJLambdaWrapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +@SuppressWarnings("deprecation") +@SpringBootTest +public class UnionTest { + + @BeforeEach + void setUp() { + Reset.reset(); + } + + + @Test + void union() { + 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 WHERE t.del=false AND (t.id = ?) UNION 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 WHERE (t.`name` = ? AND (t.`name` = ?)) UNION 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 WHERE t.del=false AND (t.pid = ?)"); + MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) + .selectAll(UserDO.class) + .eq(UserDO::getId, 1); + MPJLambdaWrapper wrapper1 = JoinWrappers.lambda(UserDO.class) + .selectAll(UserDO.class) + .disableLogicDel() + .eq(UserDO::getName, "张三 2") + .and(w -> w.eq(UserDO::getName, "张三 2")); + MPJLambdaWrapper wrapper2 = JoinWrappers.lambda(UserDO.class) + .selectAll(UserDO.class) + .eq(UserDO::getPid, 2); + wrapper.union(wrapper1, wrapper2); + List list = wrapper.list(); + + System.out.println(wrapper.getUnionSql()); + assert list.size() == 7 && list.get(0).getId() != null; + } + + @Test + void unionAll() { + ThreadLocalUtils.set("SELECT t.id FROM `user` t WHERE t.del = false AND (t.id = ?) UNION ALL SELECT t.id FROM address t UNION ALL SELECT t.id FROM area t WHERE t.del = false AND (t.id = ?)"); + MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) + .select(UserDO::getId) + .eq(UserDO::getId, 1); + MPJLambdaWrapper wrapper1 = JoinWrappers.lambda(AddressDO.class) + .select(AddressDO::getId) + .disableLogicDel(); + MPJLambdaWrapper wrapper2 = JoinWrappers.lambda(AreaDO.class) + .select(AreaDO::getId) + .eq(AreaDO::getId, 2); + wrapper.unionAll(wrapper1, wrapper2); + List list = wrapper.list(); + + assert list.size() == 23 && list.get(0).getId() != null; + } + + @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 = ?))"); + MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) + .select(UserDO::getId) + .eq(UserDO::getId, 1) + .unionAll(AddressDO.class, union -> union + .select(AddressDO::getId) + .disableLogicDel() + .eq(UserDO::getId, 2)) + .unionAll(AreaDO.class, union -> union + .selectSub(AreaDO.class, sub -> sub + .select(AreaDO::getId) + .eq(AreaDO::getId, 3) + .and(and -> and.eq(AreaDO::getId, 4)), AreaDO::getId) + .eq(AreaDO::getId, 5) + .and(and -> and.eq(AreaDO::getId, 6))); + List list = wrapper.list(); + assert list.size() == 2 && list.get(0).getId() != null; + } +} diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/KtApplication.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/KtApplication.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/KtApplication.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/KtApplication.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/dto/AddressDTO.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/dto/AddressDTO.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/dto/AddressDTO.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/dto/AddressDTO.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/dto/AreaDTO.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/dto/AreaDTO.kt new file mode 100644 index 0000000..3468fd4 --- /dev/null +++ b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/dto/AreaDTO.kt @@ -0,0 +1,12 @@ +package com.github.yulichang.test.kt.dto + + +@Suppress("unused") +class AreaDTO { + val id: Int? = null + val province: String? = null + val city: String? = null + val area: String? = null + val postcode: String? = null + val del: Boolean? = null +} diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/dto/UserDTO.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/dto/UserDTO.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/dto/UserDTO.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/dto/UserDTO.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/AddressDO.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/AddressDO.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/AddressDO.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/AddressDO.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/AddressGeneric.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/AddressGeneric.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/AddressGeneric.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/AddressGeneric.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/AreaDO.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/AreaDO.kt similarity index 93% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/AreaDO.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/AreaDO.kt index cf8f01c..16f06f3 100644 --- a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/AreaDO.kt +++ b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/AreaDO.kt @@ -13,7 +13,7 @@ class AreaDO : Serializable { var province: String? = null var city: String? = null var area: String? = null - var postcode: String? = null + var Postcode: String? = null @TableLogic var del: Boolean? = null diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/ID.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/ID.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/ID.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/ID.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/OrderDO.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/OrderDO.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/OrderDO.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/OrderDO.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/UserDO.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/UserDO.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/UserDO.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/UserDO.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/UserDto.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/UserDto.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/UserDto.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/UserDto.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/UserTTT.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/UserTTT.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/entity/UserTTT.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/entity/UserTTT.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/enums/Sex.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/enums/Sex.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/enums/Sex.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/enums/Sex.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/AddressMapper.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/AddressMapper.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/AddressMapper.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/AddressMapper.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/AreaMapper.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/AreaMapper.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/AreaMapper.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/AreaMapper.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/MyBaseMapper.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/MyBaseMapper.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/MyBaseMapper.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/MyBaseMapper.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/OrderMapper.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/OrderMapper.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/OrderMapper.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/OrderMapper.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/UserDTOMapper.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/UserDTOMapper.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/UserDTOMapper.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/UserDTOMapper.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/UserMapper.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/UserMapper.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/mapper/UserMapper.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/mapper/UserMapper.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/service/UserService.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/service/UserService.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/service/UserService.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/service/UserService.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/service/impl/UserServiceImpl.kt b/mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/service/impl/UserServiceImpl.kt similarity index 100% rename from mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/service/impl/UserServiceImpl.kt rename to mybatis-plus-join-test/test-kotlin/src/main/java/com/github/yulichang/test/kt/service/impl/UserServiceImpl.kt diff --git a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/dto/AreaDTO.kt b/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/dto/AreaDTO.kt deleted file mode 100644 index e847f5b..0000000 --- a/mybatis-plus-join-test/test-kotlin/src/main/kotlin/com/github/yulichang/test/kt/dto/AreaDTO.kt +++ /dev/null @@ -1,12 +0,0 @@ -package com.github.yulichang.test.kt.dto - - -@Suppress("unused") -class AreaDTO { - private val id: Int? = null - private val province: String? = null - private val city: String? = null - private val area: String? = null - private val postcode: String? = null - private val del: Boolean? = null -} diff --git a/mybatis-plus-join-test/test-kotlin/src/test/com/github/yulichang/test/kt/LambdaWrapperTest.kt b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/LambdaWrapperTest.kt similarity index 94% rename from mybatis-plus-join-test/test-kotlin/src/test/com/github/yulichang/test/kt/LambdaWrapperTest.kt rename to mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/LambdaWrapperTest.kt index 43b61de..1be531a 100644 --- a/mybatis-plus-join-test/test-kotlin/src/test/com/github/yulichang/test/kt/LambdaWrapperTest.kt +++ b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/LambdaWrapperTest.kt @@ -698,7 +698,9 @@ class LambdaWrapperTest { .eq(UserDO::name, "ref") userMapper!!.selectList(wrapper) try { - userMapper.insertBatchSomeColumn(ArrayList()) + val arr = ArrayList() + arr.add(UserDO()) + userMapper.insertBatchSomeColumn(arr) } catch (ignored: BadSqlGrammarException) { } } @@ -821,7 +823,9 @@ class LambdaWrapperTest { .setTableName { name -> name + "bbbbbbb" } try { userMapper!!.selectJoinList(UserDTO::class.java, wrapper) - } catch (_: BadSqlGrammarException) { + } catch (a: Exception) { + if (a.message!!.contains("sql error")) + throw a } } @@ -1092,67 +1096,4 @@ class LambdaWrapperTest { //忽略异常 h2不支持连表删除 } } - - /** - * select 子查询 - */ - @Test - fun sub() { - ThreadLocalUtils.set("SELECT ( SELECT st.id FROM `user` st WHERE st.del=false AND (st.id = t.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 <= ?)") - val wrapper: KtLambdaWrapper = KtWrappers.query(UserDO::class.java) - .selectSub( - UserDO::class.java, { w -> - w.select(UserDO::id) - .eq(UserDO::id, UserDO::id) - .last("limit 1") - }, UserDO::id - ) - .leftJoin(AddressDO::class.java, AddressDO::userId, UserDO::id) - .le(UserDO::id, 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 <= ?)") - val wrapper1: KtLambdaWrapper = KtWrappers.query(UserDO::class.java) - .selectSub(AreaDO::class.java, { w -> - w.select(AreaDO::id) - .eq(AreaDO::id, AddressDO::id) - .last("limit 1") - }, UserDO::id) - .leftJoin(AddressDO::class.java, AddressDO::userId, UserDO::id) - .le(UserDO::id, 100) - wrapper1.list() - - ThreadLocalUtils.set("SELECT ( SELECT st.id FROM address 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 <= ?)") - val wrapper2: KtLambdaWrapper = KtWrappers.query(UserDO::class.java) - .selectSub(AddressDO::class.java, { w -> - w.select(AddressDO::id) - .eq(AddressDO::id, AddressDO::id) - .last("limit 1") - }, UserDO::id) - .leftJoin(AddressDO::class.java, AddressDO::userId, UserDO::id) - .le(UserDO::id, 100) - wrapper2.list() - } - - - /** - * select 子查询 - */ - @Test - fun union() { - val wrapper: KtLambdaWrapper = KtWrappers.query(UserDO::class.java) - .selectAll(UserDO::class.java) - .eq(UserDO::id, 1) - val wrapper1: KtLambdaWrapper = KtWrappers.query(UserDO::class.java) - .selectAll(UserDO::class.java) - .eq(UserDO::name, "张三 2") - .and { a -> a.eq(UserDO::name, "张三 2") } - val wrapper2: KtLambdaWrapper = KtWrappers.query(UserDO::class.java) - .selectAll(UserDO::class.java) - .eq(UserDO::pid, 2) - - wrapper.union(wrapper1, wrapper2) - val list = wrapper.list() - assert(list.size == 7) - } } diff --git a/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/CustomWrapperTest.kt b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/CustomWrapperTest.kt new file mode 100644 index 0000000..e0f0b9c --- /dev/null +++ b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/CustomWrapperTest.kt @@ -0,0 +1,57 @@ +package com.github.yulichang.test.kt.m + +import com.github.yulichang.kt.KtLambdaWrapper +import com.github.yulichang.test.kt.entity.UserDO +import com.github.yulichang.test.kt.mapper.UserMapper +import com.github.yulichang.test.util.Reset +import com.github.yulichang.test.util.ThreadLocalUtils +import com.github.yulichang.toolkit.Ref +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import java.util.* +import kotlin.reflect.KProperty + +@SpringBootTest +class CustomWrapperTest { + @Autowired + private val userMapper: UserMapper? = null + + @BeforeEach + fun setUp() { + Reset.reset() + } + + //自定义wrapper扩展 + class CWrapper : KtLambdaWrapper() { + fun eqIfAbsent(column: KProperty<*>, `val`: Any?): CWrapper { + super.eq(Objects.nonNull(`val`), column, `val`) + return this + } + + companion object { + fun toCWrapper(): CWrapper? { + return null + } + } + } + + @Test + fun testWrapperCustomer() { + 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 WHERE t.del = false AND (t.id = ?)") + val wrapper: CWrapper = CWrapper() + .selectAll(UserDO::class.java) + .toChildren> { CWrapper.toCWrapper() } + .eqIfAbsent(UserDO::id, 1) + val dos = userMapper?.selectList(wrapper) + dos?.forEach(System.out::println) + 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 WHERE t.del = false") + val wrapper1: CWrapper = CWrapper() + .selectAll(UserDO::class.java) + .toChildren(Ref>()) + .eqIfAbsent(UserDO::id, null) + val dos1 = userMapper?.selectList(wrapper1) + dos1?.forEach(System.out::println) + } +} diff --git a/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/FieldNameTest.kt b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/FieldNameTest.kt new file mode 100644 index 0000000..6c026eb --- /dev/null +++ b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/FieldNameTest.kt @@ -0,0 +1,41 @@ +package com.github.yulichang.test.kt.m + +import com.github.yulichang.test.kt.dto.AreaDTO +import com.github.yulichang.test.kt.entity.AreaDO +import com.github.yulichang.test.kt.entity.UserDto +import com.github.yulichang.test.kt.mapper.AreaMapper +import com.github.yulichang.test.util.Reset +import com.github.yulichang.toolkit.KtWrappers +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +class FieldNameTest { + @Autowired + private val areaMapper: AreaMapper? = null + + @BeforeEach + fun setUp() { + Reset.reset() + } + + @Test + fun testFieldName() { + val list = areaMapper?.selectJoinList(AreaDO::class.java, KtWrappers.query(AreaDO::class.java) + .select(AreaDO::Postcode) + .leftJoin(UserDto::class.java, UserDto::id, AreaDO::id)) + list?.forEach(System.out::println) + assert(list?.get(0)?.Postcode != null) + } + + @Test + fun testFieldName1() { + val list = areaMapper?.selectJoinList(AreaDTO::class.java, KtWrappers.query(AreaDO::class.java) + .selectAs(AreaDO::Postcode, AreaDTO::postcode) + .leftJoin(UserDto::class.java, UserDto::id, AreaDO::id)) + list?.forEach(System.out::println) + assert(list?.get(0)?.postcode != null) + } +} diff --git a/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/SelectSubTest.kt b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/SelectSubTest.kt new file mode 100644 index 0000000..a59106c --- /dev/null +++ b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/SelectSubTest.kt @@ -0,0 +1,53 @@ +package com.github.yulichang.test.kt.m + +import com.github.yulichang.test.kt.entity.AddressDO +import com.github.yulichang.test.kt.entity.AreaDO +import com.github.yulichang.test.kt.entity.UserDO +import com.github.yulichang.test.util.ThreadLocalUtils +import com.github.yulichang.toolkit.KtWrappers +import org.junit.jupiter.api.BeforeEach +import org.springframework.boot.test.context.SpringBootTest + +@SpringBootTest +class SelectSubTest { + @BeforeEach + fun setUp() { + com.github.yulichang.test.util.Reset.reset() + } + + /** + * select 子查询 + */ + @org.junit.jupiter.api.Test + fun 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 <= ?)") + val wrapper = KtWrappers.query(UserDO::class.java) + .selectSub(UserDO::class.java, { w -> + w.select(UserDO::id) + .eq(UserDO::id, UserDO::id) + .eq(UserDO::id, 2) + .last("limit 1") + }, UserDO::id) + .selectSub(UserDO::class.java, { w -> + w.select(UserDO::id) + .eq(UserDO::id, UserDO::id) + .eq(UserDO::id, 3) + .last("limit 1") + }, UserDO::name) + .leftJoin(AddressDO::class.java, AddressDO::userId, UserDO::id) + .le(UserDO::id, 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 <= ?)") + val wrapper1 = KtWrappers.query(UserDO::class.java) + .selectSub(AreaDO::class.java, { w -> + w.select(AreaDO::id) + .eq(AreaDO::id, AddressDO::id) + .last("limit 1") + }, UserDO::id) + .leftJoin(AddressDO::class.java, AddressDO::userId, UserDO::id) + .le(UserDO::id, 100) + wrapper1.list() + } +} diff --git a/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/UnionTest.kt b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/UnionTest.kt new file mode 100644 index 0000000..3e179c7 --- /dev/null +++ b/mybatis-plus-join-test/test-kotlin/src/test/java/com/github/yulichang/test/kt/m/UnionTest.kt @@ -0,0 +1,83 @@ +package com.github.yulichang.test.kt.m + +import com.github.yulichang.test.kt.entity.AddressDO +import com.github.yulichang.test.kt.entity.AreaDO +import com.github.yulichang.test.kt.entity.UserDO +import com.github.yulichang.test.util.Reset +import com.github.yulichang.test.util.ThreadLocalUtils +import com.github.yulichang.toolkit.KtWrappers +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.springframework.boot.test.context.SpringBootTest + +@Suppress("DEPRECATION") +@SpringBootTest +class UnionTest { + @BeforeEach + fun setUp() { + Reset.reset() + } + + @Test + fun union() { + 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 WHERE t.del=false AND (t.id = ?) UNION 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 WHERE (t.`name` = ? AND (t.`name` = ?)) UNION 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 WHERE t.del=false AND (t.pid = ?)") + val wrapper = KtWrappers.query(UserDO::class.java) + .selectAll(UserDO::class.java) + .eq(UserDO::id, 1) + val wrapper1 = KtWrappers.query(UserDO::class.java) + .selectAll(UserDO::class.java) + .disableLogicDel() + .eq(UserDO::name, "张三 2") + .and { w -> w.eq(UserDO::name, "张三 2") } + val wrapper2 = KtWrappers.query(UserDO::class.java) + .selectAll(UserDO::class.java) + .eq(UserDO::pid, 2) + wrapper.union(wrapper1, wrapper2) + val list: List = wrapper.list() + println(wrapper.unionSql) + assert(list.size == 7 && list[0].id != null) + } + + + @Test + fun unionAll() { + ThreadLocalUtils.set("SELECT t.id FROM `user` t WHERE t.del = false AND (t.id = ?) UNION ALL SELECT t.id FROM address t UNION ALL SELECT t.id FROM area t WHERE t.del = false AND (t.id = ?)") + val wrapper = KtWrappers.query(UserDO::class.java) + .select(UserDO::id) + .eq(UserDO::id, 1) + val wrapper1 = KtWrappers.query(AddressDO::class.java) + .select(AddressDO::id) + .disableLogicDel() + val wrapper2 = KtWrappers.query(AreaDO::class.java) + .select(AreaDO::id) + .eq(AreaDO::id, 2) + wrapper.unionAll(wrapper1, wrapper2) + val list: List = wrapper.list() + assert(list.size == 23 && list[0].id != null) + } + + @Test + fun 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 = ?))") + val wrapper = KtWrappers.query(UserDO::class.java) + .select(UserDO::id) + .eq(UserDO::id, 1) + .unionAll(AddressDO::class.java) { union -> + union.select(AddressDO::id) + .disableLogicDel() + .eq(UserDO::id, 2) + } + .unionAll(AreaDO::class.java) { union -> + union.selectSub(AreaDO::class.java, { sub -> + sub.select(AreaDO::id) + .eq(AreaDO::id, 3) + .and { and -> and.eq(AreaDO::id, 4) } + }, AreaDO::id) + .eq(AreaDO::id, 5) + .and { and -> and.eq(AreaDO::id, 6) } + } + println(wrapper.unionSql) + val list: List = wrapper.list() + assert(list.size == 2 && list[0].id != null) + } +}