Skip to content

Commit

Permalink
fix #163
Browse files Browse the repository at this point in the history
  • Loading branch information
yulichang committed Aug 13, 2024
1 parent c6b781f commit a334074
Show file tree
Hide file tree
Showing 21 changed files with 226 additions and 119 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -50,5 +56,23 @@ default List<OrderFieldInfo> mpjGetOrderField(TableInfo tableInfo) {
new OrderFieldInfo(f.getColumn(), f.getType(), f.getSort())).collect(Collectors.toList());
}

void parserColum(String alias, String from, String selectSql, Consumer<String> columConsumer);
default void parserColum(String alias, String from, String selectSql, Consumer<String> columConsumer) {
JSqlParserHelper.parserColum(alias, from, selectSql, columConsumer);
}

default TypeHandler<?> getTypeHandler(Configuration configuration, Class<?> propertyType, Class<? extends TypeHandler<?>> 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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -75,4 +77,14 @@ public List<OrderFieldInfo> mpjGetOrderField(TableInfo tableInfo) {
public void parserColum(String alias, String from, String selectSql, Consumer<String> columConsumer) {
JSqlParserHelperV46.parserColum(alias, from, selectSql, columConsumer);
}

@Override
public TypeHandler<?> getTypeHandler(Configuration configuration, Class<?> propertyType, Class<? extends TypeHandler<?>> typeHandlerClass, Field field) {
TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
TypeHandler<?> typeHandler = registry.getMappingTypeHandler(typeHandlerClass);
if (typeHandler == null) {
typeHandler = registry.getInstance(propertyType, typeHandlerClass);
}
return typeHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -46,4 +50,14 @@ public List<OrderFieldInfo> mpjGetOrderField(TableInfo tableInfo) {
public void parserColum(String alias, String from, String selectSql, Consumer<String> columConsumer) {
JSqlParserHelperV46.parserColum(alias, from, selectSql, columConsumer);
}

@Override
public TypeHandler<?> getTypeHandler(Configuration configuration, Class<?> propertyType, Class<? extends TypeHandler<?>> typeHandlerClass, Field field) {
TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
TypeHandler<?> typeHandler = registry.getMappingTypeHandler(typeHandlerClass);
if (typeHandler == null) {
typeHandler = registry.getInstance(propertyType, typeHandlerClass);
}
return typeHandler;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -15,4 +19,14 @@ public class Adapter355 implements IAdapter {
public void parserColum(String alias, String from, String selectSql, Consumer<String> columConsumer) {
JSqlParserHelperV46.parserColum(alias, from, selectSql, columConsumer);
}

@Override
public TypeHandler<?> getTypeHandler(Configuration configuration, Class<?> propertyType, Class<? extends TypeHandler<?>> typeHandlerClass, Field field) {
TypeHandlerRegistry registry = configuration.getTypeHandlerRegistry();
TypeHandler<?> typeHandler = registry.getMappingTypeHandler(typeHandlerClass);
if (typeHandler == null) {
typeHandler = registry.getInstance(propertyType, typeHandlerClass);
}
return typeHandler;
}
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
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
* @since 1.4.3
*/
public class Adapter implements IAdapter {

@Override
public void parserColum(String alias, String from, String selectSql, Consumer<String> columConsumer) {
JSqlParserHelper.parserColum(alias, from, selectSql, columConsumer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -73,7 +73,7 @@ public abstract class AptAbstractWrapper<T, Children extends AptAbstractWrapper<
/**
* 主表 表名处理方法
*/
protected Function<String, String> tableFunc;
protected SFunction<String, String> tableFunc;

/**
* 逻辑删除位置
Expand Down Expand Up @@ -140,7 +140,7 @@ protected AptAbstractWrapper(BaseColumn<T> baseColumn, T entity) {
*
* @return 自定义表别名
*/
public Children setTableName(Function<String, String> tableFunc) {
public Children setTableName(SFunction<String, String> tableFunc) {
if (isMain) {
if (tableFunc != null) {
this.dynamicTableName = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -97,7 +98,7 @@ protected AptQueryWrapper(T entity, BaseColumn<T> baseColumn, SharedString sqlSe
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments, SharedString paramAlias,
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
TableMap aptIndex, Integer index, String keyWord, Class<?> joinClass, String tableName,
BiPredicate<Object, IfExistsSqlKeyWordEnum> IfExists) {
MBiPredicate<Object, IfExistsSqlKeyWordEnum> IfExists) {
super(baseColumn);
super.setEntity(entity);
super.setEntityClass(baseColumn.getColumnClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -113,7 +110,7 @@ public abstract class JoinAbstractWrapper<T, Children extends JoinAbstractWrappe
* IfExists 策略
*/
@Getter
protected BiPredicate<Object, IfExistsSqlKeyWordEnum> ifExists = ConfigProperties.ifExists;
protected MBiPredicate<Object, IfExistsSqlKeyWordEnum> ifExists = ConfigProperties.ifExists;

@Override
public T getEntity() {
Expand Down Expand Up @@ -163,7 +160,7 @@ public Children checkSqlInjection() {
return typedThis;
}

public Children setIfExists(BiPredicate<Object, IfExistsSqlKeyWordEnum> IfExists) {
public Children setIfExists(MBiPredicate<Object, IfExistsSqlKeyWordEnum> IfExists) {
this.ifExists = IfExists;
return typedThis;
}
Expand Down Expand Up @@ -283,12 +280,12 @@ public Children apply(boolean condition, String applySql, Object... values) {
() -> formatSqlMaybeWithParam(applySql, null, values)));
}

public Children applyFunc(String applySql, Function<AptConsumer, Column[]> consumerFunction, Object... values) {
public Children applyFunc(String applySql, SFunction<AptConsumer, Column[]> consumerFunction, Object... values) {
return applyFunc(true, applySql, consumerFunction, values);
}

public Children applyFunc(boolean condition, String applySql,
Function<AptConsumer, Column[]> consumerFunction, Object... values) {
SFunction<AptConsumer, Column[]> consumerFunction, Object... values) {
return maybeDo(condition, () -> appendSqlSegments(APPLY,
() -> formatSqlMaybeWithParam(String.format(applySql,
Arrays.stream(consumerFunction.apply(AptConsumer.func)).map(this::columnToString).toArray()), null, values)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,13 @@ private List<ResultMap> 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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

import com.github.yulichang.apt.Column;

import java.io.Serializable;

/**
* 用于selectFunc 和 applyFunc中的参数填充
* 从原来的 {@link SelectFunc} 里的内部类中提取出来
*
* @author yulichang
* @since 1.4.13
*/
public class AptConsumer {
public class AptConsumer implements Serializable {

public static final AptConsumer func = new AptConsumer();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<? extends TypeHandler<?>> 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<Class<?>, Map<Class<?>, TypeHandler<?>>> CACHE = new ConcurrentHashMap<>();

public static TypeHandler<?> getTypeHandlerCache(Class<?> table, Class<? extends TypeHandler<?>> typeHandler, Class<?> propertyType) {
public static TypeHandler<?> getTypeHandlerCache(Class<?> table, Class<? extends TypeHandler<?>> typeHandler, Class<?> propertyType, String columProperty) {
if (table == null || typeHandler == null) {
return null;
}
Map<Class<?>, 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);
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public class UserDTO {
/** user */
private String PName;
/** user */
private Map<String,String> json;
private List<UserJson> json;
/** user */
private Sex sex;
/** user */
Expand Down
Loading

0 comments on commit a334074

Please sign in to comment.