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..514e4e3 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,7 @@ public class MybatisPlusJoinAutoConfiguration { public MybatisPlusJoinAutoConfiguration(MybatisPlusJoinProperties properties) { this.properties = properties; ConfigProperties.subTableLogic = properties.getSubTableLogic(); + ConfigProperties.msCache = properties.isMsCache(); } /** 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..ac0dc62 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 @@ -24,4 +24,9 @@ public class MybatisPlusJoinProperties { * 连表查询副表是否启用逻辑删除(前提是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..62d2698 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,12 @@ "defaultValue": true, "type": "java.lang.Boolean", "description": "连表查询副表是否启用逻辑删除(前提是MP配置了逻辑删除)." + }, + { + "name": "mybatis-plus-join.ms-cache", + "defaultValue": true, + "type": "java.lang.Boolean", + "description": "MappedStatement缓存开关." } ] } \ No newline at end of file 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..1230570 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,5 @@ package com.github.yulichang.config; */ public class ConfigProperties { public static boolean subTableLogic = true; + public static boolean msCache = true; } 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 49040ce..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,6 +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.config.ConfigProperties; import com.github.yulichang.mapper.MPJTableMapperHelper; import com.github.yulichang.method.MPJResultType; import com.github.yulichang.query.MPJQueryWrapper; @@ -92,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/wrapper/MPJAbstractLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java index 128b92c..c050c7d 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 @@ -32,19 +32,19 @@ public abstract class MPJAbstractLambdaWrapper 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) { + return columnToString(index, node, (SFunction) column, isJoin); } @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, X... columns) { + return Arrays.stream(columns).map(i -> columnToString(index, node, (SFunction) i, isJoin)).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 entityClass = LambdaUtils.getEntityClass(column); - return Constant.TABLE_ALIAS + getDefault(index, entityClass, isJoin) + StringPool.DOT + + return Constant.TABLE_ALIAS + getDefault(index, node, entityClass, isJoin) + StringPool.DOT + getCache(column).getTagColumn(); } @@ -54,9 +54,9 @@ public abstract class MPJAbstractLambdaWrapper clazz, boolean isJoin) { + protected String getDefault(String index, int node, Class clazz, boolean isJoin) { if (Objects.isNull(index)) { - if (Objects.equals(clazz, getEntityClass())) { + if (!isJoin && Objects.equals(clazz, getEntityClass())) { return StringPool.EMPTY; } //正序 @@ -66,12 +66,18 @@ public abstract class MPJAbstractLambdaWrapper> extends Wrapper implements Compare, Nested, Join, Func, OnCompare { + protected static final Node ROOT_NODE = new Node(null, 0, null); + /** * 占位符 */ @@ -85,6 +89,11 @@ public abstract class MPJAbstractWrapper joinClass; + /** + * 寻路 + */ + protected Node node; + @Override public T getEntity() { return entity; @@ -318,7 +327,7 @@ public abstract class MPJAbstractWrapper 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, columns)); final String finalOne = one; appendSqlSegments(GROUP_BY, () -> finalOne); } @@ -328,9 +337,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); if (ArrayUtils.isNotEmpty(columns)) { - one += (StringPool.COMMA + columnsToString(index, false, columns)); + one += (StringPool.COMMA + columnsToString(index, getByClass(node, joinClass), false, columns)); } final String finalOne = one; appendSqlSegments(GROUP_BY, () -> finalOne); @@ -431,6 +440,7 @@ public abstract class MPJAbstractWrapper Children addCondition(boolean condition, SFunction column, SqlKeyword sqlKeyword, SFunction val) { + Node nnnn = this.node; return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(index, column, false), sqlKeyword, columnToSqlSegment(index, val, true))); } @@ -541,6 +551,7 @@ 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); } /** * 获取 columnName */ - protected String columnToString(String index, X column, boolean isJoin) { + protected String columnToString(String index, int node, X column, boolean isJoin) { return (String) column; } @@ -630,8 +641,8 @@ public abstract class MPJAbstractWrapper 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, X... columns) { + return Arrays.stream(columns).map(i -> this.columnToString(index, node, i, isJoin)).collect(joining(StringPool.COMMA)); } @Override @@ -680,4 +691,38 @@ public abstract class MPJAbstractWrapper Children le(boolean condition, SFunction column, SFunction val) { return addCondition(condition, column, LE, val); } + + + @Data + @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 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); + } } 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 b02bad9..6e203a0 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 @@ -17,7 +17,7 @@ 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.interfaces.WrapperBiConsumer; import com.github.yulichang.wrapper.resultmap.MybatisLabel; import com.github.yulichang.wrapper.segments.Select; import com.github.yulichang.wrapper.segments.SelectCache; @@ -92,12 +92,20 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapperlambdaQuery() + * 推荐使用 带 class 的构造方法 */ public MPJLambdaWrapper() { super.initNeed(); } + /** + * 推荐使用此构造方法 + */ + public MPJLambdaWrapper(Class clazz) { + super.initNeed(); + setEntityClass(clazz); + } + /** * 不建议直接 new 该实例,使用 MPJWrappers.lambdaQuery() @@ -105,7 +113,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 +127,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 @@ -293,17 +302,19 @@ 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); + public MPJLambdaWrapper join(String keyWord, Class clazz, WrapperBiConsumer consumer) { + Node nnnnn = this.node; + 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; + tableList.add(clazz, newIndex); + onWrappers.add(instance); tableIndex++; - if (Objects.nonNull(ext)) { - this.index = name; - MPJLambdaWrapper wrapper = ext.apply(typedThis); - wrapper.index = null; - } + this.index = newIndex; + consumer.accept(instance, typedThis); + this.index = oldIndex; return typedThis; } } 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/QueryJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java index 9b6a0b9..d5aee0f 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 @@ -4,7 +4,6 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.interfaces.MPJBaseJoin; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; -import com.github.yulichang.wrapper.interfaces.on.WrapperFunction; /** * @author yulichang @@ -20,7 +19,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)); + return join(Constant.LEFT_JOIN, clazz, left, right); } /** @@ -30,7 +29,7 @@ public interface QueryJoin extends MPJBaseJoin { * @param right 条件 */ default Children leftJoin(SFunction left, SFunction right) { - return leftJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right)); + return join(Constant.LEFT_JOIN, left, right); } /** @@ -42,7 +41,7 @@ public interface QueryJoin extends MPJBaseJoin { * @param function 条件 */ default Children leftJoin(Class clazz, WrapperFunction function) { - return join(Constant.LEFT_JOIN, clazz, function, null); + return join(Constant.LEFT_JOIN, clazz, function); } /** @@ -53,7 +52,7 @@ public interface QueryJoin extends MPJBaseJoin { * @param right 条件 */ default Children leftJoin(Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return leftJoin(clazz, on -> on.eq(left, right), ext); + return join(Constant.LEFT_JOIN, clazz, left, right, ext); } /** @@ -63,7 +62,7 @@ public interface QueryJoin extends MPJBaseJoin { * @param right 条件 */ default Children leftJoin(SFunction left, SFunction right, WrapperFunction ext) { - return leftJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + return join(Constant.LEFT_JOIN, left, right, ext); } /** @@ -72,94 +71,95 @@ public interface QueryJoin extends MPJBaseJoin { * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) * * @param clazz 关联实体类 - * @param function 条件 + * @param consumer 条件 */ - default Children leftJoin(Class clazz, WrapperFunction function, WrapperFunction ext) { - return join(Constant.LEFT_JOIN, clazz, function, ext); + default Children leftJoin(Class clazz, WrapperBiConsumer consumer) { + return join(Constant.LEFT_JOIN, clazz, 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)); + return join(Constant.RIGHT_JOIN, left, right); } /** * ignore 参考 left join */ default Children rightJoin(Class clazz, WrapperFunction function) { - return join(Constant.RIGHT_JOIN, clazz, function, null); + return join(Constant.RIGHT_JOIN, clazz, function); } /** * ignore 参考 left join */ default Children rightJoin(Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return rightJoin(clazz, on -> on.eq(left, right), ext); + return join(Constant.RIGHT_JOIN, clazz, left, right, ext); } /** * ignore 参考 left join */ default Children rightJoin(SFunction left, SFunction right, WrapperFunction ext) { - return rightJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + return join(Constant.RIGHT_JOIN, left, right, ext); } /** * 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, WrapperBiConsumer consumer) { + return join(Constant.RIGHT_JOIN, clazz, 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)); + return join(Constant.INNER_JOIN, LambdaUtils.getEntityClass(left), on -> on.eq(left, right)); } /** * ignore 参考 left join */ default Children innerJoin(Class clazz, WrapperFunction function) { - return join(Constant.INNER_JOIN, clazz, function, null); + return join(Constant.INNER_JOIN, clazz, function); } /** * ignore 参考 left join */ default Children innerJoin(Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return innerJoin(clazz, on -> on.eq(left, right), ext); + return join(Constant.INNER_JOIN, clazz, left, right, ext); } /** * ignore 参考 left join */ default Children innerJoin(SFunction left, SFunction right, WrapperFunction ext) { - return innerJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + return join(Constant.INNER_JOIN, left, right, ext); } /** * 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, WrapperBiConsumer consumer) { + return join(Constant.INNER_JOIN, clazz, consumer); } @@ -167,42 +167,42 @@ public interface QueryJoin extends MPJBaseJoin { * 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)); + return join(Constant.FULL_JOIN, left, right); } /** * ignore 参考 left join */ default Children fullJoin(Class clazz, WrapperFunction function) { - return join(Constant.FULL_JOIN, clazz, function, null); + return join(Constant.FULL_JOIN, clazz, function); } /** * ignore 参考 left join */ default Children fullJoin(Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return fullJoin(clazz, on -> on.eq(left, right), ext); + return join(Constant.FULL_JOIN, clazz, left, right, ext); } /** * ignore 参考 left join */ default Children fullJoin(SFunction left, SFunction right, WrapperFunction ext) { - return fullJoin(LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + return join(Constant.FULL_JOIN, left, right, ext); } /** * 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, WrapperBiConsumer consumer) { + return join(Constant.FULL_JOIN, clazz, consumer); } /** @@ -239,7 +239,7 @@ public interface QueryJoin extends MPJBaseJoin { * @param function 条件 */ default Children join(String keyWord, Class clazz, WrapperFunction function) { - return join(keyWord, clazz, function, null); + return join(keyWord, clazz, (on, e) -> function.apply(on)); } /** @@ -250,7 +250,10 @@ public interface QueryJoin extends MPJBaseJoin { * @param right 条件 */ default Children join(String keyWord, Class clazz, SFunction left, SFunction right, WrapperFunction ext) { - return join(keyWord, clazz, on -> on.eq(left, right), ext); + return join(keyWord, clazz, (on, e) -> { + on.eq(left, right); + ext.apply(e); + }); } /** @@ -260,11 +263,11 @@ public interface QueryJoin extends MPJBaseJoin { * @param right 条件 */ default Children join(String keyWord, SFunction left, SFunction right, WrapperFunction ext) { - return join(keyWord, LambdaUtils.getEntityClass(left), on -> on.eq(left, right), ext); + return join(keyWord, LambdaUtils.getEntityClass(left), left, right, ext); } /** * 内部使用, 不建议直接调用 */ - Children join(String keyWord, Class clazz, WrapperFunction function, WrapperFunction ext); + Children join(String keyWord, Class clazz, WrapperBiConsumer consumer); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/WrapperBiConsumer.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/WrapperBiConsumer.java new file mode 100644 index 0000000..fba0b98 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/WrapperBiConsumer.java @@ -0,0 +1,9 @@ +package com.github.yulichang.wrapper.interfaces; + +import com.github.yulichang.wrapper.MPJLambdaWrapper; + +@FunctionalInterface +public interface WrapperBiConsumer { + + void accept(MPJLambdaWrapper on, MPJLambdaWrapper ext); +} 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/WrapperFunction.java similarity index 82% rename from mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/WrapperFunction.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/WrapperFunction.java index 0679d71..19a7f5a 100644 --- 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/WrapperFunction.java @@ -1,4 +1,4 @@ -package com.github.yulichang.wrapper.interfaces.on; +package com.github.yulichang.wrapper.interfaces; import com.github.yulichang.wrapper.MPJLambdaWrapper; 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/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 6656290..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 @@ -113,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() @@ -134,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); } @@ -267,7 +284,7 @@ class LambdaWrapperTest { } /** - * 关联查询返回map + * 原生查询 */ @Test void testMP() { @@ -276,4 +293,16 @@ class LambdaWrapperTest { .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/pom.xml b/pom.xml index d8e6114..fc4148e 100644 --- a/pom.xml +++ b/pom.xml @@ -33,6 +33,7 @@ mybatis-plus-join-boot-starter mybatis-plus-join-core mybatis-plus-join-annotation + mybatis-plus-join-test