diff --git a/README.md b/README.md index d340e2b..fc5c741 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,8 @@ * [演示工程](https://gitee.com/best_handsome/mybatis-plus-join-demo) * 点个Star支持一下吧 :) -QQ群:1022221898 +QQ群:1022221898 +[加入微信群](https://gitee.com/best_handsome/mybatis-plus-join/issues/I65N2M) ## 使用方法 @@ -15,12 +16,12 @@ QQ群:1022221898 com.github.yulichang mybatis-plus-join-boot-starter - 1.3.8 + 1.3.11 ``` - Gradle ``` - implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.3.8' + implementation 'com.github.yulichang:mybatis-plus-join-boot-starter:1.3.11' ``` 或者clone代码到本地执行 mvn install, 再引入以上依赖
diff --git a/mybatis-plus-join-annotation/pom.xml b/mybatis-plus-join-annotation/pom.xml index cc58eeb..a42c1c1 100644 --- a/mybatis-plus-join-annotation/pom.xml +++ b/mybatis-plus-join-annotation/pom.xml @@ -7,9 +7,9 @@ com.github.yulichang mybatis-plus-join-root - 1.3.8 + 1.3.11 - 1.3.8 + 1.3.11 mybatis-plus-join-annotation mybatis-plus-join-annotation @@ -51,4 +51,4 @@ provided - \ No newline at end of file + diff --git a/mybatis-plus-join-boot-starter/pom.xml b/mybatis-plus-join-boot-starter/pom.xml index 1c37bc0..2ab8557 100644 --- a/mybatis-plus-join-boot-starter/pom.xml +++ b/mybatis-plus-join-boot-starter/pom.xml @@ -7,9 +7,9 @@ com.github.yulichang mybatis-plus-join-root - 1.3.8 + 1.3.11 - 1.3.8 + 1.3.11 mybatis-plus-join-boot-starter mybatis-plus-join-boot-starter @@ -47,7 +47,7 @@ com.github.yulichang mybatis-plus-join-core - 1.3.8 + 1.3.11 org.springframework.boot @@ -68,4 +68,4 @@ provided - \ No newline at end of file + diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java index ab495cb..b2c79a3 100644 --- a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java @@ -57,6 +57,8 @@ public class MybatisPlusJoinAutoConfiguration { public MybatisPlusJoinAutoConfiguration(MybatisPlusJoinProperties properties) { this.properties = properties; ConfigProperties.subTableLogic = properties.getSubTableLogic(); + ConfigProperties.msCache = properties.isMsCache(); + ConfigProperties.tableAlias = properties.getTableAlias(); } /** diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java index 91dfdf5..f3d842b 100644 --- a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java @@ -20,8 +20,18 @@ public class MybatisPlusJoinProperties { */ private Boolean banner = true; + /** + * 表别名 + */ + private String tableAlias = "t"; + /** * 连表查询副表是否启用逻辑删除(前提是MP配置了逻辑删除) */ private Boolean subTableLogic = true; + + /** + * MappedStatement缓存 + */ + private boolean msCache = true; } diff --git a/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index de6b922..04c9c5b 100644 --- a/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -18,6 +18,18 @@ "defaultValue": true, "type": "java.lang.Boolean", "description": "连表查询副表是否启用逻辑删除(前提是MP配置了逻辑删除)." + }, + { + "name": "mybatis-plus-join.ms-cache", + "defaultValue": true, + "type": "java.lang.Boolean", + "description": "MappedStatement缓存开关." + }, + { + "name": "mybatis-plus-join.table-alias", + "defaultValue": "t", + "type": "java.lang.String", + "description": "表别名." } ] } \ No newline at end of file diff --git a/mybatis-plus-join-core/pom.xml b/mybatis-plus-join-core/pom.xml index 288cd4f..e373655 100644 --- a/mybatis-plus-join-core/pom.xml +++ b/mybatis-plus-join-core/pom.xml @@ -7,9 +7,9 @@ com.github.yulichang mybatis-plus-join-root - 1.3.8 + 1.3.11 - 1.3.8 + 1.3.11 mybatis-plus-join-core mybatis-plus-join-core @@ -47,7 +47,7 @@ com.github.yulichang mybatis-plus-join-annotation - 1.3.8 + 1.3.11 com.baomidou diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java index e96c9aa..eda7d8c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java @@ -6,4 +6,7 @@ package com.github.yulichang.config; */ public class ConfigProperties { public static boolean subTableLogic = true; + public static boolean msCache = true; + + public static String tableAlias = "t"; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java index 29cfc13..a893038 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java @@ -7,6 +7,7 @@ import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.plugin.Interceptor; import org.apache.ibatis.plugin.InterceptorChain; +import org.apache.ibatis.session.Configuration; import org.apache.ibatis.session.SqlSessionFactory; import java.lang.reflect.Field; @@ -28,7 +29,7 @@ public class InterceptorConfig { //打印banner System.out.println(" _ _ |_ _ _|_. ___ _ | _ . _ . _ \n" + "| | |\\/|_)(_| | |_\\ |_)||_|_\\ | (_) | | | \n" + - " / | / 1.3.10"); + " / | / 1.3.11"); } } @@ -39,7 +40,7 @@ public class InterceptorConfig { } for (SqlSessionFactory factory : sqlSessionFactoryList) { try { - Field interceptorChain = org.apache.ibatis.session.Configuration.class.getDeclaredField("interceptorChain"); + Field interceptorChain = Configuration.class.getDeclaredField("interceptorChain"); interceptorChain.setAccessible(true); InterceptorChain chain = (InterceptorChain) interceptorChain.get(factory.getConfiguration()); Field interceptors = InterceptorChain.class.getDeclaredField("interceptors"); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java index 9056174..aedff5c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java @@ -32,6 +32,7 @@ public class MPJSqlInjector extends DefaultSqlInjector { /** * 升级到 mybatis plus 3.4.3.2 后对之前的版本兼容 */ + @Deprecated @SuppressWarnings("unused") public List getMethodList(Class mapperClass) { List list = Stream.of( diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index 386273b..c2c57d5 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -3,7 +3,7 @@ package com.github.yulichang.interceptor; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.*; -import com.github.yulichang.interfaces.MPJBaseJoin; +import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.mapper.MPJTableMapperHelper; import com.github.yulichang.method.MPJResultType; import com.github.yulichang.query.MPJQueryWrapper; @@ -15,8 +15,6 @@ import com.github.yulichang.wrapper.resultmap.Result; import com.github.yulichang.wrapper.segments.Select; import com.github.yulichang.wrapper.segments.SelectLabel; import org.apache.ibatis.executor.Executor; -import org.apache.ibatis.logging.Log; -import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.ResultFlag; import org.apache.ibatis.mapping.ResultMap; @@ -46,8 +44,6 @@ import java.util.concurrent.ConcurrentHashMap; public class MPJInterceptor implements Interceptor { - private static final Log logger = LogFactory.getLog(MPJInterceptor.class); - private static final List EMPTY_RESULT_MAPPING = new ArrayList<>(0); /** @@ -65,11 +61,6 @@ public class MPJInterceptor implements Interceptor { if (args[1] instanceof Map) { Map map = (Map) args[1]; Object ew = map.containsKey(Constants.WRAPPER) ? map.get(Constants.WRAPPER) : null; - if (!map.containsKey(Constant.PARAM_TYPE)) { - map.put(Constant.PARAM_TYPE, Objects.nonNull(ew) && (ew instanceof MPJBaseJoin)); - } else { - logger.warn(String.format("请不要使用MPJ预留参数名 %s", Constant.PARAM_TYPE)); - } if (CollectionUtils.isNotEmpty(map) && map.containsKey(Constant.CLAZZ)) { Class clazz = (Class) map.get(Constant.CLAZZ); if (Objects.nonNull(clazz)) { @@ -102,7 +93,9 @@ public class MPJInterceptor implements Interceptor { } if (ew instanceof MPJQueryWrapper) { MPJQueryWrapper wrapper = (MPJQueryWrapper) ew; - return getCache(ms, id + StringPool.UNDERSCORE + wrapper.getSqlSelect(), resultType, ew); + if (ConfigProperties.msCache) { + return getCache(ms, id + StringPool.UNDERSCORE + wrapper.getSqlSelect(), resultType, ew); + } } return buildMappedStatement(ms, resultType, ew, id); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableMapperHelper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableMapperHelper.java index 82a0e63..f87a2f6 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableMapperHelper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableMapperHelper.java @@ -16,10 +16,8 @@ public class MPJTableMapperHelper { public static void init(Class clazz, Class mapper) { - if (clazz != null) { + if (clazz != null && mapper != null) { CACHE.put(clazz, mapper); - } - if (mapper != null) { CACHE_REVERSE.put(mapper, clazz); } } 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 9d3ed84..ae810e2 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 @@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.injector.AbstractMethod; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; -import com.github.yulichang.toolkit.Constant; +import com.github.yulichang.config.ConfigProperties; import java.util.ArrayList; import java.util.List; @@ -43,7 +43,7 @@ public abstract class MPJAbstractMethod extends AbstractMethod implements MPJBas String[] columns = selectColumns.split(StringPool.COMMA); List selectColumnList = new ArrayList<>(); for (String c : columns) { - selectColumnList.add(Constant.TABLE_ALIAS + StringPool.DOT + c); + selectColumnList.add(ConfigProperties.tableAlias + StringPool.DOT + c); } selectColumns = String.join(StringPool.COMMA, selectColumnList); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java index 2867e58..3ac093c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java @@ -7,7 +7,6 @@ import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; import com.github.yulichang.config.ConfigProperties; -import com.github.yulichang.toolkit.Constant; import java.util.Objects; @@ -69,7 +68,7 @@ public interface MPJBaseMethod extends Constants { return filedSqlScript; } String newKeyProperty = newPrefix + tableInfo.getKeyProperty(); - String keySqlScript = Constant.TABLE_ALIAS + DOT + tableInfo.getKeyColumn() + EQUALS + SqlScriptUtils.safeParam(newKeyProperty); + String keySqlScript = ConfigProperties.tableAlias + DOT + tableInfo.getKeyColumn() + EQUALS + SqlScriptUtils.safeParam(newKeyProperty); return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty), false) + NEWLINE + filedSqlScript; } @@ -77,7 +76,7 @@ public interface MPJBaseMethod extends Constants { default String getSqlWhere(TableFieldInfo tableFieldInfo, final String prefix) { final String newPrefix = prefix == null ? EMPTY : prefix; // 默认: AND column=#{prefix + el} - String sqlScript = " AND " + String.format(tableFieldInfo.getCondition(), Constant.TABLE_ALIAS + DOT + tableFieldInfo.getColumn(), newPrefix + tableFieldInfo.getEl()); + String sqlScript = " AND " + String.format(tableFieldInfo.getCondition(), ConfigProperties.tableAlias + DOT + tableFieldInfo.getColumn(), newPrefix + tableFieldInfo.getEl()); // 查询的时候只判非空 return convertIf(tableFieldInfo, sqlScript, convertIfProperty(newPrefix, tableFieldInfo.getProperty()), tableFieldInfo.getWhereStrategy()); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectCount.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectCount.java index c422f98..e33eb38 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectCount.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/SelectCount.java @@ -2,8 +2,9 @@ package com.github.yulichang.method.mp; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; -import com.github.yulichang.toolkit.Constant; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; /** @@ -28,7 +29,7 @@ public class SelectCount extends com.baomidou.mybatisplus.core.injector.methods. @Override protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table)); } } \ No newline at end of file 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 a05606a..1602af6 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 @@ -2,9 +2,10 @@ package com.github.yulichang.method.mp; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; 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.toolkit.Constant; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; /** @@ -29,14 +30,14 @@ public class SelectList extends com.baomidou.mybatisplus.core.injector.methods.S @Override protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table)); } @Override protected String sqlSelectColumns(TableInfo table, boolean queryWrapper) { String selectColumns = super.sqlSelectColumns(table, queryWrapper); - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } } 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 5f3a964..da91597 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 @@ -2,9 +2,10 @@ package com.github.yulichang.method.mp; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; 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.toolkit.Constant; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; /** @@ -29,14 +30,14 @@ public class SelectMaps extends com.baomidou.mybatisplus.core.injector.methods.S @Override protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table)); } @Override protected String sqlSelectColumns(TableInfo table, boolean queryWrapper) { String selectColumns = super.sqlSelectColumns(table, queryWrapper); - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } } 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 402cc5e..b6e592a 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 @@ -2,9 +2,10 @@ package com.github.yulichang.method.mp; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; 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.toolkit.Constant; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; /** @@ -29,14 +30,14 @@ public class SelectMapsPage extends com.baomidou.mybatisplus.core.injector.metho @Override protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table)); } @Override protected String sqlSelectColumns(TableInfo table, boolean queryWrapper) { String selectColumns = super.sqlSelectColumns(table, queryWrapper); - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } } 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 ce07be6..81d5a35 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 @@ -2,9 +2,10 @@ package com.github.yulichang.method.mp; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; 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.toolkit.Constant; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; /** @@ -29,14 +30,14 @@ public class SelectObjs extends com.baomidou.mybatisplus.core.injector.methods.S @Override protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table)); } @Override protected String sqlSelectColumns(TableInfo table, boolean queryWrapper) { String selectColumns = super.sqlSelectColumns(table, queryWrapper); - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } } 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 93cd0a6..f6023c9 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 @@ -2,9 +2,10 @@ package com.github.yulichang.method.mp; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; 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.toolkit.Constant; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; /** @@ -34,14 +35,14 @@ public class SelectOne extends com.baomidou.mybatisplus.core.injector.methods.Se @Override protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table)); } @Override protected String sqlSelectColumns(TableInfo table, boolean queryWrapper) { String selectColumns = super.sqlSelectColumns(table, queryWrapper); - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } } 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 5816266..e252206 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 @@ -2,9 +2,10 @@ package com.github.yulichang.method.mp; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; 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.toolkit.Constant; +import com.github.yulichang.interfaces.MPJBaseJoin; import org.apache.ibatis.mapping.MappedStatement; /** @@ -29,14 +30,14 @@ public class SelectPage extends com.baomidou.mybatisplus.core.injector.methods.S @Override protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), super.sqlWhereEntityWrapper(newLine, table), mpjSqlWhereEntityWrapper(newLine, table)); } @Override protected String sqlSelectColumns(TableInfo table, boolean queryWrapper) { String selectColumns = super.sqlSelectColumns(table, queryWrapper); - return SqlScriptUtils.convertChoose(String.format("%s == null or !%s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), + return SqlScriptUtils.convertChoose(String.format("%s == null or !(%s instanceof %s)", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), selectColumns, mpjSqlSelectColumns() + StringPool.SPACE + selectColumns); } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/TableAlias.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/TableAlias.java index 1b2fd6b..8498282 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/TableAlias.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/mp/TableAlias.java @@ -3,8 +3,8 @@ 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.sql.SqlScriptUtils; +import com.github.yulichang.interfaces.MPJBaseJoin; import com.github.yulichang.method.MPJBaseMethod; -import com.github.yulichang.toolkit.Constant; /** * 兼容原生方法 @@ -15,10 +15,10 @@ import com.github.yulichang.toolkit.Constant; public interface TableAlias extends Constants, MPJBaseMethod { default String getTableName(TableInfo tableInfo) { - return tableInfo.getTableName() + SPACE + SqlScriptUtils.convertIf("${ew.alias}", - String.format("%s != null and %s", Constant.PARAM_TYPE, Constant.PARAM_TYPE), false) - + SPACE + SqlScriptUtils.convertIf("${ew.from}", - String.format("%s != null and %s and %s != null and %s != ''", Constant.PARAM_TYPE, Constant.PARAM_TYPE, - "ew.from", "ew.from"), false); + String from = SqlScriptUtils.convertIf("${ew.from}", + String.format("%s != null and %s != ''", "ew.from", "ew.from"), true); + String alias = SqlScriptUtils.convertIf("${ew.alias}" + NEWLINE + from, + String.format("%s != null and %s instanceof %s", Constants.WRAPPER, Constants.WRAPPER, MPJBaseJoin.class.getName()), true); + return tableInfo.getTableName() + SPACE + alias; } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java index 38baa6e..9214664 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java @@ -9,8 +9,8 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import com.github.yulichang.query.interfaces.MPJJoin; -import com.github.yulichang.toolkit.Constant; +import com.github.yulichang.config.ConfigProperties; +import com.github.yulichang.query.interfaces.StringJoin; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -31,7 +31,7 @@ import java.util.stream.Collectors; @Deprecated @SuppressWarnings("unused") public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper> - implements Query, T, SFunction>, MPJJoin, T> { + implements Query, T, SFunction>, StringJoin, T> { /** * 查询字段 @@ -46,7 +46,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper extends AbstractLambdaWrapper selectIgnore(SFunction... columns) { if (ArrayUtils.isNotEmpty(columns)) { for (SFunction s : columns) { - ignoreColumns.add(Constant.TABLE_ALIAS + StringPool.DOT + columnToString(s)); + ignoreColumns.add(alias + StringPool.DOT + columnToString(s)); } } return typedThis; @@ -134,7 +134,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper column, boolean onlyColumn) { - return Constant.TABLE_ALIAS + StringPool.DOT + super.columnToString(column, onlyColumn); + return alias + StringPool.DOT + super.columnToString(column, onlyColumn); } public MPJLambdaQueryWrapper select(String... columns) { @@ -162,7 +162,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper", entityClass.getSimpleName()); selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c -> - Constant.TABLE_ALIAS + StringPool.DOT + c.getColumn()).collect(Collectors.toList())); + alias + StringPool.DOT + c.getColumn()).collect(Collectors.toList())); return typedThis; } @@ -173,7 +173,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper selectAll(Class clazz) { - return selectAll(clazz, Constant.TABLE_ALIAS); + return selectAll(clazz, alias); } /** @@ -219,7 +219,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper extends AbstractWrapper> - implements Query, T, String>, MPJJoin, T> { + implements Query, T, String>, StringJoin, T> { /** * 查询字段 @@ -44,7 +44,7 @@ public class MPJQueryWrapper extends AbstractWrapper extends AbstractWrapper join(String keyWord, boolean condition, String joinSql) { if (condition) { - from.setStringValue(from.getStringValue() + keyWord + joinSql); + from.setStringValue(from.getStringValue() + StringPool.SPACE + keyWord + StringPool.SPACE + joinSql); } return typedThis; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/MPJJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/StringJoin.java similarity index 95% rename from mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/MPJJoin.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/StringJoin.java index 35f11c4..378db91 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/MPJJoin.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/StringJoin.java @@ -7,7 +7,7 @@ import com.github.yulichang.toolkit.Constant; * @author yulichang */ @SuppressWarnings("unused") -public interface MPJJoin extends MPJBaseJoin { +public interface StringJoin extends MPJBaseJoin { /** * left join diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java index c71a8d3..0488cc0 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java @@ -6,10 +6,6 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool; * @author yulichang */ public interface Constant { - /** - * 表别名 - */ - String TABLE_ALIAS = "t"; String AS = " AS "; @@ -27,30 +23,23 @@ public interface Constant { String CLAZZ = "resultTypeClass_Eg1sG"; - String PARAM_TYPE = "paramType_Gr8re1Ee"; + /** + * "LEFT JOIN" + */ + String LEFT_JOIN = LEFT + StringPool.SPACE + JOIN; /** - * " LEFT JOIN " + * "RIGHT JOIN" */ - String LEFT_JOIN = StringPool.SPACE + LEFT + StringPool.SPACE + JOIN + StringPool.SPACE; + String RIGHT_JOIN = RIGHT + StringPool.SPACE + JOIN; /** - * " RIGHT JOIN " + * "INNER JOIN" */ - String RIGHT_JOIN = StringPool.SPACE + RIGHT + StringPool.SPACE + JOIN + StringPool.SPACE; + String INNER_JOIN = INNER + StringPool.SPACE + JOIN; /** - * " INNER JOIN " + * "FULL JOIN" */ - String INNER_JOIN = StringPool.SPACE + INNER + StringPool.SPACE + JOIN + StringPool.SPACE; - - /** - * " FULL JOIN " - */ - String FULL_JOIN = StringPool.SPACE + FULL + StringPool.SPACE + JOIN + StringPool.SPACE; - - /** - * " t" - */ - String SPACE_TABLE_ALIAS = StringPool.SPACE + Constant.TABLE_ALIAS; + String FULL_JOIN = FULL + StringPool.SPACE + JOIN; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java index abf0838..644c22a 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java @@ -21,25 +21,26 @@ public class LogicInfoUtils implements Constants { private static final Map, Map> LOGIC_CACHE = new ConcurrentHashMap<>(); - public static String getLogicInfo(String tableIndex, Class clazz) { + public static String getLogicInfo(String tableIndex, Class clazz, boolean hasAlias, String alias) { Map absent = LOGIC_CACHE.get(clazz); if (absent == null) { absent = new ConcurrentHashMap<>(); LOGIC_CACHE.put(clazz, absent); } - return absent.computeIfAbsent(tableIndex, key -> getLogicStr(key, clazz)); + return absent.computeIfAbsent(hasAlias ? alias : (alias + tableIndex), key -> getLogicStr(key, clazz)); } - private static String getLogicStr(String tableIndex, Class clazz) { + private static String getLogicStr(String prefix, Class clazz) { + String logicStr; TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz); - Assert.notNull(tableInfo, "%s 不是数据库实体或没有注册到mybatis plus中", clazz.getName()); + Assert.notNull(tableInfo, "table not find by class <%s>", clazz.getSimpleName()); if (tableInfo.isWithLogicDelete() && Objects.nonNull(tableInfo.getLogicDeleteFieldInfo())) { final String value = tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue(); if (NULL.equalsIgnoreCase(value)) { - logicStr = " AND " + Constant.TABLE_ALIAS + tableIndex + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL"; + logicStr = " AND " + prefix + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL"; } else { - logicStr = " AND " + Constant.TABLE_ALIAS + tableIndex + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value); + logicStr = " AND " + prefix + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value); } } else { logicStr = StringPool.EMPTY; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java index 2b10bbb..9d6c125 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java @@ -1,8 +1,10 @@ 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.StringPool; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import com.github.yulichang.toolkit.Constant; +import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.wrapper.segments.Select; @@ -10,8 +12,10 @@ import com.github.yulichang.wrapper.segments.SelectCache; import lombok.AllArgsConstructor; import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.Getter; import java.util.*; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; import static java.util.stream.Collectors.joining; @@ -25,26 +29,39 @@ import static java.util.stream.Collectors.joining; public abstract class MPJAbstractLambdaWrapper> extends MPJAbstractWrapper { + /** + * 主表别名 + */ + protected String alias = ConfigProperties.tableAlias; + /** + * 是否构建是否存在一对多 + */ + @Getter + protected boolean resultMap = false; + /** + * 表序号 + */ + protected int tableIndex = 1; /** * 关联的表 */ - protected TableList tableList = new TableList(); + protected TableList tableList; @Override - protected String columnToString(String index, X column, boolean isJoin) { - return columnToString(index, (SFunction) column, isJoin); + protected String columnToString(String index, int node, X column, boolean isJoin, Class parent) { + return columnToString(index, node, (SFunction) column, isJoin, parent); } @Override @SafeVarargs - protected final String columnsToString(String index, boolean isJoin, X... columns) { - return Arrays.stream(columns).map(i -> columnToString(index, (SFunction) i, isJoin)).collect(joining(StringPool.COMMA)); + protected final String columnsToString(String index, int node, boolean isJoin, Class parent, X... columns) { + return Arrays.stream(columns).map(i -> columnToString(index, node, (SFunction) i, isJoin, parent)).collect(joining(StringPool.COMMA)); } - protected String columnToString(String index, SFunction column, boolean isJoin) { + protected String columnToString(String index, int node, SFunction column, boolean isJoin, Class parent) { Class entityClass = LambdaUtils.getEntityClass(column); - return Constant.TABLE_ALIAS + getDefault(index, entityClass, isJoin) + StringPool.DOT + + return getDefault(index, node, entityClass, isJoin, parent) + StringPool.DOT + getCache(column).getTagColumn(); } @@ -54,28 +71,41 @@ public abstract class MPJAbstractLambdaWrapper clazz, boolean isJoin) { + /** + * 返回前缀 + */ + protected String getDefault(String index, int node, Class clazz, boolean isJoin, Class parent) { + //外层where条件 if (Objects.isNull(index)) { - if (Objects.equals(clazz, getEntityClass())) { - return StringPool.EMPTY; + if (!isJoin && Objects.equals(clazz, getEntityClass())) { + return this.alias; } //正序 Table table = tableList.getPositive(clazz); - return Objects.isNull(table.index) ? StringPool.EMPTY : table.index; + return table.hasAlias ? table.alias : (table.alias + (Objects.isNull(table.index) ? StringPool.EMPTY : table.index)); } Table table = tableList.get(clazz, index); - if (Objects.nonNull(table.getIndex())) { - if (isJoin) { - //除自己以外的倒序第一个 - Table t = tableList.getOrElse(clazz, index); - if (Objects.isNull(t.getIndex())) { - return StringPool.EMPTY; - } - return t.getIndex(); - } - return table.getIndex(); + if (table.hasAlias) { + return table.alias; } - return StringPool.EMPTY; + if (Objects.nonNull(table.getIndex())) { + if (isJoin && (Objects.equals(clazz, getEntityClass()) || Objects.equals(parent, clazz))) { + if (node == -1) { + return table.alias; + } else if (node == 0) { + //除自己以外的倒序第一个 + Table t = tableList.getOrElse(clazz, index); + if (Objects.isNull(t.getIndex())) { + return t.alias; + } + return t.alias + t.getIndex(); + } else { + return table.alias + node; + } + } + return table.alias + table.getIndex(); + } + return table.alias; } protected String getDefaultSelect(String index, Class clazz, Select s) { @@ -105,12 +135,16 @@ public abstract class MPJAbstractLambdaWrapper clazz, String index, String alias) { + DEFAULT_TABLE = new Table(clazz, index, false, alias); + } private final List list = new ArrayList<>(); - public void add(Class clazz, String index) { - this.list.add(new Table(clazz, index)); + public void add(Class clazz, String index, boolean hasAlias, String alias) { + this.list.add(new Table(clazz, index, hasAlias, alias)); } public Table get(Class clazz) { @@ -211,5 +245,24 @@ public abstract class MPJAbstractLambdaWrapper clazz; private final String index; + + private boolean hasAlias; + + private final String alias; + } + + + /** + * 必要的初始化 + */ + protected void initNeed() { + paramNameSeq = new AtomicInteger(0); + paramNameValuePairs = new HashMap<>(16); + expression = new MergeSegments(); + lastSql = SharedString.emptyString(); + sqlComment = SharedString.emptyString(); + sqlFirst = SharedString.emptyString(); + node = ROOT_NODE; + tableList = new TableList(getEntityClass(), null, alias); } } 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 202b08e..3c06ccd 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 @@ -12,10 +12,8 @@ import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils; import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.sql.SqlScriptUtils; -import com.github.yulichang.wrapper.interfaces.Compare; -import com.github.yulichang.wrapper.interfaces.Func; -import com.github.yulichang.wrapper.interfaces.Join; -import com.github.yulichang.wrapper.interfaces.on.OnCompare; +import com.github.yulichang.wrapper.interfaces.*; +import lombok.AllArgsConstructor; import lombok.Getter; import java.util.*; @@ -35,7 +33,10 @@ import static java.util.stream.Collectors.joining; */ @SuppressWarnings("ALL") public abstract class MPJAbstractWrapper> extends Wrapper - implements Compare, Nested, Join, Func, OnCompare { + implements Compare, Nested, Join, Func, OnCompare, + CompareStr, FuncStr { + + protected static final Node ROOT_NODE = new Node(null, 0, null); /** * 占位符 @@ -85,6 +86,11 @@ public abstract class MPJAbstractWrapper joinClass; + /** + * 寻路 + */ + protected Node node; + @Override public T getEntity() { return entity; @@ -314,11 +320,36 @@ public abstract class MPJAbstractWrapper String.format("(%s)", inValue))); } + + @Override + public Children gtSql(boolean condition, SFunction column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(index, column, false), GT, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children geSql(boolean condition, SFunction column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(index, column, false), GE, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children ltSql(boolean condition, SFunction column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(index, column, false), LT, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children leSql(boolean condition, SFunction column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(index, column, false), LE, + () -> String.format("(%s)", inValue))); + } + @Override public Children groupBy(boolean condition, List> columns) { return maybeDo(condition, () -> { if (CollectionUtils.isNotEmpty(columns)) { - String one = (StringPool.COMMA + columnsToString(index, false, columns)); + String one = (StringPool.COMMA + columnsToString(index, getByClass(node, joinClass), false, parentClass(node), columns)); final String finalOne = one; appendSqlSegments(GROUP_BY, () -> finalOne); } @@ -328,9 +359,9 @@ public abstract class MPJAbstractWrapper Children groupBy(boolean condition, SFunction column, SFunction... columns) { return maybeDo(condition, () -> { - String one = columnToString(index, column, false); + String one = columnToString(index, getByClass(node, joinClass), column, false, parentClass(node)); if (ArrayUtils.isNotEmpty(columns)) { - one += (StringPool.COMMA + columnsToString(index, false, columns)); + one += (StringPool.COMMA + columnsToString(index, getByClass(node, joinClass), false, parentClass(node), columns)); } final String finalOne = one; appendSqlSegments(GROUP_BY, () -> finalOne); @@ -417,6 +448,11 @@ public abstract class MPJAbstractWrapper formatParam(null, SqlUtils.concatLike(val, sqlLike)))); } + protected Children likeValue(boolean condition, SqlKeyword keyword, String column, Object val, SqlLike sqlLike) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), keyword, + () -> formatParam(null, SqlUtils.concatLike(val, sqlLike)))); + } + /** * 普通查询条件 * @@ -435,6 +471,11 @@ public abstract class MPJAbstractWrapper appendSqlSegments(columnToSqlSegment(column), sqlKeyword, + () -> formatParam(null, val))); + } + /** * 多重嵌套查询条件 * @@ -531,18 +572,6 @@ public abstract class MPJAbstractWrapper(16); - expression = new MergeSegments(); - lastSql = SharedString.emptyString(); - sqlComment = SharedString.emptyString(); - sqlFirst = SharedString.emptyString(); - } - @Override public void clear() { entity = null; @@ -615,23 +644,40 @@ public abstract class MPJAbstractWrapper ISqlSegment columnToSqlSegment(String index, SFunction column, boolean isJoin) { - return () -> columnToString(index, column, isJoin); + return () -> columnToString(index, getByClass(node, joinClass), column, isJoin, parentClass(node)); + } + + protected final ISqlSegment columnToSqlSegment(String column) { + return () -> (String) column; } /** * 获取 columnName */ - protected String columnToString(String index, X column, boolean isJoin) { + protected String columnToString(String index, int node, X column, boolean isJoin, Class parent) { return (String) column; } + protected String columnToString(String column) { + return column; + } + + protected String columnsToString(List columns) { + return columns.stream().map(this::columnToString).collect(joining(StringPool.COMMA)); + } + + protected String columnsToString(String... columns) { + return Arrays.stream(columns).map(this::columnToString).collect(joining(StringPool.COMMA)); + } + + /** * 多字段转换为逗号 "," 分割字符串 * * @param columns 多字段 */ - protected String columnsToString(String index, boolean isJoin, X... columns) { - return Arrays.stream(columns).map(i -> this.columnToString(index, i, isJoin)).collect(joining(StringPool.COMMA)); + protected String columnsToString(String index, int node, boolean isJoin, Class parent, X... columns) { + return Arrays.stream(columns).map(i -> this.columnToString(index, node, i, isJoin, parent)).collect(joining(StringPool.COMMA)); } @Override @@ -680,4 +726,265 @@ public abstract class MPJAbstractWrapper Children le(boolean condition, SFunction column, SFunction val) { return addCondition(condition, column, LE, val); } + + @Getter + @AllArgsConstructor + public static class Node { + + private Class clazz; + + private int index; + + private Node parent; + } + + + private int getByClass(Node node, Class joinClass) { + if (joinClass == null) { + return 0; + } + if (node.parent != null) { + return dg(node.parent, joinClass); + } + return 0; + } + + private Class parentClass(Node node) { + if (node == null || node.parent == null) { + return null; + } + return node.parent.clazz; + } + + private int dg(Node node, Class joinClass) { + + if (node.clazz != null && node.clazz == joinClass) { + return node.index; + } + if (node.parent == null) { + return joinClass == getEntityClass() ? -1 : 0; + } + return getByClass(node.parent, joinClass); + } + + /* ****************************************** **/ + + + @Override + public Children allEqStr(boolean condition, Map params, boolean null2IsNull) { + if (condition && CollectionUtils.isNotEmpty(params)) { + params.forEach((k, v) -> { + if (StringUtils.checkValNotNull(v)) { + eq(k, v); + } else { + if (null2IsNull) { + isNull(k); + } + } + }); + } + return typedThis; + } + + @Override + public Children allEqStr(boolean condition, BiPredicate filter, Map params, boolean null2IsNull) { + if (condition && CollectionUtils.isNotEmpty(params)) { + params.forEach((k, v) -> { + if (filter.test(k, v)) { + if (StringUtils.checkValNotNull(v)) { + eq(k, v); + } else { + if (null2IsNull) { + isNull(k); + } + } + } + }); + } + return typedThis; + } + + @Override + public Children eq(boolean condition, String column, Object val) { + return addCondition(condition, column, EQ, val); + } + + @Override + public Children ne(boolean condition, String column, Object val) { + return addCondition(condition, column, NE, val); + } + + @Override + public Children gt(boolean condition, String column, Object val) { + return addCondition(condition, column, GT, val); + } + + @Override + public Children ge(boolean condition, String column, Object val) { + return addCondition(condition, column, GE, val); + } + + @Override + public Children lt(boolean condition, String column, Object val) { + return addCondition(condition, column, LT, val); + } + + @Override + public Children le(boolean condition, String column, Object val) { + return addCondition(condition, column, LE, val); + } + + @Override + public Children like(boolean condition, String column, Object val) { + return likeValue(condition, LIKE, column, val, SqlLike.DEFAULT); + } + + @Override + public Children notLike(boolean condition, String column, Object val) { + return likeValue(condition, NOT_LIKE, column, val, SqlLike.DEFAULT); + } + + @Override + public Children likeLeft(boolean condition, String column, Object val) { + return likeValue(condition, LIKE, column, val, SqlLike.LEFT); + } + + @Override + public Children likeRight(boolean condition, String column, Object val) { + return likeValue(condition, LIKE, column, val, SqlLike.RIGHT); + } + + @Override + public Children between(boolean condition, String column, Object val1, Object val2) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN, + () -> formatParam(null, val1), AND, () -> formatParam(null, val2))); + } + + @Override + public Children notBetween(boolean condition, String column, Object val1, Object val2) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN, + () -> formatParam(null, val1), AND, () -> formatParam(null, val2))); + } + + + /* ****************************************** **/ + + + @Override + public Children isNull(boolean condition, String column) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IS_NULL)); + } + + @Override + public Children isNotNull(boolean condition, String column) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IS_NOT_NULL)); + } + + @Override + public Children in(boolean condition, String column, Collection coll) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IN, inExpression(coll))); + } + + @Override + public Children in(boolean condition, String column, Object... values) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IN, inExpression(values))); + } + + @Override + public Children notIn(boolean condition, String column, Collection coll) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN, inExpression(coll))); + } + + @Override + public Children notIn(boolean condition, String column, Object... values) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN, inExpression(values))); + } + + @Override + public Children inSql(boolean condition, String column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IN, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children gtSql(boolean condition, String column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), GT, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children geSql(boolean condition, String column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), GE, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children ltSql(boolean condition, String column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), LT, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children leSql(boolean condition, String column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), LE, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children notInSql(boolean condition, String column, String inValue) { + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN, + () -> String.format("(%s)", inValue))); + } + + @Override + public Children groupBy(boolean condition, String column, String... columns) { + return maybeDo(condition, () -> { + String one = columnToString(column); + if (ArrayUtils.isNotEmpty(columns)) { + one += (StringPool.COMMA + columnsToString(columns)); + } + final String finalOne = one; + appendSqlSegments(GROUP_BY, () -> finalOne); + }); + } + + @Override + public final Children orderBy(boolean condition, boolean isAsc, String column, String... columns) { + return maybeDo(condition, () -> { + final SqlKeyword mode = isAsc ? ASC : DESC; + appendSqlSegments(ORDER_BY, columnToSqlSegment(columnSqlInjectFilter(column)), mode); + if (ArrayUtils.isNotEmpty(columns)) { + Arrays.stream(columns).forEach(c -> appendSqlSegments(ORDER_BY, + columnToSqlSegment(columnSqlInjectFilter(c)), mode)); + } + }); + } + + protected String columnSqlInjectFilter(String column) { + return StringUtils.sqlInjectionReplaceBlank(column); + } + + @Override + public Children groupBy(boolean condition, String column) { + return maybeDo(condition, () -> appendSqlSegments(GROUP_BY, () -> columnToString(column))); + } + + @Override + public Children groupByStr(boolean condition, List columns) { + return maybeDo(condition, () -> appendSqlSegments(GROUP_BY, () -> columnsToString(columns))); + } + + @Override + public Children orderBy(boolean condition, boolean isAsc, String column) { + return maybeDo(condition, () -> appendSqlSegments(ORDER_BY, columnToSqlSegment(columnSqlInjectFilter(column)), + isAsc ? ASC : DESC)); + } + + @Override + public Children orderByStr(boolean condition, boolean isAsc, List columns) { + return maybeDo(condition, () -> columns.forEach(c -> appendSqlSegments(ORDER_BY, + columnToSqlSegment(columnSqlInjectFilter(c)), isAsc ? ASC : DESC))); + } + + } 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 10c0c22..4fce469 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,33 +2,27 @@ 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.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; -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.config.ConfigProperties; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.LogicInfoUtils; -import com.github.yulichang.toolkit.MPJWrappers; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.wrapper.interfaces.Query; import com.github.yulichang.wrapper.interfaces.QueryJoin; import com.github.yulichang.wrapper.interfaces.QueryLabel; -import com.github.yulichang.wrapper.interfaces.on.WrapperFunction; import com.github.yulichang.wrapper.resultmap.MybatisLabel; import com.github.yulichang.wrapper.segments.Select; import com.github.yulichang.wrapper.segments.SelectCache; import com.github.yulichang.wrapper.segments.SelectNormal; import lombok.Getter; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.BiConsumer; import java.util.stream.Collectors; /** @@ -36,7 +30,6 @@ import java.util.stream.Collectors; * Lambda 语法使用 Wrapper * * @author yulichang - * @see MPJWrappers */ @SuppressWarnings({"unused"}) public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper> @@ -46,10 +39,6 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper> resultMapMybatisLabel = new ArrayList<>(); /** - * 是否构建是否存在一对多 + * 是否有表别名 */ - @Getter - private boolean resultMap = false; + private boolean hasAlias; /** * 查询字段 sql */ @@ -73,10 +61,6 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapperlambdaQuery() + * 推荐使用 带 class 的构造方法 */ public MPJLambdaWrapper() { super.initNeed(); } + /** + * 推荐使用此构造方法 + */ + public MPJLambdaWrapper(Class clazz) { + super.initNeed(); + setEntityClass(clazz); + } + + /** + * 自定义主表别名 + */ + public MPJLambdaWrapper(String alias) { + this.alias = alias; + super.initNeed(); + } + + /** + * 推荐使用此构造方法 + */ + public MPJLambdaWrapper(Class clazz, String alias) { + this.alias = alias; + setEntityClass(clazz); + super.initNeed(); + } + /** * 不建议直接 new 该实例,使用 MPJWrappers.lambdaQuery() @@ -105,7 +114,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq, Map paramNameValuePairs, MergeSegments mergeSegments, SharedString lastSql, SharedString sqlComment, SharedString sqlFirst, - TableList tableList, String index, String keyWord, Class joinClass) { + TableList tableList, String index, String keyWord, Class joinClass, Node node) { super.setEntity(entity); super.setEntityClass(entityClass); this.paramNameSeq = paramNameSeq; @@ -119,6 +128,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper { - String str = Constant.TABLE_ALIAS + getDefaultSelect(i.getIndex(), i.getClazz(), i) + StringPool.DOT + i.getColumn(); - return i.isFunc() ? (String.format(i.getFunc().getSql(), str) + Constant.AS + i.getAlias()) : (i.isHasAlias() ? (str + Constant.AS + i.getAlias()) : str); + if (i.isStr()) { + return i.getColumn(); + } + Table t = tableList.get(i.getClazz()); + String str; + if (t.isHasAlias()) { + str = t.getAlias() + StringPool.DOT + i.getColumn(); + } else { + if (i.isLabel() && Objects.nonNull(i.getIndex())) { + str = i.getIndex() + StringPool.DOT + i.getColumn(); + } else { + str = t.getAlias() + getDefaultSelect(i.getIndex(), i.getClazz(), i) + StringPool.DOT + i.getColumn(); + } + } + if (i.isFunc()) { + SFunction[] args = i.getArgs(); + if (Objects.isNull(args) || args.length == 0) { + return String.format(i.getFunc().getSql(), str) + Constant.AS + i.getAlias(); + } else { + return String.format(i.getFunc().getSql(), Arrays.stream(args).map(arg -> { + Class entityClass = LambdaUtils.getEntityClass(arg); + Table table = tableList.getPositive(entityClass); + Assert.notNull(table, "table not find by class <%s>", entityClass.getSimpleName()); + Map mapField = ColumnCache.getMapField(entityClass); + SelectCache cache = mapField.get(LambdaUtils.getName(arg)); + return tableList.get(cache.getClazz()).getAlias() + (Objects.isNull(table.getIndex()) ? StringPool.EMPTY : table.getIndex()) + StringPool.DOT + cache.getColumn(); + }).toArray()) + Constant.AS + i.getAlias(); + } + } else { + return i.isHasAlias() ? (str + Constant.AS + i.getAlias()) : str; + } }).collect(Collectors.joining(StringPool.COMMA)); sqlSelect.setStringValue(s); } @@ -190,13 +229,25 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper wrapper : onWrappers) { - String tableName = TableInfoHelper.getTableInfo(wrapper.getJoinClass()).getTableName(); - value.append(wrapper.getKeyWord()) - .append(tableName) - .append(Constant.SPACE_TABLE_ALIAS) - .append(tableList.get(wrapper.getJoinClass(), wrapper.getIndex()).getIndex()) - .append(Constant.ON) - .append(wrapper.getExpression().getNormal().getSqlSegment()); + if (StringUtils.isBlank(wrapper.from.getStringValue())) { + TableInfo info = TableInfoHelper.getTableInfo(wrapper.getJoinClass()); + Assert.notNull(info, "table not find by class <%s>", wrapper.getJoinClass().getSimpleName()); + String tableName = info.getTableName(); + value.append(StringPool.SPACE) + .append(wrapper.getKeyWord()) + .append(StringPool.SPACE) + .append(tableName) + .append(StringPool.SPACE) + .append(wrapper.hasAlias ? wrapper.alias : (wrapper.alias + (tableList.get(wrapper.getJoinClass(), wrapper.getIndex()).getIndex()))) + .append(Constant.ON) + .append(wrapper.getExpression().getNormal().getSqlSegment()); + } else { + value.append(StringPool.EMPTY) + .append(wrapper.getKeyWord()) + .append(StringPool.EMPTY) + .append(wrapper.from.getStringValue()) + .append(StringPool.EMPTY); + } } from.setStringValue(value.toString()); } @@ -204,7 +255,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper instance() { - return instance(index, null, null); + return instance(index, null, null, this.node); } - protected MPJLambdaWrapper instance(String index, String keyWord, Class joinClass) { + protected MPJLambdaWrapper instance(String index, String keyWord, Class joinClass, Node node) { return new MPJLambdaWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs, new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), - this.tableList, index, keyWord, joinClass); + this.tableList, index, keyWord, joinClass, node); } @Override @@ -275,7 +326,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper LogicInfoUtils.getLogicInfo(t.getIndex(), - t.getClazz())).collect(Collectors.joining(StringPool.SPACE)); + t.getClazz(), t.isHasAlias(), t.getAlias())).collect(Collectors.joining(StringPool.SPACE)); } return StringPool.EMPTY; } @@ -288,19 +339,47 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper join(String keyWord, Class clazz, WrapperFunction function, WrapperFunction ext) { - String name = String.valueOf(tableIndex); - MPJLambdaWrapper apply = function.apply(instance(name, keyWord, clazz)); - tableList.add(clazz, name); - onWrappers.add(apply); - tableIndex++; - if (Objects.nonNull(ext)) { - this.index = name; - MPJLambdaWrapper wrapper = ext.apply(typedThis); - wrapper.index = null; + public MPJLambdaWrapper join(String keyWord, Class clazz, String tableAlias, BiConsumer, MPJLambdaWrapper> consumer) { + String oldIndex = this.getIndex(); + String newIndex = String.valueOf(tableIndex); + Node n = Objects.isNull(oldIndex) ? new Node(clazz, tableIndex, ROOT_NODE) : new Node(clazz, tableIndex, this.node); + MPJLambdaWrapper instance = instance(newIndex, keyWord, clazz, n); + this.node = n; + onWrappers.add(instance); + if (StringUtils.isBlank(tableAlias)) { + tableList.add(clazz, newIndex, false, ConfigProperties.tableAlias); + instance.alias = ConfigProperties.tableAlias; + instance.hasAlias = false; + tableIndex++; + } else { + tableList.add(clazz, null, true, tableAlias); + instance.alias = tableAlias; + instance.hasAlias = true; + } + this.index = newIndex; + consumer.accept(instance, typedThis); + this.index = oldIndex; + return typedThis; + } + + + /** + * 自定义关键词连接 + * + * @param keyWord 连表关键词 + * @param condition 条件 + * @param joinSql sql + */ + @Override + public MPJLambdaWrapper join(String keyWord, boolean condition, String joinSql) { + if (condition) { + MPJLambdaWrapper wrapper = new MPJLambdaWrapper<>(); + wrapper.from.setStringValue(joinSql); + wrapper.keyWord = StringPool.SPACE + keyWord + StringPool.SPACE; + onWrappers.add(wrapper); } return typedThis; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/CompareStr.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/CompareStr.java new file mode 100644 index 0000000..c70f917 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/CompareStr.java @@ -0,0 +1,284 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.wrapper.interfaces; + +import java.io.Serializable; +import java.util.Map; +import java.util.function.BiPredicate; + +/** + * copu {@link com.baomidou.mybatisplus.core.conditions.interfaces.Compare} + * + * @since 1.3.12 + */ +@SuppressWarnings("ALL") +public interface CompareStr extends Serializable { + + /** + * ignore + */ + default Children allEqStr(Map params) { + return allEqStr(params, true); + } + + /** + * ignore + */ + default Children allEqStr(Map params, boolean null2IsNull) { + return allEqStr(true, params, null2IsNull); + } + + /** + * map 所有非空属性等于 = + * + * @param condition 执行条件 + * @param params map 类型的参数, key 是字段名, value 是字段值 + * @param null2IsNull 是否参数为 null 自动执行 isNull 方法, false 则忽略这个字段\ + * @return children + */ + Children allEqStr(boolean condition, Map params, boolean null2IsNull); + + /** + * ignore + */ + default Children allEqStr(BiPredicate filter, Map params) { + return allEqStr(filter, params, true); + } + + /** + * ignore + */ + default Children allEqStr(BiPredicate filter, Map params, boolean null2IsNull) { + return allEqStr(true, filter, params, null2IsNull); + } + + /** + * 字段过滤接口,传入多参数时允许对参数进行过滤 + * + * @param condition 执行条件 + * @param filter 返回 true 来允许字段传入比对条件中 + * @param params map 类型的参数, key 是字段名, value 是字段值 + * @param null2IsNull 是否参数为 null 自动执行 isNull 方法, false 则忽略这个字段 + * @return children + */ + Children allEqStr(boolean condition, BiPredicate filter, Map params, boolean null2IsNull); + + /** + * ignore + */ + default Children eq(R column, Object val) { + return eq(true, column, val); + } + + /** + * 等于 = + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children eq(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children ne(R column, Object val) { + return ne(true, column, val); + } + + /** + * 不等于 <> + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children ne(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children gt(R column, Object val) { + return gt(true, column, val); + } + + /** + * 大于 > + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children gt(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children ge(R column, Object val) { + return ge(true, column, val); + } + + /** + * 大于等于 >= + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children ge(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children lt(R column, Object val) { + return lt(true, column, val); + } + + /** + * 小于 < + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children lt(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children le(R column, Object val) { + return le(true, column, val); + } + + /** + * 小于等于 <= + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children le(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children between(R column, Object val1, Object val2) { + return between(true, column, val1, val2); + } + + /** + * BETWEEN 值1 AND 值2 + * + * @param condition 执行条件 + * @param column 字段 + * @param val1 值1 + * @param val2 值2 + * @return children + */ + Children between(boolean condition, R column, Object val1, Object val2); + + /** + * ignore + */ + default Children notBetween(R column, Object val1, Object val2) { + return notBetween(true, column, val1, val2); + } + + /** + * NOT BETWEEN 值1 AND 值2 + * + * @param condition 执行条件 + * @param column 字段 + * @param val1 值1 + * @param val2 值2 + * @return children + */ + Children notBetween(boolean condition, R column, Object val1, Object val2); + + /** + * ignore + */ + default Children like(R column, Object val) { + return like(true, column, val); + } + + /** + * LIKE '%值%' + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children like(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children notLike(R column, Object val) { + return notLike(true, column, val); + } + + /** + * NOT LIKE '%值%' + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children notLike(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children likeLeft(R column, Object val) { + return likeLeft(true, column, val); + } + + /** + * LIKE '%值' + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children likeLeft(boolean condition, R column, Object val); + + /** + * ignore + */ + default Children likeRight(R column, Object val) { + return likeRight(true, column, val); + } + + /** + * LIKE '值%' + * + * @param condition 执行条件 + * @param column 字段 + * @param val 值 + * @return children + */ + Children likeRight(boolean condition, R column, Object val); +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java index abbecba..a167f2b 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java @@ -12,7 +12,7 @@ import java.util.function.Consumer; *

* copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Func} */ -@SuppressWarnings({"unchecked", "unused"}) +@SuppressWarnings({"unchecked", "unused", "JavadocDeclaration"}) public interface Func extends Serializable { /** @@ -168,6 +168,83 @@ public interface Func extends Serializable { */ Children notInSql(boolean condition, SFunction column, String inValue); + + /** + * 字段 > ( sql语句 ) + *

例1: gtSql("id", "1, 2, 3, 4, 5, 6")

+ *

例1: gtSql("id", "select id from table where name = 'JunJun'")

+ * + * @param condition + * @param column + * @param inValue + * @return + */ + Children gtSql(boolean condition, SFunction column, String inValue); + + /** + * ignore + */ + default Children gtSql(SFunction column, String inValue) { + return gtSql(true, column, inValue); + } + + /** + * 字段 >= ( sql语句 ) + *

例1: geSql("id", "1, 2, 3, 4, 5, 6")

+ *

例1: geSql("id", "select id from table where name = 'JunJun'")

+ * + * @param condition + * @param column + * @param inValue + * @return + */ + Children geSql(boolean condition, SFunction column, String inValue); + + /** + * ignore + */ + default Children geSql(SFunction column, String inValue) { + return geSql(true, column, inValue); + } + + /** + * 字段 < ( sql语句 ) + *

例1: ltSql("id", "1, 2, 3, 4, 5, 6")

+ *

例1: ltSql("id", "select id from table where name = 'JunJun'")

+ * + * @param condition + * @param column + * @param inValue + * @return + */ + Children ltSql(boolean condition, SFunction column, String inValue); + + /** + * ignore + */ + default Children ltSql(SFunction column, String inValue) { + return ltSql(true, column, inValue); + } + + /** + * 字段 <= ( sql语句 ) + *

例1: leSql("id", "1, 2, 3, 4, 5, 6")

+ *

例1: leSql("id", "select id from table where name = 'JunJun'")

+ * + * @param condition + * @param column + * @param inValue + * @return + */ + Children leSql(boolean condition, SFunction column, String inValue); + + /** + * ignore + */ + default Children leSql(SFunction column, String inValue) { + return leSql(true, column, inValue); + } + /** * ignore */ diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/FuncStr.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/FuncStr.java new file mode 100644 index 0000000..570636a --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/FuncStr.java @@ -0,0 +1,408 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.wrapper.interfaces; + +import java.io.Serializable; +import java.util.Collection; +import java.util.List; + +/** + * copy {@link com.baomidou.mybatisplus.core.conditions.interfaces.Func} + * + * @since 1.3.12 + */ +@SuppressWarnings("ALL") +public interface FuncStr extends Serializable { + + /** + * ignore + */ + default Children isNull(R column) { + return isNull(true, column); + } + + /** + * 字段 IS NULL + *

例: isNull("name")

+ * + * @param condition 执行条件 + * @param column 字段 + * @return children + */ + Children isNull(boolean condition, R column); + + /** + * ignore + */ + default Children isNotNull(R column) { + return isNotNull(true, column); + } + + /** + * 字段 IS NOT NULL + *

例: isNotNull("name")

+ * + * @param condition 执行条件 + * @param column 字段 + * @return children + */ + Children isNotNull(boolean condition, R column); + + /** + * ignore + */ + default Children in(R column, Collection coll) { + return in(true, column, coll); + } + + /** + * 字段 IN (value.get(0), value.get(1), ...) + *

例: in("id", Arrays.asList(1, 2, 3, 4, 5))

+ * + *
  • 注意!集合为空若存在逻辑错误,请在 condition 条件中判断
  • + *
  • 如果集合为 empty 则不会进行 sql 拼接
  • + * + * @param condition 执行条件 + * @param column 字段 + * @param coll 数据集合 + * @return children + */ + Children in(boolean condition, R column, Collection coll); + + /** + * ignore + */ + default Children in(R column, Object... values) { + return in(true, column, values); + } + + /** + * 字段 IN (v0, v1, ...) + *

    例: in("id", 1, 2, 3, 4, 5)

    + * + *
  • 注意!数组为空若存在逻辑错误,请在 condition 条件中判断
  • + *
  • 如果动态数组为 empty 则不会进行 sql 拼接
  • + * + * @param condition 执行条件 + * @param column 字段 + * @param values 数据数组 + * @return children + */ + Children in(boolean condition, R column, Object... values); + + /** + * ignore + */ + default Children notIn(R column, Collection coll) { + return notIn(true, column, coll); + } + + /** + * 字段 NOT IN (value.get(0), value.get(1), ...) + *

    例: notIn("id", Arrays.asList(1, 2, 3, 4, 5))

    + * + * @param condition 执行条件 + * @param column 字段 + * @param coll 数据集合 + * @return children + */ + Children notIn(boolean condition, R column, Collection coll); + + /** + * ignore + */ + default Children notIn(R column, Object... value) { + return notIn(true, column, value); + } + + /** + * 字段 NOT IN (v0, v1, ...) + *

    例: notIn("id", 1, 2, 3, 4, 5)

    + * + * @param condition 执行条件 + * @param column 字段 + * @param values 数据数组 + * @return children + */ + Children notIn(boolean condition, R column, Object... values); + + /** + * ignore + */ + default Children inSql(R column, String inValue) { + return inSql(true, column, inValue); + } + + /** + * 字段 IN ( sql语句 ) + *

    !! sql 注入方式的 in 方法 !!

    + *

    例1: inSql("id", "1, 2, 3, 4, 5, 6")

    + *

    例2: inSql("id", "select id from table where id < 3")

    + * + * @param condition 执行条件 + * @param column 字段 + * @param inValue sql语句 + * @return children + */ + Children inSql(boolean condition, R column, String inValue); + + /** + * 字段 > ( sql语句 ) + *

    例1: gtSql("id", "1, 2, 3, 4, 5, 6")

    + *

    例1: gtSql("id", "select id from table where name = 'JunJun'")

    + * + * @param condition + * @param column + * @param inValue + * @return + */ + Children gtSql(boolean condition, R column, String inValue); + + /** + * ignore + */ + default Children gtSql(R column, String inValue) { + return gtSql(true, column, inValue); + } + + /** + * 字段 >= ( sql语句 ) + *

    例1: geSql("id", "1, 2, 3, 4, 5, 6")

    + *

    例1: geSql("id", "select id from table where name = 'JunJun'")

    + * + * @param condition + * @param column + * @param inValue + * @return + */ + Children geSql(boolean condition, R column, String inValue); + + /** + * ignore + */ + default Children geSql(R column, String inValue) { + return geSql(true, column, inValue); + } + + /** + * 字段 < ( sql语句 ) + *

    例1: ltSql("id", "1, 2, 3, 4, 5, 6")

    + *

    例1: ltSql("id", "select id from table where name = 'JunJun'")

    + * + * @param condition + * @param column + * @param inValue + * @return + */ + Children ltSql(boolean condition, R column, String inValue); + + /** + * ignore + */ + default Children ltSql(R column, String inValue) { + return ltSql(true, column, inValue); + } + + /** + * 字段 <= ( sql语句 ) + *

    例1: leSql("id", "1, 2, 3, 4, 5, 6")

    + *

    例1: leSql("id", "select id from table where name = 'JunJun'")

    + * + * @param condition + * @param column + * @param inValue + * @return + */ + Children leSql(boolean condition, R column, String inValue); + + /** + * ignore + */ + default Children leSql(R column, String inValue) { + return leSql(true, column, inValue); + } + + /** + * ignore + */ + default Children notInSql(R column, String inValue) { + return notInSql(true, column, inValue); + } + + /** + * 字段 NOT IN ( sql语句 ) + *

    !! sql 注入方式的 not in 方法 !!

    + *

    例1: notInSql("id", "1, 2, 3, 4, 5, 6")

    + *

    例2: notInSql("id", "select id from table where id < 3")

    + * + * @param condition 执行条件 + * @param column 字段 + * @param inValue sql语句 ---> 1,2,3,4,5,6 或者 select id from table where id < 3 + * @return children + */ + Children notInSql(boolean condition, R column, String inValue); + + /** + * 分组:GROUP BY 字段, ... + *

    例: groupBy("id")

    + * + * @param condition 执行条件 + * @param column 单个字段 + * @return children + */ + Children groupBy(boolean condition, R column); + + default Children groupBy(R column) { + return groupBy(true, column); + } + + /** + * 分组:GROUP BY 字段, ... + *

    例: groupBy(Arrays.asList("id", "name"))

    + * + * @param condition 执行条件 + * @param columns 字段数组 + * @return children + */ + Children groupByStr(boolean condition, List columns); + + default Children groupByStr(List columns) { + return groupByStr(true, columns); + } + + default Children groupBy(R column, R... columns) { + return groupBy(true, column, columns); + } + + /** + * 分组:GROUP BY 字段, ... + */ + Children groupBy(boolean condition, R column, R... columns); + + /** + * 排序:ORDER BY 字段, ... ASC + *

    例: orderByAsc(true, "id")

    + * + * @param condition 执行条件 + * @param column 单个字段 + * @return children + */ + default Children orderByAsc(boolean condition, R column) { + return orderBy(condition, true, column); + } + + default Children orderByAsc(R column) { + return orderByAsc(true, column); + } + + /** + * 排序:ORDER BY 字段, ... ASC + *

    例: orderByAsc(true, Arrays.asList("id", "name"))

    + * + * @param condition 执行条件 + * @param columns 字段数组 + * @return children + */ + default Children orderByAscStr(boolean condition, List columns) { + return orderByStr(condition, true, columns); + } + + default Children orderByAscStr(List columns) { + return orderByAscStr(true, columns); + } + + default Children orderByAsc(R column, R... columns) { + return orderByAsc(true, column, columns); + } + + /** + * 排序:ORDER BY 字段, ... ASC + */ + default Children orderByAsc(boolean condition, R column, R... columns) { + return orderBy(condition, true, column, columns); + } + + /** + * 排序:ORDER BY 字段, ... DESC + *

    例: orderByDesc(true, "id")

    + * + * @param condition 执行条件 + * @param column 字段 + * @return children + */ + default Children orderByDesc(boolean condition, R column) { + return orderBy(condition, false, column); + } + + default Children orderByDesc(R column) { + return orderByDesc(true, column); + } + + /** + * 排序:ORDER BY 字段, ... DESC + *

    例: orderByDesc(true, Arrays.asList("id", "name"))

    + * + * @param condition 执行条件 + * @param columns 字段列表 + * @return children + */ + default Children orderByDescStr(boolean condition, List columns) { + return orderByStr(condition, false, columns); + } + + default Children orderByDescStr(List columns) { + return orderByDescStr(true, columns); + } + + default Children orderByDesc(R column, R... columns) { + return orderByDesc(true, column, columns); + } + + /** + * 排序:ORDER BY 字段, ... DESC + */ + default Children orderByDesc(boolean condition, R column, R... columns) { + return orderBy(condition, false, column, columns); + } + + /** + * 排序:ORDER BY 字段, ... + *

    例: orderBy(true, "id")

    + * + * @param condition 执行条件 + * @param isAsc 是否是 ASC 排序 + * @param column 单个字段 + * @return children + */ + Children orderBy(boolean condition, boolean isAsc, R column); + + /** + * 排序:ORDER BY 字段, ... + *

    例: orderBy(true, Arrays.asList("id", "name"))

    + * + * @param condition 执行条件 + * @param isAsc 是否是 ASC 排序 + * @param columns 字段列表 + * @return children + */ + Children orderByStr(boolean condition, boolean isAsc, List columns); + + /** + * 排序:ORDER BY 字段, ... + */ + Children orderBy(boolean condition, boolean isAsc, R column, R... columns); + +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnCompare.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/OnCompare.java similarity index 98% rename from mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnCompare.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/OnCompare.java index 0113cb7..0906cea 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnCompare.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/OnCompare.java @@ -1,4 +1,4 @@ -package com.github.yulichang.wrapper.interfaces.on; +package com.github.yulichang.wrapper.interfaces; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java index 5c82594..e4178aa 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java @@ -3,6 +3,7 @@ package com.github.yulichang.wrapper.interfaces; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; +import com.baomidou.mybatisplus.core.toolkit.Assert; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.MPJReflectionKit; @@ -13,8 +14,10 @@ import com.github.yulichang.wrapper.segments.*; import java.io.Serializable; import java.lang.reflect.Field; +import java.util.Arrays; import java.util.List; import java.util.Map; +import java.util.function.Function; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -47,6 +50,7 @@ public interface Query extends Serializable { */ default Children select(Class entityClass, Predicate predicate) { TableInfo info = TableInfoHelper.getTableInfo(entityClass); + Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName()); Map cacheMap = ColumnCache.getMapField(entityClass); info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach( i -> getSelectColum().add(new SelectNormal(cacheMap.get(i.getProperty()), getIndex()))); @@ -57,6 +61,16 @@ public interface Query extends Serializable { @SuppressWarnings("unchecked") Children select(SFunction... columns); + /** + * String 查询 + * + * @param columns 列 + */ + default Children select(String... columns) { + getSelectColum().addAll(Arrays.stream(columns).map(SelectString::new).collect(Collectors.toList())); + return getChildren(); + } + /** * 说明: * 比如我们需要查询用户表有10个字段,然而我们只需要3个就够了,用mybatis-plus提供的select

    @@ -148,6 +162,16 @@ public interface Query extends Serializable { } + default Children selectFunc(String sql, Function[]> column, String alias) { + getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(new SelectFunc.Func()))); + return getChildren(); + } + + default Children selectFunc(String sql, Function[]> column, SFunction alias) { + getSelectColum().add(new SelectFunc(LambdaUtils.getName(alias), getIndex(), () -> sql, column.apply(new SelectFunc.Func()))); + return getChildren(); + } + /* 默认聚合函数扩展 */ /** diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java index b3760de..d933773 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java @@ -2,15 +2,18 @@ package com.github.yulichang.wrapper.interfaces; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.interfaces.MPJBaseJoin; +import com.github.yulichang.query.interfaces.StringJoin; import com.github.yulichang.toolkit.Constant; -import com.github.yulichang.toolkit.LambdaUtils; -import com.github.yulichang.wrapper.interfaces.on.WrapperFunction; +import com.github.yulichang.wrapper.MPJAbstractLambdaWrapper; +import com.github.yulichang.wrapper.MPJLambdaWrapper; + +import java.util.function.BiConsumer; /** * @author yulichang */ @SuppressWarnings("unused") -public interface QueryJoin extends MPJBaseJoin { +public interface QueryJoin extends MPJBaseJoin, StringJoin { /** * left join @@ -20,17 +23,7 @@ public interface QueryJoin extends MPJBaseJoin { * @param right 条件 */ default Children leftJoin(Class clazz, SFunction left, SFunction right) { - return leftJoin(clazz, on -> on.eq(left, right)); - } - - /** - * left join - * - * @param left 条件 - * @param right 条件 - */ - default Children leftJoin(SFunction left, SFunction right) { - return leftJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right)); + return join(Constant.LEFT_JOIN, clazz, left, right); } /** @@ -39,10 +32,10 @@ public interface QueryJoin extends MPJBaseJoin { * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) * * @param clazz 关联实体类 - * @param function 条件 + * @param function 条件` */ - default Children leftJoin(Class clazz, WrapperFunction function) { - return join(Constant.LEFT_JOIN, clazz, function, null); + default Children leftJoin(Class clazz, WrapperFunction> function) { + return join(Constant.LEFT_JOIN, clazz, function); } /** @@ -52,18 +45,31 @@ public interface QueryJoin extends MPJBaseJoin { * @param left 条件 * @param right 条件 */ - default Children leftJoin(Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return leftJoin(clazz, on -> on.eq(left, right), ext); + default Children leftJoin(Class clazz, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.LEFT_JOIN, clazz, left, right, ext); + } + + /** + * left join 多条件 + *

    + * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param consumer 条件 + */ + default Children leftJoin(Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.LEFT_JOIN, clazz, consumer); } /** * left join * + * @param clazz 关联的实体类 * @param left 条件 * @param right 条件 */ - default Children leftJoin(SFunction left, SFunction right, WrapperFunction ext) { - return leftJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + default Children leftJoin(Class clazz, String alias, SFunction left, SFunction right) { + return join(Constant.LEFT_JOIN, clazz,alias, left, right); } /** @@ -74,147 +80,294 @@ public interface QueryJoin extends MPJBaseJoin { * @param clazz 关联实体类 * @param function 条件 */ - default Children leftJoin(Class clazz, WrapperFunction function, WrapperFunction ext) { - return join(Constant.LEFT_JOIN, clazz, function, ext); + default Children leftJoin(Class clazz, String alias, WrapperFunction> function) { + return join(Constant.LEFT_JOIN, clazz,alias, function); + } + + /** + * left join + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children leftJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.LEFT_JOIN, clazz,alias, left, right, ext); + } + + /** + * left join 多条件 + *

    + * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param consumer 条件 + */ + default Children leftJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.LEFT_JOIN, clazz,alias, consumer); } /** * ignore 参考 left join */ default Children rightJoin(Class clazz, SFunction left, SFunction right) { - return rightJoin(clazz, on -> on.eq(left, right)); + return join(Constant.RIGHT_JOIN, clazz, left, right); } /** * ignore 参考 left join */ - default Children rightJoin(SFunction left, SFunction right) { - return rightJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right)); + default Children rightJoin(Class clazz, WrapperFunction> function) { + return join(Constant.RIGHT_JOIN, clazz, function); } /** * ignore 参考 left join */ - default Children rightJoin(Class clazz, WrapperFunction function) { - return join(Constant.RIGHT_JOIN, clazz, function, null); + default Children rightJoin(Class clazz, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.RIGHT_JOIN, clazz, left, right, ext); } /** * ignore 参考 left join */ - default Children rightJoin(Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return rightJoin(clazz, on -> on.eq(left, right), ext); + default Children rightJoin(Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.RIGHT_JOIN, clazz, consumer); } /** * ignore 参考 left join */ - default Children rightJoin(SFunction left, SFunction right, WrapperFunction ext) { - return rightJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + default Children rightJoin(Class clazz, String alias, SFunction left, SFunction right) { + return join(Constant.RIGHT_JOIN, clazz,alias, left, right); } /** * ignore 参考 left join */ - default Children rightJoin(Class clazz, WrapperFunction function, WrapperFunction ext) { - return join(Constant.RIGHT_JOIN, clazz, function, ext); + default Children rightJoin(Class clazz, String alias, WrapperFunction> function) { + return join(Constant.RIGHT_JOIN, clazz,alias, function); } + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.RIGHT_JOIN, clazz,alias, left, right, ext); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.RIGHT_JOIN, clazz,alias, consumer); + } + + /** * ignore 参考 left join */ default Children innerJoin(Class clazz, SFunction left, SFunction right) { - return innerJoin(clazz, on -> on.eq(left, right)); + return join(Constant.INNER_JOIN, clazz, on -> on.eq(left, right)); } /** * ignore 参考 left join */ - default Children innerJoin(SFunction left, SFunction right) { - return innerJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right)); + default Children innerJoin(Class clazz, WrapperFunction> function) { + return join(Constant.INNER_JOIN, clazz, function); } /** * ignore 参考 left join */ - default Children innerJoin(Class clazz, WrapperFunction function) { - return join(Constant.INNER_JOIN, clazz, function, null); + default Children innerJoin(Class clazz, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.INNER_JOIN, clazz, left, right, ext); } /** * ignore 参考 left join */ - default Children innerJoin(Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return innerJoin(clazz, on -> on.eq(left, right), ext); + default Children innerJoin(Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.INNER_JOIN, clazz, consumer); + } + + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, String alias, SFunction left, SFunction right) { + return join(Constant.INNER_JOIN, clazz,alias, on -> on.eq(left, right)); } /** * ignore 参考 left join */ - default Children innerJoin(SFunction left, SFunction right, WrapperFunction ext) { - return innerJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + default Children innerJoin(Class clazz, String alias, WrapperFunction> function) { + return join(Constant.INNER_JOIN, clazz,alias, function); } /** * ignore 参考 left join */ - default Children innerJoin(Class clazz, WrapperFunction function, WrapperFunction ext) { - return join(Constant.INNER_JOIN, clazz, function, ext); + default Children innerJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.INNER_JOIN, clazz,alias, left, right, ext); } + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.INNER_JOIN, clazz,alias, consumer); + } /** * ignore 参考 left join */ default Children fullJoin(Class clazz, SFunction left, SFunction right) { - return fullJoin(clazz, on -> on.eq(left, right)); + return join(Constant.FULL_JOIN, clazz, left, right); } /** * ignore 参考 left join */ - default Children fullJoin(SFunction left, SFunction right) { - return fullJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right)); + default Children fullJoin(Class clazz, WrapperFunction> function) { + return join(Constant.FULL_JOIN, clazz, function); } /** * ignore 参考 left join */ - default Children fullJoin(Class clazz, WrapperFunction function) { - return join(Constant.FULL_JOIN, clazz, function, null); + default Children fullJoin(Class clazz, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.FULL_JOIN, clazz, left, right, ext); } /** * ignore 参考 left join */ - default Children fullJoin(Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return fullJoin(clazz, on -> on.eq(left, right), ext); + default Children fullJoin(Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.FULL_JOIN, clazz, consumer); } /** * ignore 参考 left join */ - default Children fullJoin(SFunction left, SFunction right, WrapperFunction ext) { - return fullJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + default Children fullJoin(Class clazz, String alias, SFunction left, SFunction right) { + return join(Constant.FULL_JOIN, clazz,alias, left, right); } /** * ignore 参考 left join */ - default Children fullJoin(Class clazz, WrapperFunction function, WrapperFunction ext) { - return join(Constant.FULL_JOIN, clazz, function, ext); + default Children fullJoin(Class clazz, String alias, WrapperFunction> function) { + return join(Constant.FULL_JOIN, clazz,alias, function); } + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.FULL_JOIN, clazz,alias, left, right, ext); + } /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.FULL_JOIN, clazz,alias, consumer); + } + + /** + * 自定义连表关键词 * 调用此方法 keyword 前后需要带空格 比如 " LEFT JOIN " " RIGHT JOIN " *

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

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

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

    + * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param function 条件 + */ + default Children join(String keyWord, Class clazz, String alias, WrapperFunction> function) { + return join(keyWord, clazz,alias, (on, e) -> function.apply(on)); + } + + /** + * 自定义连表关键词 + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children join(String keyWord, Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(keyWord, clazz,alias, (on, e) -> { + on.eq(left, right); + ext.apply(e); + }); + } + + /** + * 内部使用, 不建议直接调用 + */ + default Children join(String keyWord, Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + return join(keyWord, clazz, null, consumer); + } + + /** + * 内部使用, 不建议直接调用 + */ + Children join(String keyWord, Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryLabel.java index 3b86af0..b9b5651 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryLabel.java @@ -46,20 +46,19 @@ public interface QueryLabel { return selectCollection(null, child, dtoField); } - default > Children selectCollection(Integer index, Class child, SFunction dtoField) { + default > Children selectCollection(String prefix, Class child, SFunction dtoField) { String dtoFieldName = LambdaUtils.getName(dtoField); Class dtoClass = LambdaUtils.getEntityClass(dtoField); Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass); Field field = fieldMap.get(dtoFieldName); Class genericType = MPJReflectionKit.getGenericType(field); MybatisLabel.Builder builder; - String s = Objects.isNull(index) ? null : index.toString(); if (genericType == null || genericType.isAssignableFrom(child)) { //找不到集合泛型 List List List , 直接查询数据库实体 - builder = new MybatisLabel.Builder<>(s, dtoFieldName, child, field.getType()); + builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType()); } else { Class ofType = (Class) genericType; - builder = new MybatisLabel.Builder<>(s, dtoFieldName, child, field.getType(), ofType, true); + builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType(), ofType, true); } addLabel(builder.build()); return getChildren(); @@ -99,7 +98,7 @@ public interface QueryLabel { return selectCollection(null, child, dtoField, collection); } - default > Children selectCollection(Integer index, + default > Children selectCollection(String prefix, Class child, SFunction dtoField, MFunc> collection) { @@ -109,8 +108,7 @@ public interface QueryLabel { //获取集合泛型 Class genericType = MPJReflectionKit.getGenericType(field); Class ofType = (Class) genericType; - MybatisLabel.Builder builder = new MybatisLabel.Builder<>(Objects.isNull(index) ? null : index.toString(), - dtoFieldName, child, field.getType(), ofType, false); + MybatisLabel.Builder builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType(), ofType, false); MybatisLabel.Builder czBuilder = collection.apply(builder); addLabel(czBuilder.build()); return getChildren(); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/WrapperFunction.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/WrapperFunction.java new file mode 100644 index 0000000..6ea8aad --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/WrapperFunction.java @@ -0,0 +1,13 @@ +package com.github.yulichang.wrapper.interfaces; + +/** + * on function + * + * @author yulichang + * @since 1.1.8 + */ +@FunctionalInterface +public interface WrapperFunction { + + T apply(T wrapper); +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/WrapperFunction.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/WrapperFunction.java deleted file mode 100644 index 0679d71..0000000 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/WrapperFunction.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.github.yulichang.wrapper.interfaces.on; - -import com.github.yulichang.wrapper.MPJLambdaWrapper; - -/** - * on function - * - * @author yulichang - * @since 1.1.8 - */ -@FunctionalInterface -public interface WrapperFunction { - - MPJLambdaWrapper apply(MPJLambdaWrapper wrapper); -} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java index 240eec2..a4f1c83 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java @@ -121,20 +121,19 @@ public class MybatisLabel { /** * 嵌套 */ - public > Builder collection(Integer index, Class entityClass, SFunction func) { + public > Builder collection(String prefix, Class entityClass, SFunction func) { String dtoFieldName = LambdaUtils.getName(func); Class dtoClass = LambdaUtils.getEntityClass(func); Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass); Field field = fieldMap.get(dtoFieldName); Class genericType = MPJReflectionKit.getGenericType(field); MybatisLabel.Builder builder; - String s = Objects.isNull(index) ? null : index.toString(); if (genericType == null || genericType.isAssignableFrom(entityClass)) { //找不到集合泛型 List List List , 直接查询数据库实体 - builder = new Builder<>(s, dtoFieldName, entityClass, field.getType()); + builder = new Builder<>(prefix, dtoFieldName, entityClass, field.getType()); } else { Class ofType = (Class) genericType; - builder = new Builder<>(s, dtoFieldName, entityClass, field.getType(), ofType, true); + builder = new Builder<>(prefix, dtoFieldName, entityClass, field.getType(), ofType, true); } mybatisLabel.mybatisLabels.add(builder.build()); return this; @@ -147,7 +146,7 @@ public class MybatisLabel { /** * 嵌套 */ - public > Builder collection(Integer index, + public > Builder collection(String prefix, Class entityClass, SFunction func, MFunc> mFunc) { @@ -157,8 +156,7 @@ public class MybatisLabel { //获取集合泛型 Class genericType = MPJReflectionKit.getGenericType(field); Class ofType = (Class) genericType; - MybatisLabel.Builder builder = new MybatisLabel.Builder<>(Objects.isNull(index) ? null : index.toString(), - dtoFieldName, entityClass, field.getType(), ofType, false); + MybatisLabel.Builder builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, entityClass, field.getType(), ofType, false); mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build()); return this; } @@ -212,6 +210,7 @@ public class MybatisLabel { private void autoBuild(boolean auto, Class entityClass, Class tagClass) { TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); + Assert.notNull(tableInfo, "table not find by class <%s>", entityClass.getSimpleName()); Map tagMap = MPJReflectionKit.getFieldMap(tagClass); if (auto && !tagMap.isEmpty()) { List listField = ColumnCache.getListField(entityClass); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java index 498a53e..51408f5 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java @@ -1,6 +1,7 @@ package com.github.yulichang.wrapper.segments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import org.apache.ibatis.type.TypeHandler; @@ -38,7 +39,11 @@ public interface Select { boolean isFunc(); + SFunction[] getArgs(); + BaseFuncEnum getFunc(); boolean isLabel(); + + boolean isStr(); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java index 7732541..92b982c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java @@ -2,6 +2,7 @@ package com.github.yulichang.wrapper.segments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import lombok.Getter; import org.apache.ibatis.type.TypeHandler; @@ -82,6 +83,11 @@ public class SelectAlias implements Select { return false; } + @Override + public SFunction[] getArgs() { + return null; + } + @Override public BaseFuncEnum getFunc() { @@ -92,4 +98,9 @@ public class SelectAlias implements Select { public boolean isLabel() { return false; } + + @Override + public boolean isStr() { + return false; + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java index 98ea88b..f6e2bde 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java @@ -2,6 +2,7 @@ package com.github.yulichang.wrapper.segments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import lombok.Getter; import org.apache.ibatis.type.TypeHandler; @@ -23,6 +24,8 @@ public class SelectFunc implements Select { private final String column; + private final SFunction[] args; + private final boolean hasAlias; private final String alias; @@ -37,6 +40,7 @@ public class SelectFunc implements Select { this.cache = cache; this.column = cache.getColumn(); this.hasAlias = true; + this.args = null; this.alias = alias; this.isFunc = true; this.func = func; @@ -45,6 +49,18 @@ public class SelectFunc implements Select { public SelectFunc(String alias, String index, BaseFuncEnum func, String column) { this.index = index; this.column = column; + this.args = null; + this.cache = null; + this.hasAlias = true; + this.alias = alias; + this.isFunc = true; + this.func = func; + } + + public SelectFunc(String alias, String index, BaseFuncEnum func, SFunction[] args) { + this.index = index; + this.column = null; + this.args = args; this.cache = null; this.hasAlias = true; this.alias = alias; @@ -98,4 +114,59 @@ public class SelectFunc implements Select { public boolean isLabel() { return false; } + + @Override + public boolean isStr() { + return false; + } + + + /** + * 泛型不同不能使用可变参数 + * 我想10个参数应该够了吧... + */ + @SuppressWarnings("unused") + public static class Func { + + public final SFunction[] accept(SFunction a) { + return new SFunction[]{a}; + } + + public final SFunction[] accept(SFunction a, SFunction b) { + return new SFunction[]{a, b}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c) { + return new SFunction[]{a, b, c}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d) { + return new SFunction[]{a, b, c, d}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e) { + return new SFunction[]{a, b, c, d, e}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f) { + return new SFunction[]{a, b, c, d, e, f}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f, SFunction g) { + return new SFunction[]{a, b, c, d, e, f, g}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f, SFunction g, SFunction h) { + return new SFunction[]{a, b, c, d, e, f, g, h}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f, SFunction g, SFunction h, SFunction i) { + return new SFunction[]{a, b, c, d, e, f, g, h, i}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f, SFunction g, SFunction h, SFunction i, SFunction j) { + return new SFunction[]{a, b, c, d, e, f, g, h, j}; + } + + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java index 703a1a5..ab1b084 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java @@ -2,6 +2,7 @@ package com.github.yulichang.wrapper.segments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import lombok.Getter; import org.apache.ibatis.type.TypeHandler; @@ -107,6 +108,11 @@ public class SelectLabel implements Select { return false; } + @Override + public SFunction[] getArgs() { + return null; + } + @Override public BaseFuncEnum getFunc() { return null; @@ -116,4 +122,9 @@ public class SelectLabel implements Select { public boolean isLabel() { return true; } + + @Override + public boolean isStr() { + return false; + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java index 038cf6c..ac1644e 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java @@ -1,6 +1,7 @@ package com.github.yulichang.wrapper.segments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import lombok.Getter; import org.apache.ibatis.type.TypeHandler; @@ -84,6 +85,11 @@ public class SelectNormal implements Select { return false; } + @Override + public SFunction[] getArgs() { + return null; + } + @Override public BaseFuncEnum getFunc() { return null; @@ -93,4 +99,9 @@ public class SelectNormal implements Select { public boolean isLabel() { return false; } + + @Override + public boolean isStr() { + return false; + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java new file mode 100644 index 0000000..99855ea --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java @@ -0,0 +1,105 @@ +package com.github.yulichang.wrapper.segments; + +import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; +import com.github.yulichang.wrapper.enums.BaseFuncEnum; +import org.apache.ibatis.type.TypeHandler; + +/** + * 自定义字符串列 + * + * @author yulichang + * @since 1.3.12 + */ +public class SelectString implements Select { + private final String column; + + public SelectString(String column) { + this.column = column; + } + + @Override + public Class getClazz() { + return null; + } + + @Override + public String getIndex() { + return null; + } + + @Override + public boolean isPk() { + return false; + } + + @Override + public String getColumn() { + return column; + } + + @Override + public Class getColumnType() { + return null; + } + + @Override + public String getTagColumn() { + return null; + } + + @Override + public String getColumProperty() { + return null; + } + + @Override + public boolean hasTypeHandle() { + return false; + } + + @Override + public TypeHandler getTypeHandle() { + return null; + } + + @Override + public boolean isHasAlias() { + return false; + } + + @Override + public String getAlias() { + return null; + } + + @Override + public TableFieldInfo getTableFieldInfo() { + return null; + } + + @Override + public boolean isFunc() { + return false; + } + + @Override + public SFunction[] getArgs() { + return null; + } + + @Override + public BaseFuncEnum getFunc() { + return null; + } + + @Override + public boolean isLabel() { + return false; + } + + @Override + public boolean isStr() { + return true; + } +} diff --git a/mybatis-plus-join-test/pom.xml b/mybatis-plus-join-test/pom.xml index 1e6a426..52d049e 100644 --- a/mybatis-plus-join-test/pom.xml +++ b/mybatis-plus-join-test/pom.xml @@ -7,11 +7,11 @@ com.github.yulichang mybatis-plus-join-root - 1.3.8 + 1.3.11 mybatis-plus-join-test pom - 1.3.8 + 1.3.11 mybatis-plus-join-test @@ -85,7 +85,7 @@ com.github.yulichang mybatis-plus-join-boot-starter - 1.3.8 + 1.3.11 @@ -110,4 +110,4 @@ - \ No newline at end of file + diff --git a/mybatis-plus-join-test/test-collection/pom.xml b/mybatis-plus-join-test/test-collection/pom.xml index 362db34..0c30c90 100644 --- a/mybatis-plus-join-test/test-collection/pom.xml +++ b/mybatis-plus-join-test/test-collection/pom.xml @@ -5,7 +5,7 @@ com.github.yulichang mybatis-plus-join-test - 1.3.8 + 1.3.11 4.0.0 @@ -17,4 +17,4 @@ UTF-8 - \ No newline at end of file + diff --git a/mybatis-plus-join-test/test-join/pom.xml b/mybatis-plus-join-test/test-join/pom.xml index b11b489..18901c7 100644 --- a/mybatis-plus-join-test/test-join/pom.xml +++ b/mybatis-plus-join-test/test-join/pom.xml @@ -5,7 +5,7 @@ com.github.yulichang mybatis-plus-join-test - 1.3.8 + 1.3.11 4.0.0 @@ -16,4 +16,4 @@ 8 UTF-8 - \ No newline at end of file + diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserDO.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserDO.java index 4f7f879..3ad17bd 100644 --- a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserDO.java +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/entity/UserDO.java @@ -45,6 +45,17 @@ public class UserDO { @TableLogic private Boolean del; + private Integer createBy; + + @TableField(exist = false) + private String createName; + + private Integer updateBy; + + @TableField(exist = false) + private String updateName; + + @TableField(exist = false) private String alias; diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/AddressMapper.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/AddressMapper.java index 246de25..fcb0786 100644 --- a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/AddressMapper.java +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/AddressMapper.java @@ -1,10 +1,9 @@ package com.github.yulichang.test.join.mapper; -import com.github.yulichang.base.MPJBaseMapper; import com.github.yulichang.test.join.entity.AddressDO; import org.apache.ibatis.annotations.Mapper; @Mapper @SuppressWarnings("unused") -public interface AddressMapper extends MPJBaseMapper { +public interface AddressMapper extends MyBaseMapper { } diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/AreaMapper.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/AreaMapper.java index 2eb72d7..163567d 100644 --- a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/AreaMapper.java +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/AreaMapper.java @@ -1,10 +1,9 @@ package com.github.yulichang.test.join.mapper; -import com.github.yulichang.base.MPJBaseMapper; import com.github.yulichang.test.join.entity.AreaDO; import org.apache.ibatis.annotations.Mapper; @Mapper @SuppressWarnings("unused") -public interface AreaMapper extends MPJBaseMapper { +public interface AreaMapper extends MyBaseMapper { } diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/MyBaseMapper.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/MyBaseMapper.java new file mode 100644 index 0000000..34cc230 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/MyBaseMapper.java @@ -0,0 +1,13 @@ +package com.github.yulichang.test.join.mapper; + +import com.github.yulichang.base.MPJBaseMapper; +import org.apache.ibatis.annotations.Mapper; + +import java.util.List; + +@Mapper +@SuppressWarnings("unused") +public interface MyBaseMapper extends MPJBaseMapper { + + int insertBatchSomeColumn(List entityList); +} diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserMapper.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserMapper.java index acbbdfa..bc22156 100644 --- a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserMapper.java +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/mapper/UserMapper.java @@ -1,10 +1,9 @@ package com.github.yulichang.test.join.mapper; -import com.github.yulichang.base.MPJBaseMapper; import com.github.yulichang.test.join.entity.UserDO; import org.apache.ibatis.annotations.Mapper; @Mapper -public interface UserMapper extends MPJBaseMapper { +public interface UserMapper extends MyBaseMapper { } diff --git a/mybatis-plus-join-test/test-join/src/main/resources/application.yml b/mybatis-plus-join-test/test-join/src/main/resources/application.yml index aeda078..e877eb1 100644 --- a/mybatis-plus-join-test/test-join/src/main/resources/application.yml +++ b/mybatis-plus-join-test/test-join/src/main/resources/application.yml @@ -23,4 +23,5 @@ mybatis-plus: # 打印 mybatis plus join banner mybatis-plus-join: banner: true - sub-table-logic: true \ No newline at end of file + sub-table-logic: true + ms-cache: true \ No newline at end of file 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 65cf279..a85eb47 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 @@ -29,53 +29,53 @@ INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北 DELETE FROM `user`; -INSERT INTO `user` (id, pid, `name`, `json`, `address_id`, sex, head_img, create_time, del) VALUES -( 1, 1, '张三 1', '{"id": 1,"name":"张三 1"}', 1, 1, 'https://url-01', '2022-01-01 12:00:00', false), -( 2, 1, '张三 2', '{"id": 2,"name":"张三 2"}', 1, 0, 'https://url-02', '2022-01-01 12:00:00', false), -( 3, 1, '张三 3', '{"id": 3,"name":"张三 3"}', 1, 0, 'https://url-03', '2022-01-01 12:00:00', false), -( 4, 1, '张三 4', '{"id": 4,"name":"张三 4"}', 1, 0, 'https://url-04', '2022-01-01 12:00:00', false), -( 5, 1, '张三 5', '{"id": 5,"name":"张三 5"}', 1, 0, 'https://url-05', '2022-01-01 12:00:00', false), -( 6, 1, '张三 6', '{"id": 6,"name":"张三 6"}', 1, 0, 'https://url-06', '2022-01-01 12:00:00', false), -( 7, 1, '张三 7', '{"id": 7,"name":"张三 7"}', 1, 0, 'https://url-07', '2022-01-01 12:00:00', false), -( 8, 1, '张三 8', '{"id": 8,"name":"张三 8"}', 1, 0, 'https://url-08', '2022-01-01 12:00:00', false), -( 9, 1, '张三 9', '{"id": 9,"name":"张三 9"}', 1, 0, 'https://url-09', '2022-01-01 12:00:00', false), -(10, 1, '张三10', '{"id":10,"name":"张三10"}', 1, 0, 'https://url-10', '2022-01-01 12:00:00', true ), -(11, 1, '张三11', '{"id":11,"name":"张三11"}', 1, 0, 'https://url-11', '2022-01-01 12:00:00', true ), -(12, 1, '张三12', '{"id":12,"name":"张三12"}', 1, 0, 'https://url-12', '2022-01-01 12:00:00', true ), -(13, 1, '张三13', '{"id":13,"name":"张三13"}', 1, 0, 'https://url-13', '2022-01-01 12:00:00', true ), -(14, 2, '张三14', '{"id":14,"name":"张三14"}', 1, 0, 'https://url-14', '2022-01-01 12:00:00', false), -(15, 2, '张三15', '{"id":15,"name":"张三15"}', 1, 0, 'https://url-15', '2022-01-01 12:00:00', false), -(16, 2, '张三16', '{"id":16,"name":"张三16"}', 1, 0, 'https://url-16', '2022-01-01 12:00:00', false), -(17, 2, '张三17', '{"id":17,"name":"张三17"}', 1, 0, 'https://url-17', '2022-01-01 12:00:00', false), -(18, 2, '张三18', '{"id":18,"name":"张三18"}', 1, 0, 'https://url-18', '2022-01-01 12:00:00', false), -(19, 2, '张三19', '{"id":19,"name":"张三19"}', 1, 0, 'https://url-19', '2022-01-01 12:00:00', true ), -(20, 2, '张三20', '{"id":20,"name":"张三20"}', 1, 0, 'https://url-20', '2022-01-01 12:00:00', true ), -(21, 2, '张三21', '{"id":21,"name":"张三21"}', 1, 0, 'https://url-21', '2022-01-01 12:00:00', true ), -(22, 2, '张三22', '{"id":22,"name":"张三22"}', 1, 0, 'https://url-22', '2022-01-01 12:00:00', true ); +INSERT INTO `user` (id, pid, `name`, `json`, `address_id`, sex, head_img, create_time, create_by, update_by, del) VALUES +( 1, 1, '张三 1', '{"id": 1,"name":"张三 1"}', 1, 1, 'https://url-01', '2022-01-01 12:00:00', 1, 2, false), +( 2, 1, '张三 2', '{"id": 2,"name":"张三 2"}', 1, 0, 'https://url-02', '2022-01-01 12:00:00', 2, 3, false), +( 3, 1, '张三 3', '{"id": 3,"name":"张三 3"}', 1, 0, 'https://url-03', '2022-01-01 12:00:00', 3, 2, false), +( 4, 1, '张三 4', '{"id": 4,"name":"张三 4"}', 1, 0, 'https://url-04', '2022-01-01 12:00:00', 9, 2, false), +( 5, 1, '张三 5', '{"id": 5,"name":"张三 5"}', 1, 0, 'https://url-05', '2022-01-01 12:00:00', 1, 2, false), +( 6, 1, '张三 6', '{"id": 6,"name":"张三 6"}', 1, 0, 'https://url-06', '2022-01-01 12:00:00', 1, 2, false), +( 7, 1, '张三 7', '{"id": 7,"name":"张三 7"}', 1, 0, 'https://url-07', '2022-01-01 12:00:00', 1, 2, false), +( 8, 1, '张三 8', '{"id": 8,"name":"张三 8"}', 1, 0, 'https://url-08', '2022-01-01 12:00:00', 1, 2, false), +( 9, 1, '张三 9', '{"id": 9,"name":"张三 9"}', 1, 0, 'https://url-09', '2022-01-01 12:00:00', 1, 2, false), +(10, 1, '张三10', '{"id":10,"name":"张三10"}', 1, 0, 'https://url-10', '2022-01-01 12:00:00', 1, 2, true ), +(11, 1, '张三11', '{"id":11,"name":"张三11"}', 1, 0, 'https://url-11', '2022-01-01 12:00:00', 1, 2, true ), +(12, 1, '张三12', '{"id":12,"name":"张三12"}', 1, 0, 'https://url-12', '2022-01-01 12:00:00', 1, 2, true ), +(13, 1, '张三13', '{"id":13,"name":"张三13"}', 1, 0, 'https://url-13', '2022-01-01 12:00:00', 1, 2, true ), +(14, 2, '张三14', '{"id":14,"name":"张三14"}', 1, 0, 'https://url-14', '2022-01-01 12:00:00', 1, 2, false), +(15, 2, '张三15', '{"id":15,"name":"张三15"}', 1, 0, 'https://url-15', '2022-01-01 12:00:00', 1, 2, false), +(16, 2, '张三16', '{"id":16,"name":"张三16"}', 1, 0, 'https://url-16', '2022-01-01 12:00:00', 1, 2, false), +(17, 2, '张三17', '{"id":17,"name":"张三17"}', 1, 0, 'https://url-17', '2022-01-01 12:00:00', 1, 2, false), +(18, 2, '张三18', '{"id":18,"name":"张三18"}', 1, 0, 'https://url-18', '2022-01-01 12:00:00', 1, 2, false), +(19, 2, '张三19', '{"id":19,"name":"张三19"}', 1, 0, 'https://url-19', '2022-01-01 12:00:00', 1, 2, true ), +(20, 2, '张三20', '{"id":20,"name":"张三20"}', 1, 0, 'https://url-20', '2022-01-01 12:00:00', 1, 2, true ), +(21, 2, '张三21', '{"id":21,"name":"张三21"}', 1, 0, 'https://url-21', '2022-01-01 12:00:00', 1, 2, true ), +(22, 2, '张三22', '{"id":22,"name":"张三22"}', 1, 0, 'https://url-22', '2022-01-01 12:00:00', 1, 2, true ); DELETE FROM address; -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 1, 1, 10001, '10000000001', '曹县01', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 2, 1, 10002, '10000000002', '曹县02', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 3, 1, 10003, '10000000003', '曹县03', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 4, 1, 10004, '10000000004', '曹县04', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 5, 1, 10005, '10000000005', '曹县05', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 6, 1, 10006, '10000000006', '曹县06', true ); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 7, 1, 10007, '10000000007', '曹县07', true ); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 8, 1, 10008, '10000000008', '曹县08', true ); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 9, 1, 10009, '10000000009', '曹县09', true ); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (10,10, 10010, '10000000010', '曹县10', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (11,11, 10011, '10000000011', '曹县11', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (12,12, 10012, '10000000012', '曹县12', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (13,13, 10013, '10000000013', '曹县13', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (14,14, 10014, '10000000014', '曹县14', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (15,15, 10015, '10000000015', '曹县15', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (16,16, 10016, '10000000016', '曹县16', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (17,17, 10017, '10000000017', '曹县17', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (18,18, 10018, '10000000018', '曹县18', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (19,19, 10019, '10000000019', '曹县19', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (20,20, 10020, '10000000020', '曹县20', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (21,21, 10021, '10000000021', '曹县21', false); -INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (22,22, 10022, '10000000022', '曹县22', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 1, 1, 10001, '10000000001', '朝阳01', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 2, 1, 10002, '10000000002', '朝阳02', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 3, 1, 10003, '10000000003', '朝阳03', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 4, 1, 10004, '10000000004', '朝阳04', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 5, 1, 10005, '10000000005', '朝阳05', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 6, 1, 10006, '10000000006', '朝阳06', true ); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 7, 1, 10007, '10000000007', '朝阳07', true ); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 8, 1, 10008, '10000000008', '朝阳08', true ); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES ( 9, 1, 10009, '10000000009', '朝阳09', true ); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (10,10, 10010, '10000000010', '朝阳10', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (11,11, 10011, '10000000011', '朝阳11', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (12,12, 10012, '10000000012', '朝阳12', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (13,13, 10013, '10000000013', '朝阳13', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (14,14, 10014, '10000000014', '朝阳14', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (15,15, 10015, '10000000015', '朝阳15', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (16,16, 10016, '10000000016', '朝阳16', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (17,17, 10017, '10000000017', '朝阳17', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (18,18, 10018, '10000000018', '朝阳18', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (19,19, 10019, '10000000019', '朝阳19', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (20,20, 10020, '10000000020', '朝阳20', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (21,21, 10021, '10000000021', '朝阳21', false); +INSERT INTO address (id, user_id, area_id, tel, address, del) VALUES (22,22, 10022, '10000000022', '朝阳22', false); 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 0a81af6..e14e58f 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 @@ -28,6 +28,8 @@ create table `user` sex tinyint not null, head_img varchar(255) not null, create_time datetime not null, + create_by int not null, + update_by int not null, del bit ); 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 a2d6738..c9f1bf0 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 @@ -1,5 +1,6 @@ package com.github.yulichang.test.join; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.github.yulichang.test.join.dto.AddressDTO; @@ -112,14 +113,30 @@ class LambdaWrapperTest { */ @Test void testInner() { - MPJLambdaWrapper wrapper = new MPJLambdaWrapper() +// //自连接 +// MPJLambdaWrapper wrapper = new MPJLambdaWrapper() // .disableSubLogicDel()//关闭副表逻辑删除 - .disableLogicDel()//关闭主表逻辑删除 +//// .disableLogicDel()//关闭主表逻辑删除 +// .selectAll(UserDO.class) +// .selectCollection(UserDO.class, UserDO::getChildren) +// .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId); +// List list = userMapper.selectJoinList(UserDO.class, wrapper); +//// assert list.size() == 2 && list.get(0).getChildren().size() == 9; +// System.out.println(list); + + //关联一张表多次 + MPJLambdaWrapper w = new MPJLambdaWrapper() + .disableLogicDel() + .disableSubLogicDel() .selectAll(UserDO.class) - .selectCollection(UserDO.class, UserDO::getChildren) - .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId); - List list = userMapper.selectJoinList(UserDO.class, wrapper); - System.out.println(list); + .leftJoin(UserDO.class, UserDO::getId, UserDO::getCreateBy, ext -> ext.selectAs(UserDO::getName, UserDO::getCreateName)) + .leftJoin(UserDO.class, (on, ext) -> { + on.eq(UserDO::getId, UserDO::getUpdateBy); + ext.selectAs(UserDO::getName, UserDO::getUpdateName); + }) + .eq(UserDO::getId, UserDO::getId); + List dos = userMapper.selectJoinList(UserDO.class, w); + assert dos.get(0).getCreateName() != null && dos.get(0).getUpdateName() != null; MPJLambdaWrapper wrapper1 = new MPJLambdaWrapper() .disableSubLogicDel() @@ -133,6 +150,7 @@ class LambdaWrapperTest { .le(UserDO::getId, 4); List list1 = userMapper.selectJoinList(UserDO.class, wrapper1); System.out.println(wrapper1.getSqlSegment()); + System.out.println(wrapper1.getSqlSelect()); assert "(t1.id <= #{ew.paramNameValuePairs.MPGENVAL1} AND t.id <= #{ew.paramNameValuePairs.MPGENVAL2})".equals(wrapper1.getSqlSegment()); System.out.println(list1); } @@ -143,20 +161,20 @@ class LambdaWrapperTest { @Test void testLogicDel() { List l1 = userMapper.selectJoinList(UserDTO.class, new MPJLambdaWrapper<>()); - assert l1.size() < 15; + assert l1.size() == 14; List l2 = userMapper.selectJoinList(UserDTO.class, new MPJLambdaWrapper() .selectAll(UserDO.class) .select(AddressDO::getAddress) .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); - assert l2.size() < 11; + assert l2.size() == 10; List l3 = userMapper.selectJoinList(UserDTO.class, new MPJLambdaWrapper() .disableSubLogicDel() .selectAll(UserDO.class) .selectCollection(AddressDO.class, UserDTO::getAddressList) .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); - assert l3.size() > 5 && l3.get(0).getAddressList().size() > 5; + assert l3.size() == 14 && l3.get(0).getAddressList().size() == 9; List l4 = userMapper.selectJoinList(UserDTO.class, new MPJLambdaWrapper() .disableSubLogicDel() @@ -165,7 +183,7 @@ class LambdaWrapperTest { .leftJoin(AddressDO.class, on -> on .eq(AddressDO::getUserId, UserDO::getId) .eq(AddressDO::getDel, false))); - assert l4.size() > 5 && l4.get(0).getAddressList().size() <= 5; + assert l4.size() == 14 && l4.get(0).getAddressList().size() == 5; } @@ -264,4 +282,27 @@ class LambdaWrapperTest { assert list.get(0).get("ADDRESS") != null; list.forEach(System.out::println); } + + /** + * 原生查询 + */ + @Test + void testMP() { + List dos = userMapper.selectList(new LambdaQueryWrapper() + .gt(UserDO::getId, 3) + .lt(UserDO::getId, 8)); + assert dos.size() == 4; + } + + /** + * 函数测试 + */ + @Test + void testFunc() { + MPJLambdaWrapper wrapper = new MPJLambdaWrapper() + .selectAll(UserDO.class); + + List dos = userMapper.selectJoinList(UserDO.class, wrapper); + System.out.println(1); + } } diff --git a/mybatis-plus-join-test/test-mapping/pom.xml b/mybatis-plus-join-test/test-mapping/pom.xml index 2db057c..5e3e4c7 100644 --- a/mybatis-plus-join-test/test-mapping/pom.xml +++ b/mybatis-plus-join-test/test-mapping/pom.xml @@ -5,7 +5,7 @@ mybatis-plus-join-test com.github.yulichang - 1.3.8 + 1.3.11 4.0.0 @@ -17,4 +17,4 @@ UTF-8 - \ No newline at end of file + diff --git a/mybatis-plus-join/pom.xml b/mybatis-plus-join/pom.xml index 1292578..fc51748 100644 --- a/mybatis-plus-join/pom.xml +++ b/mybatis-plus-join/pom.xml @@ -10,13 +10,13 @@ com.github.yulichang mybatis-plus-join-boot-starter - 1.3.8 + 1.3.11 --> 4.0.0 com.github.yulichang - 1.3.8 mybatis-plus-join + 1.3.11 mybatis-plus-join An enhanced toolkit of Mybatis-Plus to simplify development. @@ -53,7 +53,7 @@ com.github.yulichang mybatis-plus-join-boot-starter - 1.3.8 + 1.3.11 - \ No newline at end of file + diff --git a/pom.xml b/pom.xml index 636323a..fc4148e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ com.github.yulichang mybatis-plus-join-root pom - 1.3.8 + 1.3.11 mybatis-plus-join-root An enhanced toolkit of Mybatis-Plus to simplify development. @@ -45,7 +45,4 @@ UTF-8 - - -