From f4d82318bcd3aa72f07e3b88d0b0dda11e305e39 Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Wed, 30 Nov 2022 13:49:08 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yulichang/config/MappingConfig.java | 2 +- .../yulichang/interceptor/MPJInterceptor.java | 42 +++++++-- .../mapper/MPJTableMapperHelper.java | 12 ++- .../yulichang/method/MPJBaseMethod.java | 6 +- .../yulichang/query/MPJQueryWrapper.java | 30 ++++++- .../wrapper/MPJAbstractLambdaWrapper.java | 38 ++++++-- .../yulichang/wrapper/MPJAbstractWrapper.java | 87 +++++++++++-------- .../yulichang/wrapper/MPJLambdaWrapper.java | 74 ++++++++++++---- .../wrapper/interfaces/LambdaJoin.java | 12 +-- .../wrapper/interfaces/on/OnFunction.java | 4 +- .../wrapper/resultmap/MybatisLabel.java | 3 + mybatis-plus-join-test/README.md | 5 ++ .../yulichang/test/collection/enums/Sex.java | 24 ----- .../yulichang/test/join/dto/TableDTO.java | 61 ------------- .../yulichang/test/join/dto/UserDTO.java | 5 +- .../yulichang/test/join/entity/UserDO.java | 10 ++- .../test-join/src/main/resources/db/data.sql | 44 +++++----- .../src/main/resources/db/schema.sql | 1 + .../test/join/LambdaWrapperTest.java | 46 ++++++++++ 19 files changed, 311 insertions(+), 195 deletions(-) create mode 100644 mybatis-plus-join-test/README.md delete mode 100644 mybatis-plus-join-test/test-collection/src/main/java/com/github/yulichang/test/collection/enums/Sex.java delete mode 100644 mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/TableDTO.java diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MappingConfig.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MappingConfig.java index 3d8d63e..64d31fb 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MappingConfig.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MappingConfig.java @@ -14,7 +14,7 @@ public class MappingConfig { public MappingConfig() { TableInfoHelper.getTableInfos().forEach(i -> - MPJTableInfoHelper.initTableInfo(i.getEntityType(), MPJTableMapperHelper.get(i.getEntityType()))); + MPJTableInfoHelper.initTableInfo(i.getEntityType(), MPJTableMapperHelper.getMapper(i.getEntityType()))); } } 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 8a35340..0f3aba6 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 @@ -5,7 +5,9 @@ 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.mapper.MPJTableMapperHelper; import com.github.yulichang.method.MPJResultType; +import com.github.yulichang.query.MPJQueryWrapper; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.MPJReflectionKit; import com.github.yulichang.toolkit.support.SelectColumn; @@ -93,14 +95,26 @@ public class MPJInterceptor implements Interceptor { /** * 获取MappedStatement */ + @SuppressWarnings("rawtypes") public MappedStatement getMappedStatement(MappedStatement ms, Class resultType, Object ew) { String id = ms.getId() + StringPool.UNDERSCORE + resultType.getName(); - - if (ew instanceof MPJLambdaWrapper && ((MPJLambdaWrapper) ew).isResultMap()) { + if (ew instanceof MPJLambdaWrapper) { + MPJLambdaWrapper wrapper = (MPJLambdaWrapper) ew; + wrapper.setEntityClass(MPJTableMapperHelper.getEntity(getEntity(ms.getId()))); + //TODO 重构缓存 -> 根据sql缓存 //不走缓存 - return buildMappedStatement(ms, resultType, ew, id); } - //走缓存 + if (ew instanceof MPJQueryWrapper) { + MPJQueryWrapper wrapper = (MPJQueryWrapper) ew; + return getCache(ms, id + wrapper.getSqlSelect(), resultType, ew); + } + return buildMappedStatement(ms, resultType, ew, id); + } + + /** + * 走缓存 + */ + private MappedStatement getCache(MappedStatement ms, String id, Class resultType, Object ew) { Map statementMap = MS_CACHE.get(id); if (CollectionUtils.isNotEmpty(statementMap)) { MappedStatement statement = statementMap.get(ms.getConfiguration()); @@ -238,7 +252,7 @@ public class MPJInterceptor implements Interceptor { for (Result r : resultList) { String columnName = r.getColumn(); //列名去重 - columnName = getColumn(columnSet, columnName); + columnName = getColumn(columnSet, StringUtils.getTargetColumn(columnName)); columnList.add(SelectColumn.of(mybatisLabel.getEntityClass(), r.getColumn(), null, Objects.equals(columnName, r.getColumn()) ? null : columnName, null, null, true, null)); ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), r.getProperty(), @@ -320,7 +334,7 @@ public class MPJInterceptor implements Interceptor { pool.add(columnName); return columnName; } - columnName = "join_" + StringUtils.getTargetColumn(columnName); + columnName = "join_" + columnName; return getColumn(pool, columnName); } @@ -339,8 +353,10 @@ public class MPJInterceptor implements Interceptor { List resultMappingList = new ArrayList<>(resultMappings); //复制不存在的resultMapping for (Field i : fieldList) { - resultMappingList.add(new ResultMapping.Builder(ms.getConfiguration(), - i.getName(), i.getName(), i.getType()).build()); + if (MPJReflectionKit.isPrimitiveOrWrapper(i.getType())) { + resultMappingList.add(new ResultMapping.Builder(ms.getConfiguration(), + i.getName(), i.getName(), i.getType()).build()); + } } return new ResultMap.Builder(ms.getConfiguration(), id, resultType, resultMappingList).build(); } @@ -360,9 +376,17 @@ public class MPJInterceptor implements Interceptor { /** * 往 Configuration 添加resultMap */ - public synchronized static void addResultMap(MappedStatement ms, String key, ResultMap resultMap) { + private synchronized static void addResultMap(MappedStatement ms, String key, ResultMap resultMap) { if (!ms.getConfiguration().getResultMapNames().contains(key)) { ms.getConfiguration().addResultMap(resultMap); } } + + private Class getEntity(String id) { + try { + return Class.forName(id.substring(0, id.lastIndexOf(StringPool.DOT))); + } catch (ClassNotFoundException e) { + return null; + } + } } 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 b4f1541..82a0e63 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 @@ -12,14 +12,24 @@ import java.util.concurrent.ConcurrentHashMap; public class MPJTableMapperHelper { private static final Map, Class> CACHE = new ConcurrentHashMap<>(); + private static final Map, Class> CACHE_REVERSE = new ConcurrentHashMap<>(); + public static void init(Class clazz, Class mapper) { if (clazz != null) { CACHE.put(clazz, mapper); } + if (mapper != null) { + CACHE_REVERSE.put(mapper, clazz); + } } - public static Class get(Class clazz) { + public static Class getMapper(Class clazz) { return CACHE.get(clazz); } + + public static Class getEntity(Class clazz) { + return CACHE_REVERSE.get(clazz); + } + } 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 60bcad0..07ce1fd 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 @@ -26,9 +26,9 @@ public interface MPJBaseMethod extends Constants { String sqlScript = getAllSqlWhere(table, true, true, WRAPPER_ENTITY_DOT); sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true); sqlScript += NEWLINE; - sqlScript += (NEWLINE + getLogicDeleteSql(table, true, true) + NEWLINE); + sqlScript += SqlScriptUtils.convertIf(getLogicDeleteSql(table, true, true), String.format("%s.logicSql", WRAPPER), true); if (ConfigProperties.subTableLogic) { - sqlScript += (String.format("${%s.logicSql}", WRAPPER)); + sqlScript += (NEWLINE + String.format("${%s.subLogicSql}", WRAPPER)); } String normalSqlScript = SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_NONEMPTYOFNORMAL), true); normalSqlScript += NEWLINE; @@ -42,7 +42,7 @@ public interface MPJBaseMethod extends Constants { sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true); sqlScript += NEWLINE; if (ConfigProperties.subTableLogic) { - sqlScript += (String.format("${%s.logicSql}", WRAPPER) + NEWLINE); + sqlScript += (String.format("${%s.subLogicSql}", WRAPPER) + NEWLINE); } sqlScript += SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_NONEMPTYOFWHERE), true); sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java index 48df15a..e47eb17 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java @@ -8,6 +8,7 @@ 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.*; +import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.query.interfaces.MPJJoin; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.MPJWrappers; @@ -60,6 +61,10 @@ public class MPJQueryWrapper extends AbstractWrapper extends AbstractWrapper disableLogicDel() { + this.logicSql = false; + return typedThis; + } + + /** + * 启用主表逻辑删除 + */ + public MPJQueryWrapper enableLogicDel() { + this.logicSql = true; + return typedThis; + } + + /** + * 逻辑删除 + */ + public boolean getLogicSql() { + return logicSql; + } + /** * 返回一个支持 lambda 函数写法的 wrapper */ 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 b82eeae..e50deec 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 @@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.support.ColumnCache; +import lombok.Getter; import java.util.Arrays; import java.util.HashMap; @@ -32,18 +33,19 @@ public abstract class MPJAbstractLambdaWrapper, Map> columnMap = new HashMap<>(); @Override - protected String columnToString(X column) { - return columnToString((SFunction) column); + protected String columnToString(X column, boolean isJoin) { + return columnToString((SFunction) column, isJoin); } @Override @SafeVarargs - protected final String columnsToString(X... columns) { - return Arrays.stream(columns).map(i -> columnToString((SFunction) i)).collect(joining(StringPool.COMMA)); + protected final String columnsToString(boolean isJoin, X... columns) { + return Arrays.stream(columns).map(i -> columnToString((SFunction) i, isJoin)).collect(joining(StringPool.COMMA)); } - protected String columnToString(SFunction column) { - return Constant.TABLE_ALIAS + getDefault(subTable.get(LambdaUtils.getEntityClass(column))) + StringPool.DOT + + protected String columnToString(SFunction column, boolean isJoin) { + Class entityClass = LambdaUtils.getEntityClass(column); + return Constant.TABLE_ALIAS + getDefault(entityClass, isJoin) + StringPool.DOT + getCache(column).getColumn(); } @@ -57,9 +59,27 @@ public abstract class MPJAbstractLambdaWrapper clazz, boolean isJoin) { + Integer index = subTable.get(clazz); + if (Objects.nonNull(index)) { + if (getEntityClass() == null) { + return index.toString(); + } + if (isJoin && joinClass == getEntityClass()) { + return StringPool.EMPTY; + } + return index.toString(); + } + return StringPool.EMPTY; + } + + protected String getDefaultSelect(Class clazz, boolean myself) { + Integer index = subTable.get(clazz); + if (Objects.nonNull(index)) { + if (myself) { + return StringPool.EMPTY; + } + return index.toString(); } return StringPool.EMPTY; } 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 8f817d6..9ba6e58 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 @@ -16,6 +16,7 @@ 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 lombok.Getter; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -59,6 +60,10 @@ public abstract class MPJAbstractWrapper> onWrappers = new ArrayList<>(); /** * ß * 数据库表映射实体类 @@ -69,6 +74,11 @@ public abstract class MPJAbstractWrapper entityClass; + /** + * 连表实体类 on 条件 func 使用 + */ + @Getter + protected Class joinClass; @Override public T getEntity() { @@ -89,6 +99,7 @@ public abstract class MPJAbstractWrapper entityClass) { if (entityClass != null) { + onWrappers.forEach(i -> i.setEntityClass(entityClass)); this.entityClass = entityClass; } return typedThis; @@ -180,13 +191,13 @@ public abstract class MPJAbstractWrapper Children between(boolean condition, SFunction column, Object val1, Object val2) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN, + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), BETWEEN, () -> formatParam(null, val1), AND, () -> formatParam(null, val2))); } @Override public Children notBetween(boolean condition, SFunction column, Object val1, Object val2) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN, + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), NOT_BETWEEN, () -> formatParam(null, val1), AND, () -> formatParam(null, val2))); } @@ -258,43 +269,43 @@ public abstract class MPJAbstractWrapper Children isNull(boolean condition, SFunction column) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IS_NULL)); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), IS_NULL)); } @Override public Children isNotNull(boolean condition, SFunction column) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IS_NOT_NULL)); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), IS_NOT_NULL)); } @Override public Children in(boolean condition, SFunction column, Collection coll) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IN, inExpression(coll))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), IN, inExpression(coll))); } @Override public Children in(boolean condition, SFunction column, Object... values) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IN, inExpression(values))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), IN, inExpression(values))); } @Override public Children notIn(boolean condition, SFunction column, Collection coll) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN, inExpression(coll))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), NOT_IN, inExpression(coll))); } @Override public Children notIn(boolean condition, SFunction column, Object... values) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN, inExpression(values))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), NOT_IN, inExpression(values))); } @Override public Children inSql(boolean condition, SFunction column, String inValue) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), IN, + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), IN, () -> String.format("(%s)", inValue))); } @Override public Children notInSql(boolean condition, SFunction column, String inValue) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_IN, + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), NOT_IN, () -> String.format("(%s)", inValue))); } @@ -302,7 +313,7 @@ public abstract class MPJAbstractWrapper Children groupBy(boolean condition, List> columns) { return maybeDo(condition, () -> { if (CollectionUtils.isNotEmpty(columns)) { - String one = (StringPool.COMMA + columnsToString(columns)); + String one = (StringPool.COMMA + columnsToString(false, columns)); final String finalOne = one; appendSqlSegments(GROUP_BY, () -> finalOne); } @@ -312,9 +323,9 @@ public abstract class MPJAbstractWrapper Children groupBy(boolean condition, SFunction column, SFunction... columns) { return maybeDo(condition, () -> { - String one = columnToString(column); + String one = columnToString(column, false); if (ArrayUtils.isNotEmpty(columns)) { - one += (StringPool.COMMA + columnsToString(columns)); + one += (StringPool.COMMA + columnsToString(false, columns)); } final String finalOne = one; appendSqlSegments(GROUP_BY, () -> finalOne); @@ -327,7 +338,7 @@ public abstract class MPJAbstractWrapper appendSqlSegments(ORDER_BY, - columnToSqlSegment(columnSqlInjectFilter(c)), mode)); + columnToSqlSegment(columnSqlInjectFilter(c), false), mode)); } }); } @@ -338,7 +349,7 @@ public abstract class MPJAbstractWrapper appendSqlSegments(ORDER_BY, - columnToSqlSegment(columnSqlInjectFilter(c)), mode)); + columnToSqlSegment(columnSqlInjectFilter(c), false), mode)); } }); } @@ -347,10 +358,10 @@ public abstract class MPJAbstractWrapper Children orderBy(boolean condition, boolean isAsc, SFunction column, SFunction... columns) { return maybeDo(condition, () -> { final SqlKeyword mode = isAsc ? ASC : DESC; - appendSqlSegments(ORDER_BY, columnToSqlSegment(column), mode); + appendSqlSegments(ORDER_BY, columnToSqlSegment(column, false), mode); if (ArrayUtils.isNotEmpty(columns)) { Arrays.stream(columns).forEach(c -> appendSqlSegments(ORDER_BY, - columnToSqlSegment(columnSqlInjectFilter(c)), mode)); + columnToSqlSegment(columnSqlInjectFilter(c), false), mode)); } }); } @@ -397,7 +408,7 @@ public abstract class MPJAbstractWrapper拼接 LIKE 以及 值

*/ protected Children likeValue(boolean condition, SqlKeyword keyword, SFunction column, Object val, SqlLike sqlLike) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), keyword, + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), keyword, () -> formatParam(null, SqlUtils.concatLike(val, sqlLike)))); } @@ -410,13 +421,13 @@ public abstract class MPJAbstractWrapper Children addCondition(boolean condition, SFunction column, SqlKeyword sqlKeyword, Object val) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), sqlKeyword, + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, true), sqlKeyword, () -> formatParam(null, val))); } protected Children addCondition(boolean condition, SFunction column, SqlKeyword sqlKeyword, SFunction val) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), sqlKeyword, - columnToSqlSegment(val))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), sqlKeyword, + columnToSqlSegment(val, true))); } /** @@ -598,14 +609,14 @@ public abstract class MPJAbstractWrapper ISqlSegment columnToSqlSegment(SFunction column) { - return () -> columnToString(column); + protected final ISqlSegment columnToSqlSegment(SFunction column, boolean isJoin) { + return () -> columnToString(column, isJoin); } /** * 获取 columnName */ - protected String columnToString(X column) { + protected String columnToString(X column, boolean isJoin) { return (String) column; } @@ -614,8 +625,8 @@ public abstract class MPJAbstractWrapper String columnsToString(X... columns) { - return Arrays.stream(columns).map(this::columnToString).collect(joining(StringPool.COMMA)); + protected String columnsToString(boolean isJoin, X... columns) { + return Arrays.stream(columns).map(i -> this.columnToString(i, isJoin)).collect(joining(StringPool.COMMA)); } @Override @@ -667,33 +678,33 @@ public abstract class MPJAbstractWrapper Children between(boolean condition, SFunction column, SFunction val1, SFunction val2) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN, - columnToSqlSegment(val1), AND, columnToSqlSegment(val2))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), BETWEEN, + columnToSqlSegment(val1, true), AND, columnToSqlSegment(val2, true))); } public Children between(boolean condition, SFunction column, Object val1, SFunction val2) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN, - () -> formatParam(null, val1), AND, columnToSqlSegment(val2))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), BETWEEN, + () -> formatParam(null, val1), AND, columnToSqlSegment(val2, true))); } public Children between(boolean condition, SFunction column, SFunction val1, Object val2) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN, - columnToSqlSegment(val1), AND, () -> formatParam(null, val2))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), BETWEEN, + columnToSqlSegment(val1, true), AND, () -> formatParam(null, val2))); } @Override public Children notBetween(boolean condition, SFunction column, SFunction val1, SFunction val2) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN, - columnToSqlSegment(val1), AND, columnToSqlSegment(val2))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), NOT_BETWEEN, + columnToSqlSegment(val1, true), AND, columnToSqlSegment(val2, true))); } public Children notBetween(boolean condition, SFunction column, Object val1, SFunction val2) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN, - () -> formatParam(null, val1), AND, columnToSqlSegment(val2))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), NOT_BETWEEN, + () -> formatParam(null, val1), AND, columnToSqlSegment(val2, true))); } public Children notBetween(boolean condition, SFunction column, SFunction val1, Object val2) { - return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN, - columnToSqlSegment(val1), AND, () -> formatParam(null, val2))); + return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, false), NOT_BETWEEN, + columnToSqlSegment(val1, true), AND, () -> formatParam(null, val2))); } } 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 a73b3f7..c98c2a6 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 @@ -54,10 +54,6 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper selectColumns = new ArrayList<>(); - /** - * ON sql wrapper集合 - */ - private final List> onWrappers = new ArrayList<>(); /** * 映射关系 */ @@ -68,6 +64,11 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper joinClass; + private boolean subLogicSql = ConfigProperties.subTableLogic; + /** + * 主表逻辑删除开关 + */ + private boolean logicSql = true; /** * 不建议直接 new 该实例,使用 MPJWrappers.lambdaQuery() @@ -225,7 +228,9 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper genericType = MPJReflectionKit.getGenericType(field); Class ofType = (Class) genericType; MybatisLabel.Builder builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), ofType, false); - this.resultMapMybatisLabel.add(collection.apply(builder).build()); + MybatisLabel.Builder czBuilder = collection.apply(builder); + this.customResult = czBuilder.hasCustom(); + this.resultMapMybatisLabel.add(czBuilder.build()); return typedThis; } @@ -260,7 +265,9 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper builder = new MybatisLabel.Builder<>(dtoFieldName, child, field.getType(), (Class) child, false); - this.resultMapMybatisLabel.add(collection.apply(builder).build()); + MybatisLabel.Builder cfBuilder = collection.apply(builder); + this.customResult = cfBuilder.hasCustom(); + this.resultMapMybatisLabel.add(cfBuilder.build()); return typedThis; } @@ -331,7 +338,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper { - String str = Constant.TABLE_ALIAS + getDefault(subTable.get(i.getClazz())) + StringPool.DOT + i.getColumnName(); + String str = Constant.TABLE_ALIAS + getDefaultSelect(i.getClazz(), (i.getClazz() == getEntityClass() && !i.isLabel())) + StringPool.DOT + i.getColumnName(); return (i.getFuncEnum() == null ? str : String.format(i.getFuncEnum().getSql(), str)) + (StringUtils.isBlank(i.getAlias()) ? StringPool.EMPTY : (Constant.AS + i.getAlias())); }).collect(Collectors.joining(StringPool.COMMA)); @@ -393,11 +400,41 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper + * 副表逻辑删除默认在where语句中 + * 但有时候需要让它出现在on语句中, 这两种写法区别还是很大的 + * 所以可以关闭副表逻辑删除, 通过on语句多条件, 自己实现on语句的逻辑删除 + */ + public MPJLambdaWrapper disableSubLogicDel() { + this.subLogicSql = false; + return typedThis; + } + + public MPJLambdaWrapper enableSubLogicDel() { + this.subLogicSql = true; + return typedThis; + } + + /** + * 关闭主表逻辑删除 + */ + public MPJLambdaWrapper disableLogicDel() { + this.logicSql = false; + return typedThis; + } + + public MPJLambdaWrapper enableLogicDel() { + this.logicSql = true; + return typedThis; + } + /** * 副表部分逻辑删除支持 */ - public String getLogicSql() { - if (ConfigProperties.subTableLogic) { + public String getSubLogicSql() { + if (subLogicSql) { if (CollectionUtils.isEmpty(subTable)) { return StringPool.EMPTY; } @@ -407,9 +444,16 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper join(String keyWord, Class clazz, OnFunction function) { - MPJLambdaWrapper apply = function.apply(instance(keyWord, clazz)); + public MPJLambdaWrapper join(String keyWord, Class clazz, OnFunction function) { + MPJLambdaWrapper apply = function.apply(instance(keyWord, clazz)); subTable.put(clazz, tableIndex); onWrappers.add(apply); tableIndex++; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java index a6c56c8..9550bf4 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java @@ -23,14 +23,14 @@ public interface LambdaJoin extends MPJBaseJoin { } /** - * left join + * left join 多条件 *

* 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) * * @param clazz 关联实体类 * @param function 条件 */ - default Children leftJoin(Class clazz, OnFunction function) { + default Children leftJoin(Class clazz, OnFunction function) { return join(Constant.LEFT_JOIN, clazz, function); } @@ -44,7 +44,7 @@ public interface LambdaJoin extends MPJBaseJoin { /** * ignore 参考 left join */ - default Children rightJoin(Class clazz, OnFunction function) { + default Children rightJoin(Class clazz, OnFunction function) { return join(Constant.RIGHT_JOIN, clazz, function); } @@ -58,7 +58,7 @@ public interface LambdaJoin extends MPJBaseJoin { /** * ignore 参考 left join */ - default Children innerJoin(Class clazz, OnFunction function) { + default Children innerJoin(Class clazz, OnFunction function) { return join(Constant.INNER_JOIN, clazz, function); } @@ -73,7 +73,7 @@ public interface LambdaJoin extends MPJBaseJoin { /** * ignore 参考 left join */ - default Children fullJoin(Class clazz, OnFunction function) { + default Children fullJoin(Class clazz, OnFunction function) { return join(Constant.FULL_JOIN, clazz, function); } @@ -84,5 +84,5 @@ public interface LambdaJoin extends MPJBaseJoin { * @param clazz 连表实体类 * @param function 关联条件 */ - Children join(String keyWord, Class clazz, OnFunction function); + Children join(String keyWord, Class clazz, OnFunction function); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnFunction.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnFunction.java index 9cdb25c..4a30159 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnFunction.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/on/OnFunction.java @@ -9,7 +9,7 @@ import com.github.yulichang.wrapper.MPJLambdaWrapper; * @since 1.1.8 */ @FunctionalInterface -public interface OnFunction { +public interface OnFunction { - MPJLambdaWrapper apply(MPJLambdaWrapper wrapper); + 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 4e80bc8..4ec1500 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 @@ -178,6 +178,9 @@ public class MybatisLabel { return this; } + public boolean hasCustom(){ + return CollectionUtils.isNotEmpty(mybatisLabel.resultList) || CollectionUtils.isNotEmpty(mybatisLabel.mybatisLabels); + } public MybatisLabel build() { if (CollectionUtils.isEmpty(mybatisLabel.resultList)) { diff --git a/mybatis-plus-join-test/README.md b/mybatis-plus-join-test/README.md new file mode 100644 index 0000000..3e291a5 --- /dev/null +++ b/mybatis-plus-join-test/README.md @@ -0,0 +1,5 @@ +## mybatis plus join 测试模块 + +- test-collection : 对多或对一查询测试工程 +- test-join : 连表查询测试类 +- test-mapping : 注解映射测试工程 \ No newline at end of file diff --git a/mybatis-plus-join-test/test-collection/src/main/java/com/github/yulichang/test/collection/enums/Sex.java b/mybatis-plus-join-test/test-collection/src/main/java/com/github/yulichang/test/collection/enums/Sex.java deleted file mode 100644 index a5382ee..0000000 --- a/mybatis-plus-join-test/test-collection/src/main/java/com/github/yulichang/test/collection/enums/Sex.java +++ /dev/null @@ -1,24 +0,0 @@ -package com.github.yulichang.test.collection.enums; - -import com.baomidou.mybatisplus.annotation.EnumValue; -import lombok.Getter; -import lombok.ToString; - -@Getter -@ToString -public enum Sex { - - MAN(0, "男"), - - WOMAN(1, "女"); - - @EnumValue - private final int code; - private final String des; - - Sex(int code, String des) { - this.code = code; - this.des = des; - } - -} diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/TableDTO.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/TableDTO.java deleted file mode 100644 index c3d6aae..0000000 --- a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/TableDTO.java +++ /dev/null @@ -1,61 +0,0 @@ -package com.github.yulichang.test.join.dto; - -import lombok.Data; - -import java.util.List; - -@Data -public class TableDTO { - - private Integer id; - - private String name; - - private List bbList; - - @Data - public static class TableBDTO { - - private Integer id; - - private Integer aid; - - private String name; - - private List ccList; - } - - @Data - public static class TableCDTO { - - private Integer id; - - private Integer bid; - - private String name; - - private List ddList; - } - - @Data - public static class TableDDTO { - - private Integer id; - - private Integer cid; - - private String name; - - private List eeList; - } - - @Data - public static class TableEDTO { - - private Integer id; - - private Integer did; - - private String name; - } -} diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserDTO.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserDTO.java index 382a830..8c75542 100644 --- a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserDTO.java +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserDTO.java @@ -1,5 +1,6 @@ package com.github.yulichang.test.join.dto; +import com.github.yulichang.test.join.entity.UserDO; import com.github.yulichang.test.join.enums.Sex; import lombok.Data; import lombok.ToString; @@ -35,7 +36,9 @@ public class UserDTO { /** area */ private String city; /** area */ - private String area; + private Map area; private List addressList; + + private List children; } 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 8f589a4..6ef7a32 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 @@ -11,20 +11,23 @@ import lombok.EqualsAndHashCode; import lombok.ToString; import lombok.experimental.Accessors; +import java.util.List; import java.util.Map; @Data @ToString @Accessors(chain = true) @EqualsAndHashCode -@TableName(value = "`user`",autoResultMap = true) +@TableName(value = "`user`", autoResultMap = true) public class UserDO { @TableId private Integer id; + private Integer pid; + @TableField(value = "`name`", typeHandler = JacksonTypeHandler.class) - private Map aName; + private Map name; private Sex sex; @@ -34,4 +37,7 @@ public class UserDO { @TableLogic private Boolean del; + + @TableField(exist = false) + private List children; } 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 6bd397b..133e0be 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,28 +29,28 @@ INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北 DELETE FROM `user`; -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 1, '{"aa":"aaa","bb":"bbb"}', 1, 1, 'https://url-01', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 2, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-02', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 3, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-03', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 4, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-04', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 5, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-05', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 6, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-06', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 7, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-07', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 8, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-08', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES ( 9, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-09', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (10, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-10', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (11, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-11', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (12, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-12', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (13, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-13', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (14, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-14', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (15, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-15', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (16, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-16', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (17, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-17', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (18, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-18', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (19, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-19', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (20, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-20', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (21, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-21', false); -INSERT INTO `user` (id, `name`, `address_id`, sex, head_img, del) VALUES (22, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-22', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 1, 1, '{"aa":"aaa","bb":"bbb"}', 1, 1, 'https://url-01', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 2, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-02', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 3, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-03', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 4, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-04', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 5, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-05', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 6, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-06', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 7, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-07', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 8, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-08', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES ( 9, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-09', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (10, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-10', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (11, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-11', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (12, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-12', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (13, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-13', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (14, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-14', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (15, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-15', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (16, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-16', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (17, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-17', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (18, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-18', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (19, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-19', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (20, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-20', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (21, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-21', false); +INSERT INTO `user` (id, pid, `name`, `address_id`, sex, head_img, del) VALUES (22, 1, '{"aa":"aaa","bb":"bbb"}', 1, 0, 'https://url-22', false); DELETE FROM address; 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 f728079..feab00b 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 @@ -21,6 +21,7 @@ create table `user` ( id int auto_increment primary key, + `pid` int not null, `name` varchar(255) not null, `address_id` int not null, sex tinyint not null, 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 e45f746..63bf7d9 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 @@ -8,6 +8,7 @@ import com.github.yulichang.test.join.dto.UserDTO; import com.github.yulichang.test.join.entity.AddressDO; import com.github.yulichang.test.join.entity.AreaDO; import com.github.yulichang.test.join.entity.UserDO; +import com.github.yulichang.test.join.mapper.AddressMapper; import com.github.yulichang.test.join.mapper.UserMapper; import com.github.yulichang.toolkit.MPJWrappers; import com.github.yulichang.wrapper.MPJLambdaWrapper; @@ -59,6 +60,51 @@ class LambdaWrapperTest { System.out.println(list); } + /** + * ms缓存测试 + */ + @Test + void testMSCache() { + MPJLambdaWrapper wrapper = new MPJLambdaWrapper() + .selectAll(UserDO.class) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId); + List list = userMapper.selectJoinList(UserDTO.class, wrapper); + + MPJLambdaWrapper wrapper1 = new MPJLambdaWrapper() + .select(UserDO::getId) + .selectAs(UserDO::getName,UserDTO::getArea) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId); + List list1 = userMapper.selectJoinList(UserDTO.class, wrapper1); + + assert list1.get(0).getArea() != null; + } + + /** + * 自连接测试 + */ + @Test + void testInner() throws Exception { + MPJLambdaWrapper wrapper = new MPJLambdaWrapper() + .disableSubLogicDel() + .disableLogicDel() + .selectAll(UserDO.class) + .selectCollection(UserDO.class, UserDO::getChildren) + .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId); + List list = userMapper.selectJoinList(UserDO.class, wrapper); + + MPJLambdaWrapper wrapper1 = new MPJLambdaWrapper() + .disableSubLogicDel() + .disableLogicDel() + .selectAll(UserDO.class) + .selectCollection(UserDO.class, UserDO::getChildren) + .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId); + List list1 = userMapper.selectJoinList(UserDO.class, wrapper1); + + System.out.println(list); + } + /** * 简单的分页关联查询 lambda