From 326bd68ade1619b4de59e3d66c72b068903379d5 Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Mon, 15 Jul 2024 18:12:13 +0800 Subject: [PATCH] fix: https://github.com/yulichang/mybatis-plus-join/issues/155 --- .../MybatisPlusJoinAutoConfiguration.java | 7 +- .../MybatisPlusJoinIfExistsConsumer.java | 5 +- .../yulichang/config/ConfigProperties.java | 13 +- .../yulichang/config/enums/IfExistsEnum.java | 9 +- .../config/enums/LogicDelTypeEnum.java | 4 +- .../yulichang/interceptor/MPJInterceptor.java | 2 +- .../yulichang/kt/resultmap/MybatisLabel.java | 2 +- .../github/yulichang/kt/resultmap/Result.java | 6 +- .../wrapper/JoinAbstractLambdaWrapper.java | 5 +- .../wrapper/JoinAbstractWrapper.java | 6 +- .../yulichang/wrapper/MPJLambdaWrapper.java | 8 +- .../wrapper/enums/IfExistsSqlKeyWordEnum.java | 4 +- .../wrapper/interfaces/MBiPredicate.java | 15 +++ .../wrapper/interfaces/MFunction.java | 4 +- .../wrapper/interfaces/MPredicate.java | 15 +++ .../wrapper/interfaces/QueryJoin.java | 2 +- .../yulichang/wrapper/resultmap/IResult.java | 4 +- .../yulichang/wrapper/resultmap/Label.java | 3 +- .../wrapper/resultmap/MybatisLabel.java | 2 +- .../yulichang/wrapper/resultmap/Result.java | 6 +- .../yulichang/wrapper/segments/Select.java | 6 +- .../wrapper/segments/SelectAlias.java | 11 +- .../wrapper/segments/SelectCache.java | 60 ++++++--- .../wrapper/segments/SelectFunc.java | 11 +- .../wrapper/segments/SelectLabel.java | 11 +- .../wrapper/segments/SelectNormal.java | 11 +- .../wrapper/segments/SelectString.java | 9 +- .../yulichang/wrapper/segments/SelectSub.java | 11 +- .../solon/plugin/XPluginImpl.java | 4 +- .../test/join/LambdaWrapperTest.java | 120 +++++++++--------- .../yulichang/test/join/unit/CloneTest.java | 40 ++++++ 31 files changed, 273 insertions(+), 143 deletions(-) create mode 100644 mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MBiPredicate.java create mode 100644 mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MPredicate.java create mode 100644 mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/CloneTest.java 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 2df8780..ff650db 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 @@ -12,6 +12,7 @@ import com.github.yulichang.injector.MPJSqlInjector; import com.github.yulichang.interceptor.MPJInterceptor; import com.github.yulichang.toolkit.SpringContentUtils; import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum; +import com.github.yulichang.wrapper.interfaces.MBiPredicate; import org.apache.ibatis.session.SqlSessionFactory; import org.mybatis.spring.SqlSessionFactoryBean; import org.slf4j.Logger; @@ -42,7 +43,6 @@ import org.springframework.core.annotation.Order; import javax.sql.DataSource; import java.util.List; import java.util.Optional; -import java.util.function.BiPredicate; /** * springboot 自动配置类 @@ -73,9 +73,10 @@ public class MybatisPlusJoinAutoConfiguration { ConfigProperties.subQueryAlias = this.properties.getSubQueryAlias(); ConfigProperties.subTableLogic = this.properties.getSubTableLogic(); ConfigProperties.mappingMaxCount = this.properties.getMappingMaxCount(); + ConfigProperties.Convert.IfExists = this.properties.getIfExists(); ConfigProperties.ifExists = Optional.ofNullable(IfExistsConsumers.getIfAvailable()) - .map(m -> (BiPredicate) m) - .orElse((val, key) -> this.properties.getIfExists().test(val)); + .map(m -> (MBiPredicate) m) + .orElse((val, key) -> ConfigProperties.Convert.IfExists.test(val)); info("mybatis plus join properties config complete"); } diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/consumer/MybatisPlusJoinIfExistsConsumer.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/consumer/MybatisPlusJoinIfExistsConsumer.java index aa200d5..cfcd314 100644 --- a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/consumer/MybatisPlusJoinIfExistsConsumer.java +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/consumer/MybatisPlusJoinIfExistsConsumer.java @@ -1,8 +1,7 @@ package com.github.yulichang.autoconfigure.consumer; import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum; - -import java.util.function.BiPredicate; +import com.github.yulichang.wrapper.interfaces.MBiPredicate; /** * 自定义IfExists策略 @@ -10,5 +9,5 @@ import java.util.function.BiPredicate; * @author yulichang * @since 1.4.9 */ -public interface MybatisPlusJoinIfExistsConsumer extends BiPredicate { +public interface MybatisPlusJoinIfExistsConsumer extends MBiPredicate { } 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 921baa4..98042bb 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 @@ -3,8 +3,7 @@ package com.github.yulichang.config; import com.github.yulichang.config.enums.IfExistsEnum; import com.github.yulichang.config.enums.LogicDelTypeEnum; import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum; - -import java.util.function.BiPredicate; +import com.github.yulichang.wrapper.interfaces.MBiPredicate; /** * @author yulichang @@ -53,5 +52,13 @@ public class ConfigProperties { *

* NOT_BLANK 非空白字符串 例: "" -> false, " " -> false, "\r" -> false, "abc" -> true ... */ - public static BiPredicate ifExists = (val, key) -> IfExistsEnum.NOT_EMPTY.test(val); + public static MBiPredicate ifExists = (val, key) -> IfExistsEnum.NOT_EMPTY.test(val); + + + /** + * 暂存 不可使用 用于规避starter包中需要序列化 + */ + public static class Convert { + public static IfExistsEnum IfExists = IfExistsEnum.NOT_EMPTY; + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/enums/IfExistsEnum.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/enums/IfExistsEnum.java index c5e3a97..1d79b6f 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/enums/IfExistsEnum.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/enums/IfExistsEnum.java @@ -1,9 +1,10 @@ package com.github.yulichang.config.enums; import com.github.yulichang.toolkit.MPJStringUtils; +import com.github.yulichang.wrapper.interfaces.MPredicate; +import java.io.Serializable; import java.util.Objects; -import java.util.function.Predicate; /** * 条件判断策略 @@ -11,7 +12,7 @@ import java.util.function.Predicate; * @author yulichang * @since 1.4.9 */ -public enum IfExistsEnum implements Predicate { +public enum IfExistsEnum implements MPredicate, Serializable { /** * 非null @@ -26,9 +27,9 @@ public enum IfExistsEnum implements Predicate { */ NOT_BLANK(val -> NOT_NULL.and(v -> !(v instanceof CharSequence) || MPJStringUtils.isNotBlank((CharSequence) v)).test(val)); - private final Predicate predicate; + private final MPredicate predicate; - IfExistsEnum(Predicate predicate) { + IfExistsEnum(MPredicate predicate) { this.predicate = predicate; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/enums/LogicDelTypeEnum.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/enums/LogicDelTypeEnum.java index 7c66878..84797ec 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/enums/LogicDelTypeEnum.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/enums/LogicDelTypeEnum.java @@ -1,11 +1,13 @@ package com.github.yulichang.config.enums; +import java.io.Serializable; + /** * 逻辑删除位置 * * @author yulichang * @since 1.4.4 */ -public enum LogicDelTypeEnum { +public enum LogicDelTypeEnum implements Serializable { WHERE, ON } 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 e6ddd07..4726b95 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 @@ -191,7 +191,7 @@ public class MPJInterceptor implements Interceptor { } private ResultMapping selectToResult(Class entity, Select select, Class type, ResultMapping.Builder builder) { - if (select.hasTypeHandle() && select.getTableFieldInfo().getPropertyType().isAssignableFrom(type)) { + if (select.hasTypeHandle() && select.getPropertyType().isAssignableFrom(type)) { builder.typeHandler(select.getTypeHandle()); } if (select.isPk() && entity == select.getClazz()) { diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/resultmap/MybatisLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/resultmap/MybatisLabel.java index 8067232..a11c497 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/resultmap/MybatisLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/resultmap/MybatisLabel.java @@ -264,7 +264,7 @@ public class MybatisLabel implements Label { result.setIndex(mybatisLabel.index); result.setProperty(i.getColumProperty()); result.setJavaType(i.getColumnType()); - result.setJdbcType(Objects.isNull(i.getTableFieldInfo()) ? null : i.getTableFieldInfo().getJdbcType()); + result.setJdbcType(i.getJdbcType()); result.setSelectNormal(i); return result; }).collect(Collectors.toList())); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/resultmap/Result.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/resultmap/Result.java index 81a0831..83b9896 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/resultmap/Result.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/resultmap/Result.java @@ -1,6 +1,5 @@ package com.github.yulichang.kt.resultmap; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.github.yulichang.toolkit.KtUtils; import com.github.yulichang.toolkit.support.ColumnCache; @@ -14,7 +13,6 @@ import org.apache.ibatis.type.JdbcType; import java.util.Map; import java.util.Objects; -import java.util.Optional; /** * result 标签 @@ -60,7 +58,7 @@ public class Result implements IResult { result.selectNormal = selectCache; result.property = selectCache.getColumProperty(); result.javaType = selectCache.getColumnType(); - result.jdbcType = Optional.ofNullable(selectCache.getTableFieldInfo()).map(TableFieldInfo::getJdbcType).orElse(null); + result.jdbcType = selectCache.getJdbcType(); } public Builder property(KProperty property) { @@ -80,7 +78,7 @@ public class Result implements IResult { result.javaType = normal.getColumnType(); } if (Objects.isNull(result.jdbcType)) { - result.jdbcType = Objects.isNull(normal.getTableFieldInfo()) ? null : normal.getTableFieldInfo().getJdbcType(); + result.jdbcType = normal.getJdbcType(); } return this; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractLambdaWrapper.java index 84406d1..3118885 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractLambdaWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractLambdaWrapper.java @@ -24,7 +24,6 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.BiConsumer; -import java.util.function.Function; import java.util.stream.Collectors; import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY; @@ -66,7 +65,7 @@ public abstract class JoinAbstractLambdaWrapper tableFunc; + protected SFunction tableFunc; /** * 逻辑删除位置 @@ -170,7 +169,7 @@ public abstract class JoinAbstractLambdaWrapper tableFunc) { + public Children setTableName(SFunction tableFunc) { if (isMain) { if (tableFunc != null) { this.dynamicTableName = true; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractWrapper.java index 74d7804..43c4aeb 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/JoinAbstractWrapper.java @@ -122,7 +122,7 @@ public abstract class JoinAbstractWrapper ifExists = ConfigProperties.ifExists; + protected MBiPredicate ifExists = ConfigProperties.ifExists; @Override public T getEntity() { @@ -175,7 +175,7 @@ public abstract class JoinAbstractWrapper IfExists) { + public Children setIfExists(MBiPredicate IfExists) { this.ifExists = IfExists; return typedThis; } @@ -329,7 +329,7 @@ public abstract class JoinAbstractWrapper formatSqlMaybeWithParam(applySql, null, values))); } - public Children applyFunc(String applySql, Function[]> consumerFunction, Object... values) { + public Children applyFunc(String applySql, SFunction[]> consumerFunction, Object... values) { return applyFunc(true, applySql, consumerFunction, values); } 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 81337ca..afd90d3 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 @@ -10,17 +10,13 @@ import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.*; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum; -import com.github.yulichang.wrapper.interfaces.Chain; -import com.github.yulichang.wrapper.interfaces.Query; -import com.github.yulichang.wrapper.interfaces.QueryLabel; -import com.github.yulichang.wrapper.interfaces.SelectWrapper; +import com.github.yulichang.wrapper.interfaces.*; import com.github.yulichang.wrapper.resultmap.Label; import com.github.yulichang.wrapper.segments.*; import lombok.Getter; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -126,7 +122,7 @@ public class MPJLambdaWrapper extends JoinAbstractLambdaWrapper paramNameValuePairs, MergeSegments mergeSegments, SharedString paramAlias, SharedString lastSql, SharedString sqlComment, SharedString sqlFirst, TableList tableList, Integer index, String keyWord, Class joinClass, String tableName, - BiPredicate IfExists) { + MBiPredicate IfExists) { super.setEntity(entity); super.setEntityClass(entityClass); this.paramNameSeq = paramNameSeq; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/IfExistsSqlKeyWordEnum.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/IfExistsSqlKeyWordEnum.java index cdfb728..11c8d83 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/IfExistsSqlKeyWordEnum.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/enums/IfExistsSqlKeyWordEnum.java @@ -1,12 +1,14 @@ package com.github.yulichang.wrapper.enums; +import java.io.Serializable; + /** * if absent 枚举 * * @author yulichang * @since 1.4.9 */ -public enum IfExistsSqlKeyWordEnum { +public enum IfExistsSqlKeyWordEnum implements Serializable { EQ, NE, GT, diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MBiPredicate.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MBiPredicate.java new file mode 100644 index 0000000..8f6c228 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MBiPredicate.java @@ -0,0 +1,15 @@ +package com.github.yulichang.wrapper.interfaces; + +import java.io.Serializable; +import java.util.function.BiPredicate; + +/** + * on function + * + * @author yulichang + * @since 1.4.14 + */ +@FunctionalInterface +public interface MBiPredicate extends BiPredicate, Serializable { + +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MFunction.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MFunction.java index 3c214f0..dc968db 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MFunction.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MFunction.java @@ -1,5 +1,7 @@ package com.github.yulichang.wrapper.interfaces; +import java.io.Serializable; + /** * on function * @@ -7,7 +9,7 @@ package com.github.yulichang.wrapper.interfaces; * @since 1.1.8 */ @FunctionalInterface -public interface MFunction { +public interface MFunction extends Serializable { T apply(T wrapper); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MPredicate.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MPredicate.java new file mode 100644 index 0000000..2dd7c19 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/MPredicate.java @@ -0,0 +1,15 @@ +package com.github.yulichang.wrapper.interfaces; + +import java.io.Serializable; +import java.util.function.Predicate; + +/** + * on function + * + * @author yulichang + * @since 1.4.14 + */ +@FunctionalInterface +public interface MPredicate extends Predicate, Serializable { + +} 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 a4815fc..72cd083 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 @@ -362,7 +362,7 @@ public interface QueryJoin extends MPJBaseJoin, String * @param right 扩展 用于关联表的 select 和 where */ default Children join(String keyWord, Class clazz, SFunction left, String rightAlias, SFunction right) { - return join(keyWord, clazz, on -> on.eq(left, rightAlias,right)); + return join(keyWord, clazz, on -> on.eq(left, rightAlias, right)); } /** diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/IResult.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/IResult.java index fdfb957..c25bc83 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/IResult.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/IResult.java @@ -3,7 +3,9 @@ package com.github.yulichang.wrapper.resultmap; import com.github.yulichang.wrapper.segments.SelectCache; import org.apache.ibatis.type.JdbcType; -public interface IResult { +import java.io.Serializable; + +public interface IResult extends Serializable { boolean isId(); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Label.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Label.java index 3ffeced..aca7a97 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Label.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Label.java @@ -1,5 +1,6 @@ package com.github.yulichang.wrapper.resultmap; +import java.io.Serializable; import java.util.List; /** @@ -8,7 +9,7 @@ import java.util.List; * @author yulichang * @since 2023/3/17 11:35 */ -public interface Label { +public interface Label extends Serializable { String getProperty(); 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 412d2b2..a9a6eb8 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 @@ -268,7 +268,7 @@ public class MybatisLabel implements Label { result.setIndex(mybatisLabel.index); result.setProperty(i.getColumProperty()); result.setJavaType(i.getColumnType()); - result.setJdbcType(Objects.isNull(i.getTableFieldInfo()) ? null : i.getTableFieldInfo().getJdbcType()); + result.setJdbcType(i.getJdbcType()); result.setSelectNormal(i); return result; }).collect(Collectors.toList())); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java index c920c60..2ac52db 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java @@ -1,6 +1,5 @@ package com.github.yulichang.wrapper.resultmap; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.LambdaUtils; @@ -13,7 +12,6 @@ import org.apache.ibatis.type.JdbcType; import java.util.Map; import java.util.Objects; -import java.util.Optional; /** * result 标签 @@ -59,7 +57,7 @@ public class Result implements IResult { result.selectNormal = selectCache; result.property = selectCache.getColumProperty(); result.javaType = selectCache.getColumnType(); - result.jdbcType = Optional.ofNullable(selectCache.getTableFieldInfo()).map(TableFieldInfo::getJdbcType).orElse(null); + result.jdbcType = selectCache.getJdbcType(); } public Builder property(SFunction property) { @@ -80,7 +78,7 @@ public class Result implements IResult { result.javaType = normal.getColumnType(); } if (Objects.isNull(result.jdbcType)) { - result.jdbcType = Objects.isNull(normal.getTableFieldInfo()) ? null : normal.getTableFieldInfo().getJdbcType(); + result.jdbcType = normal.getJdbcType(); } return this; } 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 5d1e0cb..afa8858 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,7 +1,7 @@ package com.github.yulichang.wrapper.segments; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.github.yulichang.wrapper.enums.BaseFuncEnum; +import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import java.io.Serializable; @@ -40,7 +40,9 @@ public interface Select extends Serializable { String getAlias(); - TableFieldInfo getTableFieldInfo(); + Class getPropertyType(); + + JdbcType getJdbcType(); boolean isFunc(); 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 157178c..fa843d4 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 @@ -1,9 +1,9 @@ package com.github.yulichang.wrapper.segments; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import lombok.Getter; +import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; /** @@ -79,8 +79,13 @@ public class SelectAlias implements Select { } @Override - public TableFieldInfo getTableFieldInfo() { - return cache.getTableFieldInfo(); + public Class getPropertyType() { + return cache.getPropertyType(); + } + + @Override + public JdbcType getJdbcType() { + return cache.getJdbcType(); } @Override diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectCache.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectCache.java index ac0c4bf..eb0db70 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectCache.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectCache.java @@ -8,11 +8,15 @@ import com.github.yulichang.toolkit.MPJStringUtils; import com.github.yulichang.toolkit.TableHelper; import lombok.Getter; import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import org.apache.ibatis.type.TypeHandlerRegistry; import org.apache.ibatis.type.UnknownTypeHandler; +import java.io.Serializable; +import java.util.Map; import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; /** * 缓存 @@ -21,7 +25,7 @@ import java.util.Objects; * @since 1.3.10 */ @Getter -public class SelectCache { +public class SelectCache implements Serializable { /** * 实体类 @@ -55,20 +59,17 @@ public class SelectCache { */ private final String columProperty; - /** - * mp 字段信息 - */ - private final TableFieldInfo tableFieldInfo; - /** * 使用使用 hasTypeHandle */ private final boolean hasTypeHandle; /** - * hasTypeHandle 类型 + * tableFieldInfo中信息 */ - private final TypeHandler typeHandler; + private final Class propertyType; + private final JdbcType jdbcType; + private final Class> typeHandlerClass; /** * 是否查询 @@ -82,29 +83,48 @@ public class SelectCache { this.columnType = columnType; this.columProperty = columProperty; this.tagColumn = MPJStringUtils.getTargetColumn(column); - this.tableFieldInfo = tableFieldInfo; this.isSelect = isSelect; if (Objects.isNull(tableFieldInfo)) { this.hasTypeHandle = false; - this.typeHandler = null; + this.propertyType = null; + this.jdbcType = null; + this.typeHandlerClass = null; } else { - this.hasTypeHandle = this.tableFieldInfo.getTypeHandler() != null && tableFieldInfo.getTypeHandler() != UnknownTypeHandler.class; - if (this.hasTypeHandle) { - TableInfo info = TableHelper.getAssert(clazz); - this.typeHandler = getTypeHandler(AdapterHelper.getAdapter().mpjGetConfiguration(info), tableFieldInfo); - } else { - this.typeHandler = null; - } + this.propertyType = tableFieldInfo.getPropertyType(); + this.jdbcType = tableFieldInfo.getJdbcType(); + this.typeHandlerClass = tableFieldInfo.getTypeHandler(); + this.hasTypeHandle = tableFieldInfo.getTypeHandler() != null && tableFieldInfo.getTypeHandler() != UnknownTypeHandler.class; } } + public TypeHandler getTypeHandler() { + if (this.hasTypeHandle) { + return Cache.getTypeHandlerCache(this.clazz, this.typeHandlerClass, this.propertyType); + } + return null; + } - private TypeHandler getTypeHandler(Configuration configuration, TableFieldInfo info) { + private static TypeHandler getTypeHandler(Configuration configuration, Class propertyType, Class> typeHandlerClass) { TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry(); - TypeHandler typeHandler = registry.getMappingTypeHandler(info.getTypeHandler()); + TypeHandler typeHandler = registry.getMappingTypeHandler(typeHandlerClass); if (typeHandler == null) { - typeHandler = registry.getInstance(info.getPropertyType(), info.getTypeHandler()); + typeHandler = registry.getInstance(propertyType, typeHandlerClass); } return typeHandler; } + + public static class Cache { + private static final Map, Map, TypeHandler>> CACHE = new ConcurrentHashMap<>(); + + public static TypeHandler getTypeHandlerCache(Class table, Class> typeHandler, Class propertyType) { + if (table == null || typeHandler == null) { + return null; + } + Map, TypeHandler> map = CACHE.computeIfAbsent(table, k -> new ConcurrentHashMap<>()); + return map.computeIfAbsent(typeHandler, k -> { + TableInfo info = TableHelper.getAssert(table); + return getTypeHandler(AdapterHelper.getAdapter().mpjGetConfiguration(info), propertyType, typeHandler); + }); + } + } } 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 396a30d..d97b794 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 @@ -1,12 +1,12 @@ 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.toolkit.LambdaUtils; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import lombok.AllArgsConstructor; import lombok.Getter; +import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import java.util.Arrays; @@ -143,8 +143,13 @@ public class SelectFunc implements Select { } @Override - public TableFieldInfo getTableFieldInfo() { - return Objects.isNull(cache) ? null : cache.getTableFieldInfo(); + public Class getPropertyType() { + return Objects.isNull(cache) ? null : cache.getPropertyType(); + } + + @Override + public JdbcType getJdbcType() { + return Objects.isNull(cache) ? null : cache.getJdbcType(); } @Override 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 148c9e4..47e06a2 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 @@ -1,9 +1,9 @@ package com.github.yulichang.wrapper.segments; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import lombok.Getter; +import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; /** @@ -100,8 +100,13 @@ public class SelectLabel implements Select { } @Override - public TableFieldInfo getTableFieldInfo() { - return cache.getTableFieldInfo(); + public Class getPropertyType() { + return cache.getPropertyType(); + } + + @Override + public JdbcType getJdbcType() { + return cache.getJdbcType(); } @Override 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 6ca79fd..a2e9d07 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,8 +1,8 @@ package com.github.yulichang.wrapper.segments; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import lombok.Getter; +import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; /** @@ -81,8 +81,13 @@ public class SelectNormal implements Select { } @Override - public TableFieldInfo getTableFieldInfo() { - return cache.getTableFieldInfo(); + public Class getPropertyType() { + return cache.getPropertyType(); + } + + @Override + public JdbcType getJdbcType() { + return cache.getJdbcType(); } @Override 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 index 975bed9..9dee793 100644 --- 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 @@ -1,7 +1,7 @@ package com.github.yulichang.wrapper.segments; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.github.yulichang.wrapper.enums.BaseFuncEnum; +import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; /** @@ -86,7 +86,12 @@ public class SelectString implements Select { } @Override - public TableFieldInfo getTableFieldInfo() { + public Class getPropertyType() { + return null; + } + + @Override + public JdbcType getJdbcType() { return null; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectSub.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectSub.java index 8426987..6dd7132 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectSub.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectSub.java @@ -1,8 +1,8 @@ package com.github.yulichang.wrapper.segments; -import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.github.yulichang.wrapper.enums.BaseFuncEnum; +import org.apache.ibatis.type.JdbcType; import org.apache.ibatis.type.TypeHandler; import java.util.Objects; @@ -24,7 +24,7 @@ public class SelectSub implements Select { private final String tagProperty; - public SelectSub(Supplier column, boolean hasTableAlias, String tableAlias,String tagProperty) { + public SelectSub(Supplier column, boolean hasTableAlias, String tableAlias, String tagProperty) { this.column = column; this.hasTableAlias = hasTableAlias; this.tableAlias = tableAlias; @@ -97,7 +97,12 @@ public class SelectSub implements Select { } @Override - public TableFieldInfo getTableFieldInfo() { + public Class getPropertyType() { + return null; + } + + @Override + public JdbcType getJdbcType() { return null; } diff --git a/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/XPluginImpl.java b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/XPluginImpl.java index 4da0704..b384784 100644 --- a/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/XPluginImpl.java +++ b/mybatis-plus-join-solon-plugin/src/main/java/com/github/yulichang/mybatisplusjoin/solon/plugin/XPluginImpl.java @@ -10,6 +10,7 @@ import com.github.yulichang.injector.MPJSqlInjector; import com.github.yulichang.toolkit.SpringContentUtils; import com.github.yulichang.toolkit.reflect.GenericTypeUtils; import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum; +import com.github.yulichang.wrapper.interfaces.MBiPredicate; import org.apache.ibatis.session.SqlSessionFactory; import org.apache.ibatis.solon.MybatisAdapter; import org.apache.ibatis.solon.integration.MybatisAdapterManager; @@ -22,7 +23,6 @@ import org.noear.solon.core.util.GenericUtil; import javax.sql.DataSource; import java.util.*; -import java.util.function.BiPredicate; import java.util.function.Function; import java.util.stream.Collectors; @@ -62,7 +62,7 @@ public class XPluginImpl implements Plugin { ConfigProperties.mappingMaxCount = prop.get("mappingMaxCount", Integer::parseInt); ConfigProperties.ifExists = prop.get("ifExists", val -> Arrays.stream(IfExistsEnum.values()).filter(e -> e.name().equalsIgnoreCase(val)).findFirst() - .map(m -> (BiPredicate) (o, enums) -> m.test(o)) + .map(m -> (MBiPredicate) (o, enums) -> m.test(o)) .orElseThrow(() -> ExceptionUtils.mpe("mybatis-plus-join.ifExists 配置错误"))); // 后续操作 context.onEvent(AppLoadEndEvent.class, e -> { 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 e6038c6..a93cd11 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 @@ -64,14 +64,14 @@ class LambdaWrapperTest { ThreadLocalUtils.set("SELECT t.id, t.user_id, t.tenant_id FROM user_tenant t WHERE t.tenant_id = 1"); MPJLambdaWrapper lambda = JoinWrappers.lambda(UserTenantDO.class) .selectAsClass(UserTenantDO.class, UserTenantDTO.class); - List list = userTenantMapper.selectJoinList(UserTenantDO.class, lambda); + List list = userTenantMapper.selectJoinList(UserTenantDO.class, lambda.clone()); assert list.size() == 5 && list.get(0).getIdea() != null; ThreadLocalUtils.set("SELECT t.tenant_id, t.user_id, t.id FROM user_tenant t WHERE t.tenant_id = 1"); MPJLambdaWrapper lambda1 = JoinWrappers.lambda(UserTenantDO.class) .selectAsClass(UserTenantDO.class, UserTenantDescDTO.class); - List list1 = userTenantMapper.selectJoinList(UserTenantDO.class, lambda1); + List list1 = userTenantMapper.selectJoinList(UserTenantDO.class, lambda1.clone()); assert list1.size() == 5 && list1.get(0).getIdea() != null; } @@ -79,7 +79,7 @@ class LambdaWrapperTest { void testSimple() { MPJLambdaWrapper lambda = JoinWrappers.lambda(UserTenantDO.class); lambda.selectAs(UserTenantDO::getIdea, UserTenantDO::getIdea); - List list = userTenantMapper.selectList(lambda); + List list = userTenantMapper.selectList(lambda.clone()); assert list.size() == 5 && list.get(0).getIdea() != null; } @@ -126,7 +126,7 @@ class LambdaWrapperTest { .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId) .le(UserDO::getId, 10000) .orderByDesc(UserDO::getId); - List list = userMapper.selectJoinList(UserDTO.class, wrapper); + List list = userMapper.selectJoinList(UserDTO.class, wrapper.clone()); assert wrapper.checkJoinTable(AddressDO.class); assert wrapper.checkJoinTable(AreaDO.class); @@ -167,7 +167,7 @@ class LambdaWrapperTest { .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .le(UserDO::getId, 10000) .orderByDesc(UserDO::getId); - List list = userMapper.selectJoinList(UserDTO.class, wrapper); + List list = userMapper.selectJoinList(UserDTO.class, wrapper.clone()); assert list.get(0).getAddressIds() != null; list.forEach(System.out::println); @@ -214,7 +214,7 @@ class LambdaWrapperTest { .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId) .orderByDesc(UserDO::getId); - List list = userMapper.selectJoinList(UserDTO.class, wrapper); + List list = userMapper.selectJoinList(UserDTO.class, wrapper.clone()); assert list.get(0).getAddressList().get(0).getId() != null; list.forEach(System.out::println); @@ -237,7 +237,7 @@ class LambdaWrapperTest { .select(UserDO::getId) .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId); - List list = userMapper.selectJoinList(Integer.class, wrapper); + List list = userMapper.selectJoinList(Integer.class, wrapper.clone()); assert list.get(0) != null; System.out.println(list); @@ -255,7 +255,7 @@ class LambdaWrapperTest { .select(UserDO::getCreateTime) .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId); - List list1 = userMapper.selectJoinList(Timestamp.class, wrapper1); + List list1 = userMapper.selectJoinList(Timestamp.class, wrapper1.clone()); assert list1.get(0) != null; System.out.println(list); @@ -281,9 +281,9 @@ class LambdaWrapperTest { " AND t.del = false\n" + " AND (t.id <= ?)\n" + "ORDER BY t.id ASC, t.`name` ASC"); - MPJLambdaWrapper wrapper = new MPJLambdaWrapper(new UserDO() {{ - setId(1); - }}) + UserDO userDO = new UserDO(); + userDO.setId(1); + MPJLambdaWrapper wrapper = new MPJLambdaWrapper<>(userDO) .selectAll(UserDO.class) // .setEntity(new UserDO() {{ // setId(1); @@ -291,7 +291,7 @@ class LambdaWrapperTest { .le(UserDO::getId, 100) .orderByAsc(UserDO::getId, UserDO::getName); - List list = userMapper.selectList(wrapper); + List list = userMapper.selectList(wrapper.clone()); list.forEach(System.out::println); } @@ -322,7 +322,7 @@ class LambdaWrapperTest { .ge(UserDO::getId, 0)) .leftJoin(UserDO.class, "uc", UserDO::getId, UserDto::getUpdateBy, ext -> ext .selectAs(UserDO::getName, UserDto::getUpdateName)); - List userDtos = userDTOMapper.selectJoinList(UserDto.class, wrapper); + List userDtos = userDTOMapper.selectJoinList(UserDto.class, wrapper.clone()); assert StringUtils.isNotBlank(userDtos.get(0).getUserName()); assert StringUtils.isNotBlank(userDtos.get(0).getCreateName()); assert StringUtils.isNotBlank(userDtos.get(0).getUpdateName()); @@ -360,7 +360,7 @@ class LambdaWrapperTest { .select(UserDO::getImg)) .leftJoin(UserDO.class, "uc", UserDO::getId, UserDO::getUpdateBy) .eq(UserDO::getId, UserDO::getId); - userMapper.selectJoinList(UserDO.class, w); + userMapper.selectJoinList(UserDO.class, w.clone()); System.out.println(1); } @@ -404,7 +404,7 @@ class LambdaWrapperTest { .selectCollection(UserDO.class, UserDO::getChildren) .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId) .gt(UserDO::getId, 0); - List list = userMapper.selectJoinList(UserDO.class, wrapper); + List list = userMapper.selectJoinList(UserDO.class, wrapper.clone()); System.out.println(list); ThreadLocalUtils.set("SELECT t.id,\n" + @@ -438,7 +438,7 @@ class LambdaWrapperTest { .eq(UserDO::getId, UserDO::getUpdateBy); }) .eq(UserDO::getId, UserDO::getId); - List dos = userMapper.selectJoinList(UserDO.class, w); + List dos = userMapper.selectJoinList(UserDO.class, w.clone()); assert dos.get(0).getCreateName() != null && dos.get(0).getUpdateName() != null; @@ -494,7 +494,7 @@ class LambdaWrapperTest { .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId) .le(UserDO::getId, 5)) .le(UserDO::getId, 4); - List list1 = userMapper.selectJoinList(UserDO.class, wrapper1); + List list1 = userMapper.selectJoinList(UserDO.class, wrapper1.clone()); System.out.println(list1); } @@ -503,20 +503,20 @@ class LambdaWrapperTest { */ @Test void testLogicDel() { - List l1 = userMapper.selectJoinList(UserDTO.class, new MPJLambdaWrapper<>()); + List l1 = userMapper.selectJoinList(UserDTO.class, new MPJLambdaWrapper().clone()); 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)); + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId).clone()); 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)); + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId).clone()); assert l3.size() == 14 && l3.get(0).getAddressList().size() == 9; List l4 = userMapper.selectJoinList(UserDTO.class, new MPJLambdaWrapper() @@ -525,7 +525,7 @@ class LambdaWrapperTest { .selectCollection(AddressDO.class, UserDTO::getAddressList) .leftJoin(AddressDO.class, on -> on .eq(AddressDO::getUserId, UserDO::getId) - .eq(AddressDO::getDel, false))); + .eq(AddressDO::getDel, false)).clone()); assert l4.size() == 14 && l4.get(0).getAddressList().size() == 5; } @@ -540,7 +540,7 @@ class LambdaWrapperTest { .selectAll(UserDO.class) .selectCollection(UserDO.class, UserDO::getChildren) .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId); - List list = userMapper.selectJoinList(UserDO.class, wrapper); + List list = userMapper.selectJoinList(UserDO.class, wrapper.clone()); assert list.get(0).getName() != null && list.get(0).getChildren().get(0).getName() != null; assert list.get(0).getImg() != null && list.get(0).getChildren().get(0).getImg() != null; System.out.println(list); @@ -557,7 +557,7 @@ class LambdaWrapperTest { .select(UserDO::getId) .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .orderByDesc(UserDO::getId); - List list = userMapper.selectObjs(wrapper); + List list = userMapper.selectObjs(wrapper.clone()); } @Test @@ -590,7 +590,7 @@ class LambdaWrapperTest { .selectAll(AddressDO.class, "aa") // .selectCollection(UserDO.class, UserDO::getChildren) .leftJoin(AddressDO.class, "aa", AddressDO::getUserId, UserDO::getId); - List list = userMapper.selectJoinList(UserDO.class, wrapper); + List list = userMapper.selectJoinList(UserDO.class, wrapper.clone()); System.out.println(list); } @@ -604,7 +604,7 @@ class LambdaWrapperTest { .selectCollection("t2", AddressDO.class, UserDO::getAddressList2) .leftJoin(AddressDO.class, AddressDO::getId, UserDO::getAddressId) .leftJoin(AddressDO.class, AddressDO::getId, UserDO::getAddressId2); - List list = userMapper.selectJoinList(UserDO.class, wrapper); + List list = userMapper.selectJoinList(UserDO.class, wrapper.clone()); assert list.get(0).getAddressList().get(0).getAddress() != null; assert list.get(0).getAddressList2().get(0).getAddress() != null; @@ -625,7 +625,7 @@ class LambdaWrapperTest { .select(AddressDO::getAddress) .select(AreaDO::getProvince) .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) - .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId)); + .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId).clone()); iPage.getRecords().forEach(System.out::println); } @@ -636,24 +636,24 @@ class LambdaWrapperTest { @Test void test3() { ThreadLocalUtils.set("SELECT t.id,\n" + - " t.pid,\n" + - " t.`name`,\n" + - " t.`json`,\n" + - " t.sex,\n" + - " t.head_img,\n" + - " t.create_time,\n" + - " t.address_id,\n" + - " t.address_id2,\n" + - " t.del,\n" + - " t.create_by,\n" + - " t.update_by,\n" + - " t1.address\n" + - "FROM `user` t\n" + - " LEFT JOIN address t1 ON (t.id = t1.user_id AND t.id = t1.user_id)\n" + - "WHERE t.del = false\n" + - " AND t1.del = false\n" + - " AND (t.id = ? AND (t.head_img = ? OR t1.user_id = ?) AND t.id = ?)\n" + - "LIMIT ?", + " t.pid,\n" + + " t.`name`,\n" + + " t.`json`,\n" + + " t.sex,\n" + + " t.head_img,\n" + + " t.create_time,\n" + + " t.address_id,\n" + + " t.address_id2,\n" + + " t.del,\n" + + " t.create_by,\n" + + " t.update_by,\n" + + " t1.address\n" + + "FROM `user` t\n" + + " LEFT JOIN address t1 ON (t.id = t1.user_id AND t.id = t1.user_id)\n" + + "WHERE t.del = false\n" + + " AND t1.del = false\n" + + " AND (t.id = ? AND (t.head_img = ? OR t1.user_id = ?) AND t.id = ?)\n" + + "LIMIT ?", "SELECT * FROM ( SELECT TMP.*, ROWNUM ROW_ID FROM ( SELECT t.id, t.pid, t.`name`, t.`json`, t.sex, t.head_img, " + "t.create_time, t.address_id, t.address_id2, t.del, t.create_by, t.update_by, t1.address FROM `user` t " + "LEFT JOIN address t1 ON (t.id = t1.user_id AND t.id = t1.user_id) WHERE t.del = false AND t1.del = false AND " + @@ -669,7 +669,7 @@ class LambdaWrapperTest { .and(i -> i.eq(UserDO::getImg, "er") .or() .eq(AddressDO::getUserId, 1)) - .eq(UserDO::getId, 1)); + .eq(UserDO::getId, 1).clone()); page.getRecords().forEach(System.out::println); } @@ -681,7 +681,7 @@ class LambdaWrapperTest { UserDTO one = userMapper.selectJoinOne(UserDTO.class, JoinWrappers.lambda() .selectSum(UserDO::getId) .selectMax(UserDO::getId, UserDTO::getHeadImg) - .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId).clone()); System.out.println(one); } @@ -707,7 +707,7 @@ class LambdaWrapperTest { List> list = userMapper.selectJoinMaps(JoinWrappers.lambda() .selectAll(UserDO.class) .select(AddressDO::getAddress) - .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId)); + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId).clone()); assert list.get(0).get("ADDRESS") != null || list.get(0).get("address") != null; list.forEach(System.out::println); } @@ -719,7 +719,7 @@ class LambdaWrapperTest { void testMP() { List dos = userMapper.selectList(new LambdaQueryWrapper() .gt(UserDO::getId, 3) - .lt(UserDO::getId, 8)); + .lt(UserDO::getId, 8).clone()); assert dos.size() == 4; ThreadLocalUtils.set( @@ -728,7 +728,7 @@ class LambdaWrapperTest { "SELECT * FROM `user` t WHERE t.del=false AND (t.id > ? AND t.id < ?) "); List dos1 = userMapper.selectList(new MPJLambdaWrapper() .gt(UserDO::getId, 3) - .lt(UserDO::getId, 8)); + .lt(UserDO::getId, 8).clone()); assert dos1.size() == 4; } @@ -758,13 +758,13 @@ class LambdaWrapperTest { MPJLambdaWrapper wrapper = new MPJLambdaWrapper() .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId); - Object integer = userMapper.selectCount(wrapper); + Object integer = userMapper.selectCount(wrapper.clone()); ThreadLocalUtils.set("SELECT COUNT( * ) FROM `user` t LEFT JOIN address t1 ON (t1.user_id = t.id) LEFT JOIN area t2 ON (t2.id = t1.area_id) WHERE t.del=false AND t1.del=false AND t2.del=false"); MPJLambdaWrapper wrapper1 = new MPJLambdaWrapper() .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .leftJoin(AreaDO.class, AreaDO::getId, AddressDO::getAreaId); - Long aLong1 = userMapper.selectJoinCount(wrapper1); + Long aLong1 = userMapper.selectJoinCount(wrapper1.clone()); } @@ -786,7 +786,7 @@ class LambdaWrapperTest { .orderByDesc(UserDO::getId) .setTableName(name -> String.format("(select * from %s)", name)); - List list = userMapper.selectJoinList(UserDTO.class, wrapper); + List list = userMapper.selectJoinList(UserDTO.class, wrapper.clone()); } @@ -835,7 +835,7 @@ class LambdaWrapperTest { .le(UserDO::getId, 10000) .orderByDesc(UserDO::getId); System.out.println(wrapper.getFrom()); - List list = userMapper.selectJoinList(UserDTO.class, wrapper); + List list = userMapper.selectJoinList(UserDTO.class, wrapper.clone()); assert list.get(0).getAddressList() != null && list.get(0).getAddressList().get(0).getId() != null; list.forEach(System.out::println); @@ -909,7 +909,7 @@ class LambdaWrapperTest { .le(UserDO::getId, 10000) .orderByDesc(UserDO::getId); - List list = wrapper.list(UserDTO.class); + List list = wrapper.clone().list(UserDTO.class); System.out.println(list); assert list.get(0).getAddressList() != null && list.get(0).getAddressList().get(0).getId() != null; @@ -924,7 +924,7 @@ class LambdaWrapperTest { .leftJoin(UserDO.class, UserDO::getAddressId, AddressDO::getId) .leftJoin(UserDO.class, UserDO::getPid, UserDO::getId); - List addressDOS = wrapper.list(); + List addressDOS = wrapper.clone().list(); } /** @@ -938,7 +938,7 @@ class LambdaWrapperTest { .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) .eq(AddressDO::getId, AddressDO::getId); - List addressDOS = wrapper.list(); + List addressDOS = wrapper.clone().list(); } /** @@ -952,7 +952,7 @@ class LambdaWrapperTest { .leftJoin(AddressDO.class, "aaa", AddressDO::getUserId, UserDO::getId, ext -> ext .eq(AddressDO::getId, AddressDO::getId)) .eq(AddressDO::getId, AddressDO::getId); - List addressDOS = wrapper.list(); + List addressDOS = wrapper.clone().list(); } /** @@ -969,7 +969,7 @@ class LambdaWrapperTest { "SELECT id,user_id,name FROM order_t t"); } MPJLambdaWrapper wrapper = JoinWrappers.lambda(OrderDO.class); - List list = wrapper.list(); + List list = wrapper.clone().list(); if (VersionUtils.compare(MybatisPlusVersion.getVersion(), "3.4.3") >= 0) { ThreadLocalUtils.set("SELECT t.id,t.user_id,t.name,t1.`name` AS userName FROM order_t t LEFT JOIN `user` t1 ON (t1.id = t.user_id) WHERE t1.del=false ORDER BY t.name DESC", @@ -982,7 +982,7 @@ class LambdaWrapperTest { .selectAll(OrderDO.class) .selectAs(UserDO::getName, OrderDO::getUserName) .leftJoin(UserDO.class, UserDO::getId, OrderDO::getUserId); - List l = w.list(); + List l = w.clone().list(); } /** @@ -996,6 +996,6 @@ class LambdaWrapperTest { .le(UserDO::getId, 100) .checkSqlInjection() .orderByDesc("t.id"); - wrapper.list(); + wrapper.clone().list(); } } diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/CloneTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/CloneTest.java new file mode 100644 index 0000000..422d1f9 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/CloneTest.java @@ -0,0 +1,40 @@ +package com.github.yulichang.test.join.unit; + +import com.github.yulichang.test.join.entity.AddressDO; +import com.github.yulichang.test.join.entity.UserDO; +import com.github.yulichang.test.util.Reset; +import com.github.yulichang.toolkit.JoinWrappers; +import com.github.yulichang.wrapper.MPJLambdaWrapper; +import com.github.yulichang.wrapper.segments.Fun; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +@SpringBootTest +public class CloneTest { + + + @BeforeEach + void setUp() { + Reset.reset(); + } + + + @Test + void cloneTest() { + MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class) + .selectAll(UserDO.class) + .leftJoin(AddressDO.class, AddressDO::getUserId, UserDO::getId) + .applyFunc("concat(%s,%s,{0}) is not null", arg -> arg.accept(UserDO::getId, AddressDO::getUserId), "12") + .applyFunc("concat(%s,%s,{0}) is not null", arg -> arg.accept( + Fun.f("t", UserDO::getId), + Fun.f("t1", AddressDO::getUserId)), "12"); + + wrapper.list().forEach(System.out::println); + + MPJLambdaWrapper clone = wrapper.clone(); + + clone.list().forEach(System.out::println); + } + +}