From a3340741f1b3a4d58bb51247790da1f6653904df Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Tue, 13 Aug 2024 14:33:08 +0800 Subject: [PATCH] fix https://github.com/yulichang/mybatis-plus-join/issues/163 --- .../yulichang/adapter/base/IAdapter.java | 26 ++++++++++- .../{AdapterV33x.java => Adapter33x.java} | 14 +++++- .../yulichang/adapter/v3431/Adapter3431.java | 14 ++++++ .../yulichang/adapter/v355/Adapter355.java | 14 ++++++ .../com/github/yulichang/adapter/Adapter.java | 7 --- .../yulichang/adapter/AdapterHelper.java | 4 +- .../extension/apt/AptAbstractWrapper.java | 8 ++-- .../extension/apt/AptQueryWrapper.java | 11 ++--- .../extension/apt/JoinAbstractWrapper.java | 13 +++--- .../yulichang/interceptor/MPJInterceptor.java | 9 +++- .../wrapper/segments/AptConsumer.java | 4 +- .../wrapper/segments/SelectCache.java | 22 ++++------ .../yulichang/test/join/dto/UserDTO.java | 2 +- .../yulichang/test/join/dto/UserJson.java | 12 +++++ .../yulichang/test/join/entity/UserDO.java | 4 +- .../test-join/src/main/resources/db/data.sql | 44 +++++++++---------- .../src/main/resources/db/oracle/data.sql | 44 +++++++++---------- .../src/main/resources/db/pgsql/data.sql | 44 +++++++++---------- .../test/join/apt/unit/ApplyFuncTest.java | 2 + .../test/join/mysql/UpdateJoinTest.java | 16 ++++--- .../test/join/unit/TypeHandlerTest.java | 31 +++++++++++++ 21 files changed, 226 insertions(+), 119 deletions(-) rename mybatis-plus-join-adapter/mybatis-plus-join-adapter-v33x/src/main/java/com/github/yulichang/adapter/v33x/{AdapterV33x.java => Adapter33x.java} (80%) create mode 100644 mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserJson.java create mode 100644 mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/TypeHandlerTest.java diff --git a/mybatis-plus-join-adapter/mybatis-plus-join-adapter-base/src/main/java/com/github/yulichang/adapter/base/IAdapter.java b/mybatis-plus-join-adapter/mybatis-plus-join-adapter-base/src/main/java/com/github/yulichang/adapter/base/IAdapter.java index 8dfc949c..aa1ab1e9 100644 --- a/mybatis-plus-join-adapter/mybatis-plus-join-adapter-base/src/main/java/com/github/yulichang/adapter/base/IAdapter.java +++ b/mybatis-plus-join-adapter/mybatis-plus-join-adapter-base/src/main/java/com/github/yulichang/adapter/base/IAdapter.java @@ -1,9 +1,15 @@ package com.github.yulichang.adapter.base; +import com.baomidou.mybatisplus.core.handlers.IJsonTypeHandler; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.MybatisUtils; import com.github.yulichang.adapter.base.metadata.OrderFieldInfo; +import com.github.yulichang.adapter.jsqlparser.JSqlParserHelper; import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.type.TypeHandler; +import org.apache.ibatis.type.TypeHandlerRegistry; +import org.apache.ibatis.type.UnknownTypeHandler; import java.lang.reflect.Field; import java.util.List; @@ -50,5 +56,23 @@ default List mpjGetOrderField(TableInfo tableInfo) { new OrderFieldInfo(f.getColumn(), f.getType(), f.getSort())).collect(Collectors.toList()); } - void parserColum(String alias, String from, String selectSql, Consumer columConsumer); + default void parserColum(String alias, String from, String selectSql, Consumer columConsumer) { + JSqlParserHelper.parserColum(alias, from, selectSql, columConsumer); + } + + default TypeHandler getTypeHandler(Configuration configuration, Class propertyType, Class> typeHandlerClass, Field field) { + TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry(); + TypeHandler typeHandler = registry.getMappingTypeHandler(typeHandlerClass); + if (typeHandlerClass != null && typeHandlerClass != UnknownTypeHandler.class) { + if (IJsonTypeHandler.class.isAssignableFrom(typeHandlerClass)) { + // 保证每次实例化 + typeHandler = MybatisUtils.newJsonTypeHandler(typeHandlerClass, propertyType, field); + } else { + if (typeHandler == null) { + typeHandler = registry.getInstance(propertyType, typeHandlerClass); + } + } + } + return typeHandler; + } } diff --git a/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v33x/src/main/java/com/github/yulichang/adapter/v33x/AdapterV33x.java b/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v33x/src/main/java/com/github/yulichang/adapter/v33x/Adapter33x.java similarity index 80% rename from mybatis-plus-join-adapter/mybatis-plus-join-adapter-v33x/src/main/java/com/github/yulichang/adapter/v33x/AdapterV33x.java rename to mybatis-plus-join-adapter/mybatis-plus-join-adapter-v33x/src/main/java/com/github/yulichang/adapter/v33x/Adapter33x.java index ce87259f..94023e3c 100644 --- a/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v33x/src/main/java/com/github/yulichang/adapter/v33x/AdapterV33x.java +++ b/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v33x/src/main/java/com/github/yulichang/adapter/v33x/Adapter33x.java @@ -10,6 +10,8 @@ import com.github.yulichang.adapter.base.tookit.VersionUtils; import com.github.yulichang.adapter.jsqlparser.v46.JSqlParserHelperV46; import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.type.TypeHandler; +import org.apache.ibatis.type.TypeHandlerRegistry; import java.lang.reflect.Field; import java.util.List; @@ -21,7 +23,7 @@ * @author yulichang * @since 1.4.3 */ -public class AdapterV33x implements IAdapter { +public class Adapter33x implements IAdapter { private static final boolean is330 = VersionUtils.compare(MybatisPlusVersion.getVersion(), "3.3.0") == 0; @@ -75,4 +77,14 @@ public List mpjGetOrderField(TableInfo tableInfo) { public void parserColum(String alias, String from, String selectSql, Consumer columConsumer) { JSqlParserHelperV46.parserColum(alias, from, selectSql, columConsumer); } + + @Override + public TypeHandler getTypeHandler(Configuration configuration, Class propertyType, Class> typeHandlerClass, Field field) { + TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry(); + TypeHandler typeHandler = registry.getMappingTypeHandler(typeHandlerClass); + if (typeHandler == null) { + typeHandler = registry.getInstance(propertyType, typeHandlerClass); + } + return typeHandler; + } } diff --git a/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v3431/src/main/java/com/github/yulichang/adapter/v3431/Adapter3431.java b/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v3431/src/main/java/com/github/yulichang/adapter/v3431/Adapter3431.java index 8fcc455e..19edb2e3 100644 --- a/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v3431/src/main/java/com/github/yulichang/adapter/v3431/Adapter3431.java +++ b/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v3431/src/main/java/com/github/yulichang/adapter/v3431/Adapter3431.java @@ -10,7 +10,11 @@ import com.github.yulichang.adapter.base.tookit.VersionUtils; import com.github.yulichang.adapter.jsqlparser.v46.JSqlParserHelperV46; import lombok.AllArgsConstructor; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.type.TypeHandler; +import org.apache.ibatis.type.TypeHandlerRegistry; +import java.lang.reflect.Field; import java.util.List; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -46,4 +50,14 @@ public List mpjGetOrderField(TableInfo tableInfo) { public void parserColum(String alias, String from, String selectSql, Consumer columConsumer) { JSqlParserHelperV46.parserColum(alias, from, selectSql, columConsumer); } + + @Override + public TypeHandler getTypeHandler(Configuration configuration, Class propertyType, Class> typeHandlerClass, Field field) { + TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry(); + TypeHandler typeHandler = registry.getMappingTypeHandler(typeHandlerClass); + if (typeHandler == null) { + typeHandler = registry.getInstance(propertyType, typeHandlerClass); + } + return typeHandler; + } } diff --git a/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v355/src/main/java/com/github/yulichang/adapter/v355/Adapter355.java b/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v355/src/main/java/com/github/yulichang/adapter/v355/Adapter355.java index 04704a0f..ed3d4125 100644 --- a/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v355/src/main/java/com/github/yulichang/adapter/v355/Adapter355.java +++ b/mybatis-plus-join-adapter/mybatis-plus-join-adapter-v355/src/main/java/com/github/yulichang/adapter/v355/Adapter355.java @@ -2,7 +2,11 @@ import com.github.yulichang.adapter.base.IAdapter; import com.github.yulichang.adapter.jsqlparser.v46.JSqlParserHelperV46; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.type.TypeHandler; +import org.apache.ibatis.type.TypeHandlerRegistry; +import java.lang.reflect.Field; import java.util.function.Consumer; /** @@ -15,4 +19,14 @@ public class Adapter355 implements IAdapter { public void parserColum(String alias, String from, String selectSql, Consumer columConsumer) { JSqlParserHelperV46.parserColum(alias, from, selectSql, columConsumer); } + + @Override + public TypeHandler getTypeHandler(Configuration configuration, Class propertyType, Class> typeHandlerClass, Field field) { + TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry(); + TypeHandler typeHandler = registry.getMappingTypeHandler(typeHandlerClass); + if (typeHandler == null) { + typeHandler = registry.getInstance(propertyType, typeHandlerClass); + } + return typeHandler; + } } \ No newline at end of file diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/adapter/Adapter.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/adapter/Adapter.java index 0d685d67..30b9569c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/adapter/Adapter.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/adapter/Adapter.java @@ -1,9 +1,6 @@ package com.github.yulichang.adapter; import com.github.yulichang.adapter.base.IAdapter; -import com.github.yulichang.adapter.jsqlparser.JSqlParserHelper; - -import java.util.function.Consumer; /** * @author yulichang @@ -11,8 +8,4 @@ */ public class Adapter implements IAdapter { - @Override - public void parserColum(String alias, String from, String selectSql, Consumer columConsumer) { - JSqlParserHelper.parserColum(alias, from, selectSql, columConsumer); - } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/adapter/AdapterHelper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/adapter/AdapterHelper.java index c697c1f9..7ae98652 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/adapter/AdapterHelper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/adapter/AdapterHelper.java @@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils; import com.github.yulichang.adapter.base.IAdapter; import com.github.yulichang.adapter.base.tookit.VersionUtils; -import com.github.yulichang.adapter.v33x.AdapterV33x; +import com.github.yulichang.adapter.v33x.Adapter33x; import com.github.yulichang.adapter.v3431.Adapter3431; import com.github.yulichang.adapter.v355.Adapter355; import lombok.Getter; @@ -32,7 +32,7 @@ public class AdapterHelper { } else if (VersionUtils.compare(version, "3.4.0") >= 0) { adapter = new Adapter3431(); } else if (VersionUtils.compare(version, "3.3.0") >= 0) { - adapter = new AdapterV33x(); + adapter = new Adapter33x(); } else { throw ExceptionUtils.mpe("MPJ需要MP版本3.3.0+,当前MP版本%s", version); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/AptAbstractWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/AptAbstractWrapper.java index 71d340d3..9f726c42 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/AptAbstractWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/AptAbstractWrapper.java @@ -7,17 +7,18 @@ import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.StringPool; import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.adapter.AdapterHelper; import com.github.yulichang.apt.BaseColumn; import com.github.yulichang.apt.Column; import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.config.enums.LogicDelTypeEnum; +import com.github.yulichang.extension.apt.interfaces.QueryJoin; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LogicInfoUtils; import com.github.yulichang.toolkit.TableHelper; import com.github.yulichang.toolkit.TableMap; import com.github.yulichang.toolkit.support.ColumnCache; -import com.github.yulichang.extension.apt.interfaces.QueryJoin; import com.github.yulichang.wrapper.interfaces.MFunction; import com.github.yulichang.wrapper.segments.SelectCache; import lombok.Getter; @@ -27,7 +28,6 @@ import java.nio.charset.StandardCharsets; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.Function; import java.util.stream.Collectors; import static com.baomidou.mybatisplus.core.enums.WrapperKeyword.APPLY; @@ -73,7 +73,7 @@ public abstract class AptAbstractWrapper tableFunc; + protected SFunction tableFunc; /** * 逻辑删除位置 @@ -140,7 +140,7 @@ protected AptAbstractWrapper(BaseColumn baseColumn, T entity) { * * @return 自定义表别名 */ - public Children setTableName(Function 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/extension/apt/AptQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/AptQueryWrapper.java index 00feb9ca..6d8250a8 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/AptQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/AptQueryWrapper.java @@ -8,15 +8,17 @@ import com.github.yulichang.apt.BaseColumn; import com.github.yulichang.apt.Column; import com.github.yulichang.config.ConfigProperties; +import com.github.yulichang.extension.apt.interfaces.Query; +import com.github.yulichang.extension.apt.interfaces.QueryLabel; import com.github.yulichang.extension.apt.toolkit.AptWrapperUtils; import com.github.yulichang.extension.apt.toolkit.AptWrappers; +import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; -import com.github.yulichang.toolkit.*; +import com.github.yulichang.toolkit.TableMap; import com.github.yulichang.toolkit.support.ColumnCache; -import com.github.yulichang.extension.apt.interfaces.Query; -import com.github.yulichang.extension.apt.interfaces.QueryLabel; import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum; import com.github.yulichang.wrapper.interfaces.Chain; +import com.github.yulichang.wrapper.interfaces.MBiPredicate; import com.github.yulichang.wrapper.interfaces.SelectWrapper; import com.github.yulichang.wrapper.resultmap.Label; import com.github.yulichang.wrapper.segments.Select; @@ -27,7 +29,6 @@ import java.util.*; import java.util.concurrent.atomic.AtomicInteger; -import java.util.function.BiPredicate; import java.util.function.Consumer; import java.util.stream.Collectors; @@ -97,7 +98,7 @@ protected AptQueryWrapper(T entity, BaseColumn baseColumn, SharedString sqlSe Map paramNameValuePairs, MergeSegments mergeSegments, SharedString paramAlias, SharedString lastSql, SharedString sqlComment, SharedString sqlFirst, TableMap aptIndex, Integer index, String keyWord, Class joinClass, String tableName, - BiPredicate IfExists) { + MBiPredicate IfExists) { super(baseColumn); super.setEntity(entity); super.setEntityClass(baseColumn.getColumnClass()); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/JoinAbstractWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/JoinAbstractWrapper.java index 5e3e5a50..573d8fd0 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/JoinAbstractWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/extension/apt/JoinAbstractWrapper.java @@ -22,10 +22,7 @@ import com.github.yulichang.extension.apt.interfaces.Func; import com.github.yulichang.extension.apt.interfaces.OnCompare; import com.github.yulichang.wrapper.enums.IfExistsSqlKeyWordEnum; -import com.github.yulichang.wrapper.interfaces.CompareStrIfExists; -import com.github.yulichang.wrapper.interfaces.DoSomething; -import com.github.yulichang.wrapper.interfaces.FuncStr; -import com.github.yulichang.wrapper.interfaces.Join; +import com.github.yulichang.wrapper.interfaces.*; import com.github.yulichang.wrapper.segments.AptConsumer; import lombok.Getter; @@ -113,7 +110,7 @@ public abstract class JoinAbstractWrapper ifExists = ConfigProperties.ifExists; + protected MBiPredicate ifExists = ConfigProperties.ifExists; @Override public T getEntity() { @@ -163,7 +160,7 @@ public Children checkSqlInjection() { return typedThis; } - public Children setIfExists(BiPredicate IfExists) { + public Children setIfExists(MBiPredicate IfExists) { this.ifExists = IfExists; return typedThis; } @@ -283,12 +280,12 @@ public Children apply(boolean condition, String applySql, Object... values) { () -> 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); } public Children applyFunc(boolean condition, String applySql, - Function consumerFunction, Object... values) { + SFunction consumerFunction, Object... values) { return maybeDo(condition, () -> appendSqlSegments(APPLY, () -> formatSqlMaybeWithParam(String.format(applySql, Arrays.stream(consumerFunction.apply(AptConsumer.func)).map(this::columnToString).toArray()), null, values))); 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 8059324a..2279deab 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 @@ -190,8 +190,13 @@ private List buildResultMap(MappedStatement ms, Class resultType, } private ResultMapping selectToResult(Class entity, Select select, Class type, ResultMapping.Builder builder) { - if (select.hasTypeHandle() && select.getPropertyType().isAssignableFrom(type)) { - builder.typeHandler(select.getTypeHandle()); + if (select.hasTypeHandle()) { + if (select.getPropertyType().isAssignableFrom(type)) { + builder.typeHandler(select.getTypeHandle()); + } else { + throw new ClassCastException(String.format("%s not cast to %s [%s]", + select.getPropertyType().getSimpleName(), type.getSimpleName(), entity.getName() + "." + select.getColumProperty())); + } } if (select.isPk() && entity == select.getClazz()) { builder.flags(Collections.singletonList(ResultFlag.ID)); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/AptConsumer.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/AptConsumer.java index 8f74da67..93a75cf7 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/AptConsumer.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/AptConsumer.java @@ -2,6 +2,8 @@ import com.github.yulichang.apt.Column; +import java.io.Serializable; + /** * 用于selectFunc 和 applyFunc中的参数填充 * 从原来的 {@link SelectFunc} 里的内部类中提取出来 @@ -9,7 +11,7 @@ * @author yulichang * @since 1.4.13 */ -public class AptConsumer { +public class AptConsumer implements Serializable { public static final AptConsumer func = new AptConsumer(); 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 eb0db70a..cc6a3564 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 @@ -5,15 +5,15 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.github.yulichang.adapter.AdapterHelper; import com.github.yulichang.toolkit.MPJStringUtils; +import com.github.yulichang.toolkit.ReflectionKit; 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.lang.reflect.Field; import java.util.Map; import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; @@ -99,31 +99,25 @@ public SelectCache(Class clazz, boolean isPk, String column, Class columnT public TypeHandler getTypeHandler() { if (this.hasTypeHandle) { - return Cache.getTypeHandlerCache(this.clazz, this.typeHandlerClass, this.propertyType); + return Cache.getTypeHandlerCache(this.clazz, this.typeHandlerClass, this.propertyType, this.columProperty); } return null; } - private static TypeHandler getTypeHandler(Configuration configuration, Class propertyType, Class> typeHandlerClass) { - TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry(); - TypeHandler typeHandler = registry.getMappingTypeHandler(typeHandlerClass); - if (typeHandler == null) { - 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) { + public static TypeHandler getTypeHandlerCache(Class table, Class> typeHandler, Class propertyType, String columProperty) { 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); + @SuppressWarnings("OptionalGetWithoutIsPresent") + TableFieldInfo fieldInfo = info.getFieldList().stream().filter(f -> f.getProperty().equals(columProperty)).findFirst().get(); + Field field = AdapterHelper.getAdapter().mpjGetField(fieldInfo, () -> ReflectionKit.getFieldMap(table).get(columProperty)); + return AdapterHelper.getAdapter().getTypeHandler(AdapterHelper.getAdapter().mpjGetConfiguration(info), propertyType, typeHandler, field); }); } } 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 32457b29..c75b2ba9 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 @@ -20,7 +20,7 @@ public class UserDTO { /** user */ private String PName; /** user */ - private Map json; + private List json; /** user */ private Sex sex; /** user */ diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserJson.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserJson.java new file mode 100644 index 00000000..39cd5bf0 --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserJson.java @@ -0,0 +1,12 @@ +package com.github.yulichang.test.join.dto; + +import lombok.Data; + +import java.io.Serializable; + +@Data +public class UserJson implements Serializable { + + private Long id; + private String name; +} 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 e013528f..a95e8190 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 @@ -6,6 +6,7 @@ import com.baomidou.mybatisplus.extension.handlers.JacksonTypeHandler; import com.github.yulichang.annotation.DynamicTableName; import com.github.yulichang.annotation.Table; +import com.github.yulichang.test.join.dto.UserJson; import com.github.yulichang.test.join.enums.Sex; import lombok.Data; import lombok.EqualsAndHashCode; @@ -16,7 +17,6 @@ import java.io.Serializable; import java.time.LocalDateTime; import java.util.List; -import java.util.Map; @Table @Data @@ -34,7 +34,7 @@ public class UserDO extends ID implements Serializable { private String name; @TableField(value = "`json`", typeHandler = JacksonTypeHandler.class) - private Map json; + private List json; private Sex sex; 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 f0b22313..999c4251 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 @@ -30,28 +30,28 @@ INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北 DELETE FROM `user`; INSERT INTO `user` (id, pid, `name`, `json`, `address_id`, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES -( 1, 1, '张三 1', '{"id": 1,"name":"张三 1"}', 1, 2, 1, 'https://url-01', '2022-01-01 12:00:00', 1, 2, false), -( 2, 1, '张三 2', '{"id": 2,"name":"张三 2"}', 1, 2, 0, 'https://url-02', '2022-01-01 12:00:00', 2, 3, false), -( 3, 1, '张三 3', '{"id": 3,"name":"张三 3"}', 1, 2, 0, 'https://url-03', '2022-01-01 12:00:00', 3, 2, false), -( 4, 1, '张三 4', '{"id": 4,"name":"张三 4"}', 1, 2, 0, 'https://url-04', '2022-01-01 12:00:00', 9, 2, false), -( 5, 1, '张三 5', '{"id": 5,"name":"张三 5"}', 1, 2, 0, 'https://url-05', '2022-01-01 12:00:00', 1, 2, false), -( 6, 1, '张三 6', '{"id": 6,"name":"张三 6"}', 1, 2, 0, 'https://url-06', '2022-01-01 12:00:00', 1, 2, false), -( 7, 1, '张三 7', '{"id": 7,"name":"张三 7"}', 1, 2, 0, 'https://url-07', '2022-01-01 12:00:00', 1, 2, false), -( 8, 1, '张三 8', '{"id": 8,"name":"张三 8"}', 1, 2, 0, 'https://url-08', '2022-01-01 12:00:00', 1, 2, false), -( 9, 1, '张三 9', '{"id": 9,"name":"张三 9"}', 1, 2, 0, 'https://url-09', '2022-01-01 12:00:00', 1, 2, false), -(10, 1, '张三10', '{"id":10,"name":"张三10"}', 1, 2, 0, 'https://url-10', '2022-01-01 12:00:00', 1, 2, true ), -(11, 1, '张三11', '{"id":11,"name":"张三11"}', 1, 2, 0, 'https://url-11', '2022-01-01 12:00:00', 1, 2, true ), -(12, 1, '张三12', '{"id":12,"name":"张三12"}', 1, 2, 0, 'https://url-12', '2022-01-01 12:00:00', 1, 2, true ), -(13, 1, '张三13', '{"id":13,"name":"张三13"}', 1, 2, 0, 'https://url-13', '2022-01-01 12:00:00', 1, 2, true ), -(14, 2, '张三14', '{"id":14,"name":"张三14"}', 1, 2, 0, 'https://url-14', '2022-01-01 12:00:00', 1, 2, false), -(15, 2, '张三15', '{"id":15,"name":"张三15"}', 1, 2, 0, 'https://url-15', '2022-01-01 12:00:00', 1, 2, false), -(16, 2, '张三16', '{"id":16,"name":"张三16"}', 1, 2, 0, 'https://url-16', '2022-01-01 12:00:00', 1, 2, false), -(17, 2, '张三17', '{"id":17,"name":"张三17"}', 1, 2, 0, 'https://url-17', '2022-01-01 12:00:00', 1, 2, false), -(18, 2, '张三18', '{"id":18,"name":"张三18"}', 1, 2, 0, 'https://url-18', '2022-01-01 12:00:00', 1, 2, false), -(19, 2, '张三19', '{"id":19,"name":"张三19"}', 1, 2, 0, 'https://url-19', '2022-01-01 12:00:00', 1, 2, true ), -(20, 2, '张三20', '{"id":20,"name":"张三20"}', 1, 2, 0, 'https://url-20', '2022-01-01 12:00:00', 1, 2, true ), -(21, 2, '张三21', '{"id":21,"name":"张三21"}', 1, 2, 0, 'https://url-21', '2022-01-01 12:00:00', 1, 2, true ), -(22, 2, '张三22', '{"id":22,"name":"张三22"}', 1, 2, 0, 'https://url-22', '2022-01-01 12:00:00', 1, 2, true ); +( 1, 1, '张三 1', '[{"id": 1,"name":"张三 1"}]', 1, 2, 1, 'https://url-01', '2022-01-01 12:00:00', 1, 2, false), +( 2, 1, '张三 2', '[{"id": 2,"name":"张三 2"}]', 1, 2, 0, 'https://url-02', '2022-01-01 12:00:00', 2, 3, false), +( 3, 1, '张三 3', '[{"id": 3,"name":"张三 3"}]', 1, 2, 0, 'https://url-03', '2022-01-01 12:00:00', 3, 2, false), +( 4, 1, '张三 4', '[{"id": 4,"name":"张三 4"}]', 1, 2, 0, 'https://url-04', '2022-01-01 12:00:00', 9, 2, false), +( 5, 1, '张三 5', '[{"id": 5,"name":"张三 5"}]', 1, 2, 0, 'https://url-05', '2022-01-01 12:00:00', 1, 2, false), +( 6, 1, '张三 6', '[{"id": 6,"name":"张三 6"}]', 1, 2, 0, 'https://url-06', '2022-01-01 12:00:00', 1, 2, false), +( 7, 1, '张三 7', '[{"id": 7,"name":"张三 7"}]', 1, 2, 0, 'https://url-07', '2022-01-01 12:00:00', 1, 2, false), +( 8, 1, '张三 8', '[{"id": 8,"name":"张三 8"}]', 1, 2, 0, 'https://url-08', '2022-01-01 12:00:00', 1, 2, false), +( 9, 1, '张三 9', '[{"id": 9,"name":"张三 9"}]', 1, 2, 0, 'https://url-09', '2022-01-01 12:00:00', 1, 2, false), +(10, 1, '张三10', '[{"id":10,"name":"张三10"}]', 1, 2, 0, 'https://url-10', '2022-01-01 12:00:00', 1, 2, true ), +(11, 1, '张三11', '[{"id":11,"name":"张三11"}]', 1, 2, 0, 'https://url-11', '2022-01-01 12:00:00', 1, 2, true ), +(12, 1, '张三12', '[{"id":12,"name":"张三12"}]', 1, 2, 0, 'https://url-12', '2022-01-01 12:00:00', 1, 2, true ), +(13, 1, '张三13', '[{"id":13,"name":"张三13"}]', 1, 2, 0, 'https://url-13', '2022-01-01 12:00:00', 1, 2, true ), +(14, 2, '张三14', '[{"id":14,"name":"张三14"}]', 1, 2, 0, 'https://url-14', '2022-01-01 12:00:00', 1, 2, false), +(15, 2, '张三15', '[{"id":15,"name":"张三15"}]', 1, 2, 0, 'https://url-15', '2022-01-01 12:00:00', 1, 2, false), +(16, 2, '张三16', '[{"id":16,"name":"张三16"}]', 1, 2, 0, 'https://url-16', '2022-01-01 12:00:00', 1, 2, false), +(17, 2, '张三17', '[{"id":17,"name":"张三17"}]', 1, 2, 0, 'https://url-17', '2022-01-01 12:00:00', 1, 2, false), +(18, 2, '张三18', '[{"id":18,"name":"张三18"}]', 1, 2, 0, 'https://url-18', '2022-01-01 12:00:00', 1, 2, false), +(19, 2, '张三19', '[{"id":19,"name":"张三19"}]', 1, 2, 0, 'https://url-19', '2022-01-01 12:00:00', 1, 2, true ), +(20, 2, '张三20', '[{"id":20,"name":"张三20"}]', 1, 2, 0, 'https://url-20', '2022-01-01 12:00:00', 1, 2, true ), +(21, 2, '张三21', '[{"id":21,"name":"张三21"}]', 1, 2, 0, 'https://url-21', '2022-01-01 12:00:00', 1, 2, true ), +(22, 2, '张三22', '[{"id":22,"name":"张三22"}]', 1, 2, 0, 'https://url-22', '2022-01-01 12:00:00', 1, 2, true ); DELETE FROM address; diff --git a/mybatis-plus-join-test/test-join/src/main/resources/db/oracle/data.sql b/mybatis-plus-join-test/test-join/src/main/resources/db/oracle/data.sql index 852bca6c..1cdf40cb 100644 --- a/mybatis-plus-join-test/test-join/src/main/resources/db/oracle/data.sql +++ b/mybatis-plus-join-test/test-join/src/main/resources/db/oracle/data.sql @@ -29,28 +29,28 @@ INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北 DELETE FROM "user"; -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 1, 1, '张三 1', '{"id": 1,"name":"张三 1"}', 1, 2, 1, 'https://url-01', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 2, 1, '张三 2', '{"id": 2,"name":"张三 2"}', 1, 2, 0, 'https://url-02', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 2, 3, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 3, 1, '张三 3', '{"id": 3,"name":"张三 3"}', 1, 2, 0, 'https://url-03', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 3, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 4, 1, '张三 4', '{"id": 4,"name":"张三 4"}', 1, 2, 0, 'https://url-04', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 9, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 5, 1, '张三 5', '{"id": 5,"name":"张三 5"}', 1, 2, 0, 'https://url-05', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 6, 1, '张三 6', '{"id": 6,"name":"张三 6"}', 1, 2, 0, 'https://url-06', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 7, 1, '张三 7', '{"id": 7,"name":"张三 7"}', 1, 2, 0, 'https://url-07', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 8, 1, '张三 8', '{"id": 8,"name":"张三 8"}', 1, 2, 0, 'https://url-08', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 9, 1, '张三 9', '{"id": 9,"name":"张三 9"}', 1, 2, 0, 'https://url-09', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (10, 1, '张三10', '{"id":10,"name":"张三10"}', 1, 2, 0, 'https://url-10', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (11, 1, '张三11', '{"id":11,"name":"张三11"}', 1, 2, 0, 'https://url-11', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (12, 1, '张三12', '{"id":12,"name":"张三12"}', 1, 2, 0, 'https://url-12', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (13, 1, '张三13', '{"id":13,"name":"张三13"}', 1, 2, 0, 'https://url-13', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (14, 2, '张三14', '{"id":14,"name":"张三14"}', 1, 2, 0, 'https://url-14', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (15, 2, '张三15', '{"id":15,"name":"张三15"}', 1, 2, 0, 'https://url-15', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (16, 2, '张三16', '{"id":16,"name":"张三16"}', 1, 2, 0, 'https://url-16', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (17, 2, '张三17', '{"id":17,"name":"张三17"}', 1, 2, 0, 'https://url-17', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (18, 2, '张三18', '{"id":18,"name":"张三18"}', 1, 2, 0, 'https://url-18', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (19, 2, '张三19', '{"id":19,"name":"张三19"}', 1, 2, 0, 'https://url-19', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (20, 2, '张三20', '{"id":20,"name":"张三20"}', 1, 2, 0, 'https://url-20', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (21, 2, '张三21', '{"id":21,"name":"张三21"}', 1, 2, 0, 'https://url-21', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); -INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (22, 2, '张三22', '{"id":22,"name":"张三22"}', 1, 2, 0, 'https://url-22', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 1, 1, '张三 1', '[{"id": 1,"name":"张三 1"}]', 1, 2, 1, 'https://url-01', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 2, 1, '张三 2', '[{"id": 2,"name":"张三 2"}]', 1, 2, 0, 'https://url-02', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 2, 3, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 3, 1, '张三 3', '[{"id": 3,"name":"张三 3"}]', 1, 2, 0, 'https://url-03', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 3, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 4, 1, '张三 4', '[{"id": 4,"name":"张三 4"}]', 1, 2, 0, 'https://url-04', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 9, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 5, 1, '张三 5', '[{"id": 5,"name":"张三 5"}]', 1, 2, 0, 'https://url-05', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 6, 1, '张三 6', '[{"id": 6,"name":"张三 6"}]', 1, 2, 0, 'https://url-06', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 7, 1, '张三 7', '[{"id": 7,"name":"张三 7"}]', 1, 2, 0, 'https://url-07', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 8, 1, '张三 8', '[{"id": 8,"name":"张三 8"}]', 1, 2, 0, 'https://url-08', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES ( 9, 1, '张三 9', '[{"id": 9,"name":"张三 9"}]', 1, 2, 0, 'https://url-09', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (10, 1, '张三10', '[{"id":10,"name":"张三10"}]', 1, 2, 0, 'https://url-10', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (11, 1, '张三11', '[{"id":11,"name":"张三11"}]', 1, 2, 0, 'https://url-11', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (12, 1, '张三12', '[{"id":12,"name":"张三12"}]', 1, 2, 0, 'https://url-12', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (13, 1, '张三13', '[{"id":13,"name":"张三13"}]', 1, 2, 0, 'https://url-13', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (14, 2, '张三14', '[{"id":14,"name":"张三14"}]', 1, 2, 0, 'https://url-14', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (15, 2, '张三15', '[{"id":15,"name":"张三15"}]', 1, 2, 0, 'https://url-15', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (16, 2, '张三16', '[{"id":16,"name":"张三16"}]', 1, 2, 0, 'https://url-16', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (17, 2, '张三17', '[{"id":17,"name":"张三17"}]', 1, 2, 0, 'https://url-17', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (18, 2, '张三18', '[{"id":18,"name":"张三18"}]', 1, 2, 0, 'https://url-18', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, false); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (19, 2, '张三19', '[{"id":19,"name":"张三19"}]', 1, 2, 0, 'https://url-19', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (20, 2, '张三20', '[{"id":20,"name":"张三20"}]', 1, 2, 0, 'https://url-20', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (21, 2, '张三21', '[{"id":21,"name":"张三21"}]', 1, 2, 0, 'https://url-21', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); +INSERT INTO "user" (id, pid, "name", "json", address_id, address_id2 , sex, head_img, create_time, create_by, update_by, del) VALUES (22, 2, '张三22', '[{"id":22,"name":"张三22"}]', 1, 2, 0, 'https://url-22', TO_TIMESTAMP('2022-01-01 12:00:00', 'YYYY-MM-DD HH24:MI:SS'), 1, 2, true ); DELETE FROM address; diff --git a/mybatis-plus-join-test/test-join/src/main/resources/db/pgsql/data.sql b/mybatis-plus-join-test/test-join/src/main/resources/db/pgsql/data.sql index fe859390..244f8cc0 100644 --- a/mybatis-plus-join-test/test-join/src/main/resources/db/pgsql/data.sql +++ b/mybatis-plus-join-test/test-join/src/main/resources/db/pgsql/data.sql @@ -29,28 +29,28 @@ INSERT INTO area (id, province, city, area, postcode, del) VALUES (10022, '北 DELETE FROM "user"; -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 1, 1, '张三 1', '{"id": 1,"name":"张三 1"}', 1, 2, 1, 'https://url-01', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 2, 1, '张三 2', '{"id": 2,"name":"张三 2"}', 1, 2, 0, 'https://url-02', '2022-01-01 12:00:00', 2, 3, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 3, 1, '张三 3', '{"id": 3,"name":"张三 3"}', 1, 2, 0, 'https://url-03', '2022-01-01 12:00:00', 3, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 4, 1, '张三 4', '{"id": 4,"name":"张三 4"}', 1, 2, 0, 'https://url-04', '2022-01-01 12:00:00', 9, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 5, 1, '张三 5', '{"id": 5,"name":"张三 5"}', 1, 2, 0, 'https://url-05', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 6, 1, '张三 6', '{"id": 6,"name":"张三 6"}', 1, 2, 0, 'https://url-06', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 7, 1, '张三 7', '{"id": 7,"name":"张三 7"}', 1, 2, 0, 'https://url-07', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 8, 1, '张三 8', '{"id": 8,"name":"张三 8"}', 1, 2, 0, 'https://url-08', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 9, 1, '张三 9', '{"id": 9,"name":"张三 9"}', 1, 2, 0, 'https://url-09', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (10, 1, '张三10', '{"id":10,"name":"张三10"}', 1, 2, 0, 'https://url-10', '2022-01-01 12:00:00', 1, 2, true ); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (11, 1, '张三11', '{"id":11,"name":"张三11"}', 1, 2, 0, 'https://url-11', '2022-01-01 12:00:00', 1, 2, true ); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (12, 1, '张三12', '{"id":12,"name":"张三12"}', 1, 2, 0, 'https://url-12', '2022-01-01 12:00:00', 1, 2, true ); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (13, 1, '张三13', '{"id":13,"name":"张三13"}', 1, 2, 0, 'https://url-13', '2022-01-01 12:00:00', 1, 2, true ); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (14, 2, '张三14', '{"id":14,"name":"张三14"}', 1, 2, 0, 'https://url-14', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (15, 2, '张三15', '{"id":15,"name":"张三15"}', 1, 2, 0, 'https://url-15', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (16, 2, '张三16', '{"id":16,"name":"张三16"}', 1, 2, 0, 'https://url-16', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (17, 2, '张三17', '{"id":17,"name":"张三17"}', 1, 2, 0, 'https://url-17', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (18, 2, '张三18', '{"id":18,"name":"张三18"}', 1, 2, 0, 'https://url-18', '2022-01-01 12:00:00', 1, 2, false); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (19, 2, '张三19', '{"id":19,"name":"张三19"}', 1, 2, 0, 'https://url-19', '2022-01-01 12:00:00', 1, 2, true ); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (20, 2, '张三20', '{"id":20,"name":"张三20"}', 1, 2, 0, 'https://url-20', '2022-01-01 12:00:00', 1, 2, true ); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (21, 2, '张三21', '{"id":21,"name":"张三21"}', 1, 2, 0, 'https://url-21', '2022-01-01 12:00:00', 1, 2, true ); -INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (22, 2, '张三22', '{"id":22,"name":"张三22"}', 1, 2, 0, 'https://url-22', '2022-01-01 12:00:00', 1, 2, true ); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 1, 1, '张三 1', '[{"id": 1,"name":"张三 1"}]', 1, 2, 1, 'https://url-01', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 2, 1, '张三 2', '[{"id": 2,"name":"张三 2"}]', 1, 2, 0, 'https://url-02', '2022-01-01 12:00:00', 2, 3, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 3, 1, '张三 3', '[{"id": 3,"name":"张三 3"}]', 1, 2, 0, 'https://url-03', '2022-01-01 12:00:00', 3, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 4, 1, '张三 4', '[{"id": 4,"name":"张三 4"}]', 1, 2, 0, 'https://url-04', '2022-01-01 12:00:00', 9, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 5, 1, '张三 5', '[{"id": 5,"name":"张三 5"}]', 1, 2, 0, 'https://url-05', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 6, 1, '张三 6', '[{"id": 6,"name":"张三 6"}]', 1, 2, 0, 'https://url-06', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 7, 1, '张三 7', '[{"id": 7,"name":"张三 7"}]', 1, 2, 0, 'https://url-07', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 8, 1, '张三 8', '[{"id": 8,"name":"张三 8"}]', 1, 2, 0, 'https://url-08', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES ( 9, 1, '张三 9', '[{"id": 9,"name":"张三 9"}]', 1, 2, 0, 'https://url-09', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (10, 1, '张三10', '[{"id":10,"name":"张三10"}]', 1, 2, 0, 'https://url-10', '2022-01-01 12:00:00', 1, 2, true ); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (11, 1, '张三11', '[{"id":11,"name":"张三11"}]', 1, 2, 0, 'https://url-11', '2022-01-01 12:00:00', 1, 2, true ); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (12, 1, '张三12', '[{"id":12,"name":"张三12"}]', 1, 2, 0, 'https://url-12', '2022-01-01 12:00:00', 1, 2, true ); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (13, 1, '张三13', '[{"id":13,"name":"张三13"}]', 1, 2, 0, 'https://url-13', '2022-01-01 12:00:00', 1, 2, true ); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (14, 2, '张三14', '[{"id":14,"name":"张三14"}]', 1, 2, 0, 'https://url-14', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (15, 2, '张三15', '[{"id":15,"name":"张三15"}]', 1, 2, 0, 'https://url-15', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (16, 2, '张三16', '[{"id":16,"name":"张三16"}]', 1, 2, 0, 'https://url-16', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (17, 2, '张三17', '[{"id":17,"name":"张三17"}]', 1, 2, 0, 'https://url-17', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (18, 2, '张三18', '[{"id":18,"name":"张三18"}]', 1, 2, 0, 'https://url-18', '2022-01-01 12:00:00', 1, 2, false); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (19, 2, '张三19', '[{"id":19,"name":"张三19"}]', 1, 2, 0, 'https://url-19', '2022-01-01 12:00:00', 1, 2, true ); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (20, 2, '张三20', '[{"id":20,"name":"张三20"}]', 1, 2, 0, 'https://url-20', '2022-01-01 12:00:00', 1, 2, true ); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (21, 2, '张三21', '[{"id":21,"name":"张三21"}]', 1, 2, 0, 'https://url-21', '2022-01-01 12:00:00', 1, 2, true ); +INSERT INTO "user" (id, "pid", "name", "json", "address_id", "address_id2" , sex, head_img, create_time, create_by, update_by, del) VALUES (22, 2, '张三22', '[{"id":22,"name":"张三22"}]', 1, 2, 0, 'https://url-22', '2022-01-01 12:00:00', 1, 2, true ); DELETE FROM address; diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/apt/unit/ApplyFuncTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/apt/unit/ApplyFuncTest.java index 4822a45d..302236f0 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/apt/unit/ApplyFuncTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/apt/unit/ApplyFuncTest.java @@ -37,6 +37,7 @@ void applyFunc() { .leftJoin(addr, addr.userId, u.id) .applyFunc("concat(%s,%s,{0}) is not null", arg -> arg.accept(u.id, addr.userId), "12") .applyFunc("concat(%s,%s,{0}) is not null", arg -> arg.accept(u.id, addr.userId), "12") + .clone() .list(); list.forEach(System.out::println); @@ -55,6 +56,7 @@ void applyFunc() { .leftJoin(addr1, addr1.userId, u1.id) .applyFunc("concat(%s,%s,{0}) is not null", arg -> arg.accept(u1.id, addr1.userId), "12") .applyFunc("concat(%s,%s,{0}) is not null", arg -> arg.accept(u1.id,addr1.userId), "12") + .clone() .list(); list1.forEach(System.out::println); diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/mysql/UpdateJoinTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/mysql/UpdateJoinTest.java index 142c19ed..8a15d6a7 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/mysql/UpdateJoinTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/mysql/UpdateJoinTest.java @@ -2,6 +2,7 @@ import com.baomidou.mybatisplus.core.plugins.IgnoreStrategy; import com.baomidou.mybatisplus.core.plugins.InterceptorIgnoreHelper; +import com.github.yulichang.test.join.dto.UserJson; import com.github.yulichang.test.join.entity.AddressDO; import com.github.yulichang.test.join.entity.OrderDO; import com.github.yulichang.test.join.entity.UserDO; @@ -17,8 +18,9 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit.jupiter.EnabledIf; -import java.util.HashMap; +import java.util.ArrayList; import java.util.List; +import java.util.Objects; /** * 连表更新没有同意语法语法,不同数据库差别较大 @@ -60,11 +62,15 @@ void updateInc1() { @Test void updateInc2() { - HashMap map = new HashMap<>(); - map.put("aaa", "bbb"); + UserJson json = new UserJson(); + json.setId(1111L); + json.setName("11111111111"); + + List jsonList = new ArrayList<>(); + jsonList.add(json); UserDO userDO = new UserDO(); - userDO.setJson(map); + userDO.setJson(jsonList); InterceptorIgnoreHelper.handle(IgnoreStrategy.builder().tenantLine(true).build()); @@ -78,7 +84,7 @@ void updateInc2() { List list = JoinWrappers.lambda(UserDO.class).list(); list.forEach(System.out::println); list.forEach(c -> { - assert c.getJson().get("aaa").equals("bbb"); + assert Objects.equals(c.getJson().get(0).getName(), "11111111111"); }); } diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/TypeHandlerTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/TypeHandlerTest.java new file mode 100644 index 00000000..3f1bf03d --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/unit/TypeHandlerTest.java @@ -0,0 +1,31 @@ +package com.github.yulichang.test.join.unit; + +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 org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; + +import java.util.List; + +@SpringBootTest +public class TypeHandlerTest { + + @BeforeEach + void setUp() { + Reset.reset(); + } + + @Test + void typeHandler(){ + MPJLambdaWrapper wrapper = JoinWrappers.lambda(UserDO.class); + + List list = wrapper.list(); + + System.out.println(list.get(0).getJson().get(0).getClass().getName()); + + list.forEach(System.out::println); + } +}