diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java index 8867365..22a510a 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java @@ -30,7 +30,7 @@ public class MPJInterceptorConfig { System.out.println(" _ _ |_ _ _|_. ___ _ | _ . _ . _ \n" + "| | |\\/|_)(_| | |_\\ |_)||_|_\\ | (_) | | | \n" + " / | /\n" + - " 1.4.7"); + " 1.4.8"); } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java index feef86a..469abc9 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java @@ -6,9 +6,11 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.github.yulichang.adapter.v352.AbstractMethod352; import com.github.yulichang.config.ConfigProperties; +import org.apache.ibatis.builder.SqlSourceBuilder; import java.util.ArrayList; import java.util.List; +import java.util.StringTokenizer; /** * @author yulichang @@ -82,4 +84,22 @@ public abstract class MPJAbstractMethod extends AbstractMethod352 implements MPJ return ""; } } + + public String removeExtraWhitespaces(String sql) { + try { + return SqlSourceBuilder.removeExtraWhitespaces(sql); + } catch (Throwable t) { + StringTokenizer tokenizer = new StringTokenizer(sql); + StringBuilder builder = new StringBuilder(); + boolean hasMoreTokens = tokenizer.hasMoreTokens(); + while (hasMoreTokens) { + builder.append(tokenizer.nextToken()); + hasMoreTokens = tokenizer.hasMoreTokens(); + if (hasMoreTokens) { + builder.append(' '); + } + } + return builder.toString(); + } + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java index 6fc295b..b8c5baa 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/SelectJoinCount.java @@ -27,7 +27,7 @@ public class SelectJoinCount extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_COUNT; String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlCount(), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Long.class); } } 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 f18ca33..fcd4f17 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 @@ -31,7 +31,7 @@ public class SelectJoinList extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_LIST; String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class); } 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 74fd719..4c73c8b 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 @@ -32,7 +32,7 @@ public class SelectJoinMap extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAP; String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class); } 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 7db4c14..446cf80 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 @@ -33,7 +33,7 @@ public class SelectJoinMaps extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS; String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class); } 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 da609a2..60a1ed4 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 @@ -33,7 +33,7 @@ public class SelectJoinMapsPage extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_MAPS_PAGE; String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, Map.class); } 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 3ded876..6b1042e 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 @@ -30,7 +30,7 @@ public class SelectJoinOne extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_ONE; String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class); } 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 49a512a..f531bcb 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 @@ -31,7 +31,7 @@ public class SelectJoinPage extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.SELECT_JOIN_PAGE; String sql = String.format(sqlMethod.getSql(), sqlFirst(), sqlDistinct(), sqlSelectColumns(tableInfo, true), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), sqlWhereEntityWrapper(true, tableInfo), mpjSqlOrderBy(tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addSelectMappedStatementForOther(mapperClass, sqlMethod.getMethod(), sqlSource, MPJResultType.class); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/UpdateJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/UpdateJoin.java index aee7f04..c9fd322 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/UpdateJoin.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/UpdateJoin.java @@ -31,7 +31,7 @@ public class UpdateJoin extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.UPDATE_JOIN; String sql = String.format(sqlMethod.getSql(), sqlFirst(), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), mpjSqlSet(true, true, tableInfo, true, ENTITY, ENTITY_DOT), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/UpdateJoinAndNull.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/UpdateJoinAndNull.java index 3796cb7..320c136 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/UpdateJoinAndNull.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/UpdateJoinAndNull.java @@ -30,7 +30,7 @@ public class UpdateJoinAndNull extends MPJAbstractMethod { SqlMethod sqlMethod = SqlMethod.UPDATE_JOIN_AND_NULL; String sql = String.format(sqlMethod.getSql(), sqlFirst(), mpjTableName(tableInfo), sqlAlias(), sqlFrom(), mpjSqlSet(true, true, tableInfo, true, ENTITY, ENTITY_DOT), sqlWhereEntityWrapper(true, tableInfo), sqlComment()); - SqlSource sqlSource = languageDriver.createSqlSource(configuration, sql, modelClass); + SqlSource sqlSource = languageDriver.createSqlSource(configuration, removeExtraWhitespaces(sql), modelClass); return this.addUpdateMappedStatement(mapperClass, modelClass, sqlMethod.getMethod(), sqlSource); } 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 90233ff..bfa8ed5 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 @@ -3,6 +3,7 @@ package com.github.yulichang.method.mp; 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.StringUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; @@ -35,8 +36,12 @@ public class SelectList extends com.baomidou.mybatisplus.core.injector.methods.S @Override protected String sqlOrderBy(TableInfo table) { + String orderBy = super.sqlOrderBy(table); + if (StringUtils.isBlank(orderBy)) { + return orderBy; + } return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), - super.sqlOrderBy(table), mpjSqlOrderBy(table)); + orderBy, mpjSqlOrderBy(table)); } @Override 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 3b3a312..ed4ab63 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 @@ -3,6 +3,7 @@ package com.github.yulichang.method.mp; 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.StringUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; @@ -35,6 +36,10 @@ public class SelectMaps extends com.baomidou.mybatisplus.core.injector.methods.S @Override protected String sqlOrderBy(TableInfo table) { + String orderBy = super.sqlOrderBy(table); + if (StringUtils.isBlank(orderBy)) { + return orderBy; + } return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlOrderBy(table), mpjSqlOrderBy(table)); } 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 b9df82d..e22e3a6 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 @@ -3,6 +3,7 @@ package com.github.yulichang.method.mp; 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.StringUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; @@ -35,6 +36,10 @@ public class SelectMapsPage extends com.baomidou.mybatisplus.core.injector.metho @Override protected String sqlOrderBy(TableInfo table) { + String orderBy = super.sqlOrderBy(table); + if (StringUtils.isBlank(orderBy)) { + return orderBy; + } return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlOrderBy(table), mpjSqlOrderBy(table)); } 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 00988ad..546793b 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 @@ -3,6 +3,7 @@ package com.github.yulichang.method.mp; 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.StringUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; @@ -48,6 +49,16 @@ public class SelectObjs extends com.baomidou.mybatisplus.core.injector.methods.S selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } + @Override + protected String sqlOrderBy(TableInfo table) { + String orderBy = super.sqlOrderBy(table); + if (StringUtils.isBlank(orderBy)) { + return orderBy; + } + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), + super.sqlOrderBy(table), mpjSqlOrderBy(table)); + } + @Override protected String sqlComment() { return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", 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 1f93b0e..b8503e2 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 @@ -3,6 +3,7 @@ package com.github.yulichang.method.mp; 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.StringUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; @@ -45,6 +46,16 @@ public class SelectOne extends com.baomidou.mybatisplus.core.injector.methods.Se selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } + @Override + protected String sqlOrderBy(TableInfo table) { + String orderBy = super.sqlOrderBy(table); + if (StringUtils.isBlank(orderBy)) { + return orderBy; + } + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), + super.sqlOrderBy(table), mpjSqlOrderBy(table)); + } + @Override protected String sqlComment() { return super.sqlComment() + StringPool.NEWLINE + SqlScriptUtils.convertIf("${ew.unionSql}", String.format("%s != null and (%s instanceof %s)", 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 5d20d10..adf9e34 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 @@ -3,6 +3,7 @@ package com.github.yulichang.method.mp; 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.StringUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; @@ -35,6 +36,10 @@ public class SelectPage extends com.baomidou.mybatisplus.core.injector.methods.S @Override protected String sqlOrderBy(TableInfo table) { + String orderBy = super.sqlOrderBy(table); + if (StringUtils.isBlank(orderBy)) { + return orderBy; + } return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlOrderBy(table), mpjSqlOrderBy(table)); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java index 20ce880..8cab848 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java @@ -1,5 +1,6 @@ package com.github.yulichang.query; +import com.baomidou.mybatisplus.core.MybatisPlusVersion; import com.baomidou.mybatisplus.core.conditions.AbstractWrapper; import com.baomidou.mybatisplus.core.conditions.SharedString; import com.baomidou.mybatisplus.core.conditions.query.Query; @@ -8,6 +9,7 @@ import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.*; +import com.github.yulichang.adapter.base.tookit.VersionUtils; import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.query.interfaces.StringJoin; import com.github.yulichang.toolkit.Asserts; @@ -89,7 +91,13 @@ public class MPJQueryWrapper extends AbstractWrapper clazz) { - super.setEntityClass(clazz); + try { + super.setEntityClass(clazz); + } catch (NoSuchMethodError error) { + if (VersionUtils.compare(MybatisPlusVersion.getVersion(), "3.3.0") > 0) { + throw error; + } + } super.initNeed(); } diff --git a/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/config/MybatisPlusConfig.java b/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/config/MybatisPlusConfig.java index a0c2f95..2d9c743 100644 --- a/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/config/MybatisPlusConfig.java +++ b/mybatis-plus-join-test/test-base/src/main/java/com/github/yulichang/test/config/MybatisPlusConfig.java @@ -8,11 +8,15 @@ import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.extension.injector.methods.InsertBatchSomeColumn; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; +import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler; import com.baomidou.mybatisplus.extension.plugins.inner.InnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; +import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor; import com.github.yulichang.injector.MPJSqlInjector; import com.github.yulichang.test.util.ThreadLocalUtils; import lombok.SneakyThrows; +import net.sf.jsqlparser.expression.Expression; +import net.sf.jsqlparser.expression.LongValue; import org.apache.ibatis.builder.SqlSourceBuilder; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.statement.StatementHandler; @@ -44,6 +48,17 @@ public class MybatisPlusConfig { MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor(); PaginationInnerInterceptor page = new PaginationInnerInterceptor(DbType.H2); page.setOptimizeJoin(false); + interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() { + @Override + public Expression getTenantId() { + return new LongValue(1); + } + + @Override + public boolean ignoreTable(String tableName) { + return !"user_tenant".equals(tableName); + } + })); interceptor.addInnerInterceptor(page); interceptor.addInnerInterceptor(new SqlInterceptor()); return interceptor; diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserTenantDO.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserTenantDO.java new file mode 100644 index 0000000..91efc27 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserTenantDO.java @@ -0,0 +1,23 @@ +package com.github.yulichang.test.join.entity; + + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import lombok.Data; +import lombok.ToString; +import lombok.experimental.FieldNameConstants; + +@Data +@ToString +@FieldNameConstants +@TableName(value = "user_tenant") +public class UserTenantDO { + + @TableId + private Integer id; + + + private Integer userId; + + private Integer tenantId; +} diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserTenantMapper.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserTenantMapper.java new file mode 100644 index 0000000..dddf8d8 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserTenantMapper.java @@ -0,0 +1,10 @@ +package com.github.yulichang.test.join.mapper; + +import com.github.yulichang.test.join.entity.UserTenantDO; +import org.apache.ibatis.annotations.Mapper; + +@Mapper +public interface UserTenantMapper extends MyBaseMapper { + + +} diff --git a/mybatis-plus-join-test/test-join/src/main/resources/db/data.sql b/mybatis-plus-join-test/test-join/src/main/resources/db/data.sql index 55ec13a..e9e4427 100644 --- a/mybatis-plus-join-test/test-join/src/main/resources/db/data.sql +++ b/mybatis-plus-join-test/test-join/src/main/resources/db/data.sql @@ -99,4 +99,15 @@ INSERT INTO order_t (id, user_id, age, name) VALUES (5, 5, 4, '5,4'), (6, 6, 3, '6,3'), (7, 7, 2, '7,2'), -(8, 8, 1, '8,1'); \ No newline at end of file +(8, 8, 1, '8,1'); + +DELETE FROM user_tenant; +INSERT INTO user_tenant (id,user_id, tenant_id) VALUES +(1, 1, 1), +(2, 2, 1), +(3, 3, 1), +(4, 4, 1), +(5, 5, 1), +(6, 6, 2), +(7, 7, 2), +(8, 8, 2); \ No newline at end of file diff --git a/mybatis-plus-join-test/test-join/src/main/resources/db/schema.sql b/mybatis-plus-join-test/test-join/src/main/resources/db/schema.sql index 9e9c7ef..64c7602 100644 --- a/mybatis-plus-join-test/test-join/src/main/resources/db/schema.sql +++ b/mybatis-plus-join-test/test-join/src/main/resources/db/schema.sql @@ -63,4 +63,13 @@ create table order_t user_id int null, age int null, name varchar(255) null +); + +DROP TABLE IF EXISTS user_tenant; +create table user_tenant +( + id int auto_increment + primary key, + user_id int null, + tenant_id int null ); \ No newline at end of file diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java index 0f78344..90b31b4 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 @@ -680,7 +680,9 @@ class LambdaWrapperTest { .eq(UserDO::getName, "ref"); userMapper.selectList(wrapper); try { - userMapper.insertBatchSomeColumn(new ArrayList<>()); + userMapper.insertBatchSomeColumn(new ArrayList() {{ + add(new UserDO()); + }}); } catch (BadSqlGrammarException ignored) { } } @@ -774,7 +776,7 @@ class LambdaWrapperTest { */ @Test void testTable() { - ThreadLocalUtils.set("SELECT t.id FROM `user`bbbbbbb t LEFT JOIN addressaaaaaaaaaa t1 ON (t1.user_id = t.id) LEFT JOIN area t2 ON (t2.id = t1.area_id) WHERE t.del=false AND t1.del=false AND t2.del=false AND (t.id <= ?) ORDER BY t.id DESC"); + ThreadLocalUtils.set("SELECT t.id FROM bbbbbbb t LEFT JOIN addressaaaaaaaaaa t1 ON (t1.user_id = t.id) LEFT JOIN area t2 ON (t2.id = t1.area_id) WHERE t.del=false AND t1.del=false AND t2.del=false AND (t.id <= ?) ORDER BY t.id DESC"); MPJLambdaWrapper wrapper = new MPJLambdaWrapper() .select(UserDO::getId) .leftJoin(AddressDO.class, on -> on @@ -783,10 +785,11 @@ class LambdaWrapperTest { .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId) .le(UserDO::getId, 10000) .orderByDesc(UserDO::getId) - .setTableName(name -> name + "bbbbbbb"); + .setTableName(name -> "bbbbbbb"); try { List list = userMapper.selectJoinList(UserDTO.class, wrapper); } catch (BadSqlGrammarException ignored) { + } } @@ -1050,7 +1053,7 @@ class LambdaWrapperTest { */ @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 <= ?)"); + 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) @@ -1059,7 +1062,7 @@ class LambdaWrapperTest { .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 <= ?)"); + 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) diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/QueryWrapperTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/QueryWrapperTest.java index f29b628..43ec2cc 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/QueryWrapperTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/QueryWrapperTest.java @@ -5,8 +5,11 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.query.MPJQueryWrapper; import com.github.yulichang.test.join.dto.UserDTO; import com.github.yulichang.test.join.entity.UserDO; +import com.github.yulichang.test.join.entity.UserTenantDO; import com.github.yulichang.test.join.mapper.UserMapper; +import com.github.yulichang.test.join.mapper.UserTenantMapper; import com.github.yulichang.test.util.ThreadLocalUtils; +import com.github.yulichang.toolkit.JoinWrappers; import org.junit.jupiter.api.Test; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.BadSqlGrammarException; @@ -20,6 +23,10 @@ class QueryWrapperTest { @Resource private UserMapper userMapper; + @Resource + private UserTenantMapper userTenantMapper; + + /** * 链表查询 */ @@ -30,6 +37,16 @@ class QueryWrapperTest { .select("name AS nameName") .last("LIMIT 1")); System.out.println(dto); + + + IPage iPage1 = userTenantMapper.selectJoinPage(new Page<>(1, 10), UserDTO.class, + JoinWrappers.query(UserTenantDO.class) + .selectAll(UserTenantDO.class) + .select("t1.name as PName") + .leftJoin("(select * from `user` where id <> -1) t1 on t1.id = t.user_id") + .apply("t.id <> -1")); + + iPage1.getRecords().forEach(System.out::println); } /** @@ -37,11 +54,11 @@ class QueryWrapperTest { */ @Test void table() { - 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,name AS nameName FROM `user`fwear t WHERE t.del=false LIMIT 1", - "SELECT t.id,t.pid,t.`name`,t.`json`,t.sex,t.head_img AS img,t.create_time,t.address_id,t.address_id2,t.del,t.create_by,t.update_by,name AS nameName FROM `user`fwear t WHERE t.del=false LIMIT 1"); + 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,name AS nameName FROM fwear t WHERE t.del=false LIMIT 1", + "SELECT t.id,t.pid,t.`name`,t.`json`,t.sex,t.head_img AS img,t.create_time,t.address_id,t.address_id2,t.del,t.create_by,t.update_by,name AS nameName FROM fwear t WHERE t.del=false LIMIT 1"); MPJQueryWrapper wrapper = new MPJQueryWrapper() .selectAll(UserDO.class) - .setTableName(name -> name + "fwear") + .setTableName(name -> "fwear") .select("name AS nameName") .last("LIMIT 1"); try {