diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interfaces/MPJBaseJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interfaces/MPJBaseJoin.java index 2fbf71e..aeb1ba2 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interfaces/MPJBaseJoin.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interfaces/MPJBaseJoin.java @@ -15,4 +15,8 @@ public interface MPJBaseJoin { default String getDeleteLogicSql() { return StringPool.EMPTY; } + + default String getUnionSql(){ + return StringPool.EMPTY; + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinList.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinList.java index 3667c3f..f18ca33 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinList.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinList.java @@ -1,6 +1,10 @@ package com.github.yulichang.method; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; @@ -30,4 +34,10 @@ public class SelectJoinList extends MPJAbstractMethod { SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMap.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMap.java index 2951078..74fd719 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMap.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMap.java @@ -1,6 +1,10 @@ package com.github.yulichang.method; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; @@ -31,4 +35,10 @@ public class SelectJoinMap extends MPJAbstractMethod { SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMaps.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMaps.java index 8e0c67a..7db4c14 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMaps.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMaps.java @@ -1,6 +1,10 @@ package com.github.yulichang.method; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; @@ -32,4 +36,10 @@ public class SelectJoinMaps extends MPJAbstractMethod { SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMapsPage.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMapsPage.java index 2c4169c..da609a2 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMapsPage.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinMapsPage.java @@ -1,6 +1,10 @@ package com.github.yulichang.method; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; @@ -32,4 +36,10 @@ public class SelectJoinMapsPage extends MPJAbstractMethod { SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinOne.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinOne.java index 596c076..3ded876 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinOne.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinOne.java @@ -1,6 +1,10 @@ package com.github.yulichang.method; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; @@ -29,4 +33,10 @@ public class SelectJoinOne extends MPJAbstractMethod { SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinPage.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinPage.java index f3b3d2a..49a512a 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinPage.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinPage.java @@ -1,6 +1,10 @@ package com.github.yulichang.method; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.StringPool; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.SqlSource; @@ -30,4 +34,10 @@ public class SelectJoinPage extends MPJAbstractMethod { SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectList.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectList.java index fb6cc63..90233ff 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectList.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectList.java @@ -45,4 +45,10 @@ public class SelectList extends com.baomidou.mybatisplus.core.injector.methods.S return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMaps.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMaps.java index 2485317..3b3a312 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMaps.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMaps.java @@ -45,4 +45,10 @@ public class SelectMaps extends com.baomidou.mybatisplus.core.injector.methods.S return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java index 41e08a2..b9df82d 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectMapsPage.java @@ -45,4 +45,10 @@ public class SelectMapsPage extends com.baomidou.mybatisplus.core.injector.metho return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectObjs.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectObjs.java index d6543a8..46f12ef 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectObjs.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectObjs.java @@ -39,4 +39,10 @@ public class SelectObjs extends com.baomidou.mybatisplus.core.injector.methods.S return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectOne.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectOne.java index 4d3f74f..1f93b0e 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectOne.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectOne.java @@ -44,4 +44,10 @@ public class SelectOne extends com.baomidou.mybatisplus.core.injector.methods.Se return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectPage.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectPage.java index 371cb68..5d20d10 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectPage.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectPage.java @@ -45,4 +45,10 @@ public class SelectPage extends com.baomidou.mybatisplus.core.injector.methods.S return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } + + @Override + protected String sqlComment() { + return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", + Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/WrapperUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/WrapperUtils.java index 3416ab5..68772e0 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/WrapperUtils.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/WrapperUtils.java @@ -19,7 +19,7 @@ import java.util.Optional; */ public class WrapperUtils { - public static String buildSqlByWrapper(Class clazz, MPJLambdaWrapper wrapper, SFunction alias) { + public static String buildSubSqlByWrapper(Class clazz, MPJLambdaWrapper wrapper, SFunction alias) { TableInfo tableInfo = TableHelper.get(clazz); Asserts.hasTable(tableInfo, clazz); String first = Optional.ofNullable(wrapper.getSqlFirst()).orElse(StringPool.EMPTY); @@ -40,7 +40,7 @@ public class WrapperUtils { ((wrapper.isEmptyOfNormal() ? StringPool.EMPTY : (hasWhere ? " AND " : " WHERE ")) + wrapper.getSqlSegment()) : StringPool.EMPTY; String sqlComment = Optional.ofNullable(wrapper.getSqlComment()).orElse(StringPool.EMPTY); - return String.format("(%s SELECT %s FROM %s %s %s %s %s %s %s) AS %s", + return String.format(" (%s SELECT %s FROM %s %s %s %s %s %s %s) AS %s ", first, wrapper.getSqlSelect(), tableInfo.getTableName(), @@ -53,6 +53,39 @@ public class WrapperUtils { LambdaUtils.getName(alias)); } + public static String buildUnionSqlByWrapper(Class clazz, MPJLambdaWrapper wrapper) { + TableInfo tableInfo = TableHelper.get(clazz); + Asserts.hasTable(tableInfo, clazz); + String first = Optional.ofNullable(wrapper.getSqlFirst()).orElse(StringPool.EMPTY); + boolean hasWhere = false; + String entityWhere = getEntitySql(tableInfo, wrapper); + if (StringUtils.isNotBlank(entityWhere)) { + hasWhere = true; + } + String mainLogic = mainLogic(hasWhere, clazz, wrapper); + if (StringUtils.isNotBlank(mainLogic)) { + hasWhere = true; + } + String subLogic = subLogic(hasWhere, wrapper); + if (StringUtils.isNotBlank(subLogic)) { + hasWhere = true; + } + String sqlSegment = (wrapper.getSqlSegment() != null && StringUtils.isNotBlank(wrapper.getSqlSegment())) ? + ((wrapper.isEmptyOfNormal() ? StringPool.EMPTY : (hasWhere ? " AND " : " WHERE ")) + wrapper.getSqlSegment()) : StringPool.EMPTY; + + String sqlComment = Optional.ofNullable(wrapper.getSqlComment()).orElse(StringPool.EMPTY); + return String.format(" %s SELECT %s FROM %s %s %s %s %s %s %s ", + first, + wrapper.getSqlSelect(), + tableInfo.getTableName(), + wrapper.getAlias(), + wrapper.getFrom(), + mainLogic, + subLogic, + sqlSegment, + sqlComment); + } + private static String formatParam(MPJLambdaWrapper wrapper, Object param) { final String genParamName = Constants.WRAPPER_PARAM + wrapper.getParamNameSeq().incrementAndGet(); final String paramStr = wrapper.getParamAlias() + ".paramNameValuePairs." + genParamName; @@ -60,8 +93,8 @@ public class WrapperUtils { return SqlScriptUtils.safeParam(paramStr, null); } - private static String getEntitySql(TableInfo tableInfo, MPJLambdaWrapper wrapper) { - T obj = wrapper.getEntity(); + private static String getEntitySql(TableInfo tableInfo, MPJLambdaWrapper wrapper) { + Object obj = wrapper.getEntity(); if (Objects.isNull(obj)) { return StringPool.EMPTY; } @@ -90,7 +123,7 @@ public class WrapperUtils { return sb.toString(); } - private static String mainLogic(boolean hasWhere, Class clazz, MPJLambdaWrapper wrapper) { + private static String mainLogic(boolean hasWhere, Class clazz, MPJLambdaWrapper wrapper) { String info = LogicInfoUtils.getLogicInfo(null, clazz, true, wrapper.getAlias()); if (StringUtils.isNotBlank(info)) { if (hasWhere) { @@ -101,7 +134,7 @@ public class WrapperUtils { return StringPool.EMPTY; } - private static String subLogic(boolean hasWhere, MPJLambdaWrapper wrapper) { + private static String subLogic(boolean hasWhere, MPJLambdaWrapper wrapper) { String sql = wrapper.getSubLogicSql(); if (StringUtils.isNotBlank(sql)) { if (hasWhere) { 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 df6cc9d..925d409 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 @@ -2,10 +2,7 @@ package com.github.yulichang.wrapper; import com.baomidou.mybatisplus.core.conditions.SharedString; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; -import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; -import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; -import com.baomidou.mybatisplus.core.toolkit.StringPool; -import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; @@ -56,6 +53,11 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper> resultMapMybatisLabel = new ArrayList<>(); + /** + * union sql + */ + private SharedString unionSql; + /** * 推荐使用 带 class 的构造方法 @@ -190,11 +192,49 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper union(MPJLambdaWrapper... wrappers) { + StringBuilder sb = new StringBuilder(); + for (MPJLambdaWrapper wrapper : wrappers) { + Class entityClass = wrapper.getEntityClass(); + Assert.notNull(entityClass, "请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); + sb.append(" UNION ") + .append(WrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); + } + if (Objects.isNull(unionSql)) { + unionSql = SharedString.emptyString(); + } + unionSql.setStringValue(unionSql.getStringValue() + sb); + return typedThis; + } + + /** + * union all + */ + @SafeVarargs + public final MPJLambdaWrapper unionAll(MPJLambdaWrapper... wrappers) { + StringBuilder sb = new StringBuilder(); + for (MPJLambdaWrapper wrapper : wrappers) { + Class entityClass = wrapper.getEntityClass(); + Assert.notNull(entityClass, "请使用 new MPJLambdaWrapper(主表.class) 或 JoinWrappers.lambda(主表.class) 构造方法"); + sb.append(" UNION ALL ") + .append(WrapperUtils.buildUnionSqlByWrapper(entityClass, wrapper)); + } + if (Objects.isNull(unionSql)) { + unionSql = SharedString.emptyString(); + } + unionSql.setStringValue(unionSql.getStringValue() + sb); + return typedThis; + } + /** * 查询条件 SQL 片段 */ @@ -242,6 +282,10 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) .selectSub(UserDO.class, w -> w.select(UserDO::getId) - .le(UserDO::getId,1000) + .le(UserDO::getId, 1000) .last("limit 1"), UserDO::getPid) - .leftJoin(AddressDO.class,AddressDO::getUserId,UserDO::getId) - .le(UserDO::getId,100); + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .le(UserDO::getId, 100); + wrapper.list(); + System.out.println(1); + } + + + /** + * select 子查询 + */ + @Test + void union() { + MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) + .selectAll(UserDO.class); + MPJLambdaWrapper wrapper1 = JoinWrappers.lambda(UserDO.class) + .selectAll(UserDO.class); + MPJLambdaWrapper wrapper2 = JoinWrappers.lambda(UserDO.class) + .selectAll(UserDO.class); + + wrapper.union(wrapper1, wrapper2); wrapper.list(); System.out.println(1); }