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 55927b8..16f3763 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 @@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.core.injector.AbstractSqlInjector; import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.mybatisplus.core.injector.ISqlInjector; import com.github.yulichang.config.ConfigProperties; -import com.github.yulichang.config.InterceptorConfig; +import com.github.yulichang.config.MPJInterceptorConfig; import com.github.yulichang.injector.MPJSqlInjector; import com.github.yulichang.interceptor.MPJInterceptor; import com.github.yulichang.toolkit.SpringContentUtils; @@ -75,8 +75,8 @@ public class MybatisPlusJoinAutoConfiguration { */ @Bean @ConditionalOnBean(SqlSessionFactory.class) - public InterceptorConfig interceptorConfig(List sqlSessionFactoryList) { - return new InterceptorConfig(sqlSessionFactoryList, properties.getBanner()); + public MPJInterceptorConfig mpjInterceptorConfig(List sqlSessionFactoryList) { + return new MPJInterceptorConfig(sqlSessionFactoryList, properties.getBanner()); } /** diff --git a/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java b/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java index dedfe5a..1fe8dd7 100644 --- a/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java +++ b/mybatis-plus-join-core/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java @@ -6,6 +6,7 @@ import com.github.yulichang.annotation.EntityMapping; import com.github.yulichang.annotation.FieldMapping; import com.github.yulichang.exception.MPJException; import com.github.yulichang.mapper.MPJTableInfo; +import com.github.yulichang.toolkit.TableHelper; import org.apache.ibatis.session.Configuration; import java.lang.reflect.Field; @@ -75,7 +76,7 @@ public class MPJTableInfoHelper { } MPJTableInfo mpjTableInfo = new MPJTableInfo(); mpjTableInfo.setMapperClass(mapperClass); - TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz); + TableInfo tableInfo = TableHelper.get(clazz); if (tableInfo == null) { return; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java similarity index 91% rename from mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java index 70819c9..5c49671 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/InterceptorConfig.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/MPJInterceptorConfig.java @@ -18,12 +18,12 @@ import java.util.List; * * @author yulichang */ -public class InterceptorConfig { +public class MPJInterceptorConfig { - private static final Log logger = LogFactory.getLog(InterceptorConfig.class); + private static final Log logger = LogFactory.getLog(MPJInterceptorConfig.class); - public InterceptorConfig(List sqlSessionFactoryList, Boolean banner) { + public MPJInterceptorConfig(List sqlSessionFactoryList, Boolean banner) { replaceInterceptorChain(sqlSessionFactoryList); if (banner) { //打印banner diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java index aedff5c..11b5931 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/injector/MPJSqlInjector.java @@ -6,13 +6,19 @@ import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector; import com.baomidou.mybatisplus.core.injector.methods.*; import com.baomidou.mybatisplus.core.mapper.Mapper; import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.toolkit.ArrayUtils; import com.baomidou.mybatisplus.core.toolkit.ClassUtils; import com.github.yulichang.mapper.MPJTableMapperHelper; import com.github.yulichang.method.*; import com.github.yulichang.method.mp.SelectOne; +import com.github.yulichang.toolkit.TableHelper; +import com.github.yulichang.toolkit.reflect.GenericTypeUtils; import org.apache.ibatis.builder.MapperBuilderAssistant; -import org.springframework.core.GenericTypeResolver; +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.lang.reflect.TypeVariable; +import java.lang.reflect.WildcardType; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -116,10 +122,33 @@ public class MPJSqlInjector extends DefaultSqlInjector { Class modelClass = getSuperClassGenericType(mapperClass, Mapper.class, 0); super.inspectInject(builderAssistant, mapperClass); MPJTableMapperHelper.init(modelClass, mapperClass); + TableHelper.init(modelClass, extractModelClassOld(mapperClass)); } public static Class getSuperClassGenericType(final Class clazz, final Class genericIfc, final int index) { - Class[] typeArguments = GenericTypeResolver.resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc); + Class[] typeArguments = GenericTypeUtils.resolveTypeArguments(ClassUtils.getUserClass(clazz), genericIfc); return null == typeArguments ? null : typeArguments[index]; } + + protected Class extractModelClassOld(Class mapperClass) { + Type[] types = mapperClass.getGenericInterfaces(); + ParameterizedType target = null; + for (Type type : types) { + if (type instanceof ParameterizedType) { + Type[] typeArray = ((ParameterizedType) type).getActualTypeArguments(); + if (ArrayUtils.isNotEmpty(typeArray)) { + for (Type t : typeArray) { + if (t instanceof TypeVariable || t instanceof WildcardType) { + break; + } else { + target = (ParameterizedType) type; + break; + } + } + } + break; + } + } + return target == null ? null : (Class) target.getActualTypeArguments()[0]; + } } 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 efd0c7d..865a097 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 @@ -1,7 +1,6 @@ package com.github.yulichang.interceptor; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.*; import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.mapper.MPJTableMapperHelper; @@ -9,6 +8,7 @@ import com.github.yulichang.method.MPJResultType; import com.github.yulichang.query.MPJQueryWrapper; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.MPJReflectionKit; +import com.github.yulichang.toolkit.TableHelper; import com.github.yulichang.wrapper.MPJLambdaWrapper; import com.github.yulichang.wrapper.resultmap.MybatisLabel; import com.github.yulichang.wrapper.resultmap.Result; @@ -84,7 +84,7 @@ public class MPJInterceptor implements Interceptor { */ @SuppressWarnings("rawtypes") public MappedStatement getMappedStatement(MappedStatement ms, Class resultType, Object ew) { - String id = ms.getId() + StringPool.UNDERSCORE + resultType.getName(); + String id = ms.getId() + StringPool.DASH + (resultType.getName().replaceAll("\\.", StringPool.DASH)); if (ew instanceof MPJLambdaWrapper) { MPJLambdaWrapper wrapper = (MPJLambdaWrapper) ew; wrapper.setEntityClass(MPJTableMapperHelper.getEntity(getEntity(ms.getId()))); @@ -94,12 +94,13 @@ public class MPJInterceptor implements Interceptor { if (ew instanceof MPJQueryWrapper) { MPJQueryWrapper wrapper = (MPJQueryWrapper) ew; if (ConfigProperties.msCache) { - return getCache(ms, id + StringPool.UNDERSCORE + wrapper.getSqlSelect(), resultType, ew); + return getCache(ms, id + StringPool.UNDERSCORE + removeDot(wrapper.getSqlSelect()), resultType, ew); } } return buildMappedStatement(ms, resultType, ew, id); } + /** * 走缓存 */ @@ -150,7 +151,7 @@ public class MPJInterceptor implements Interceptor { @SuppressWarnings({"rawtypes", "unchecked"}) private List buildResultMap(MappedStatement ms, Class resultType, Object obj) { List result = new ArrayList<>(); - TableInfo tableInfo = TableInfoHelper.getTableInfo(resultType); + TableInfo tableInfo = TableHelper.get(resultType); String id = ms.getId() + StringPool.DOT + Constants.MYBATIS_PLUS + StringPool.UNDERSCORE + resultType.getName(); //基本数据类型 if (MPJReflectionKit.isPrimitiveOrWrapper(resultType)) { @@ -244,9 +245,9 @@ public class MPJInterceptor implements Interceptor { ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), r.getProperty(), columnName, r.getJavaType()); if (r.isId()) {//主键标记为id标签 builder.flags(Collections.singletonList(ResultFlag.ID)); - childId.append(ResultFlag.ID); + childId.append("i"); } else { - childId.append(ResultFlag.CONSTRUCTOR); + childId.append("c"); } //TypeHandle if (label.hasTypeHandle() && label.getColumnType().isAssignableFrom(field.getType())) { @@ -361,4 +362,12 @@ public class MPJInterceptor implements Interceptor { return null; } } + + private String removeDot(String str) { + if (StringUtils.isBlank(str)) { + return str; + } else { + return str.replaceAll("\\.", StringPool.DASH); + } + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java index 5d0b036..433c32e 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/mapper/MPJTableFieldInfo.java @@ -4,12 +4,12 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.metadata.MPJTableInfoHelper; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.*; import com.github.yulichang.annotation.EntityMapping; import com.github.yulichang.annotation.FieldMapping; import com.github.yulichang.exception.MPJException; import com.github.yulichang.toolkit.SpringContentUtils; +import com.github.yulichang.toolkit.TableHelper; import lombok.EqualsAndHashCode; import lombok.Getter; import lombok.ToString; @@ -164,7 +164,8 @@ public class MPJTableFieldInfo { } private void initBindField(String bindName) { - TableInfo info = TableInfoHelper.getTableInfo(this.joinClass); + TableInfo info = TableHelper.get(this.joinClass); + Assert.notNull(info, "未注册的实体类 <%s>", this.joinClass.getSimpleName()); Field field = info.getFieldList().stream() .filter(i -> i.getColumn().equals(bindName)) .map(TableFieldInfo::getField).findFirst().orElse(null); @@ -285,7 +286,7 @@ public class MPJTableFieldInfo { } private TableInfo getTableInfo(Class clazz) { - TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz); + TableInfo tableInfo = TableHelper.get(clazz); if (tableInfo == null) { throw new MPJException("未注册 mapper " + clazz.getName()); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java index 9214664..44c12af 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java @@ -6,11 +6,11 @@ import com.baomidou.mybatisplus.core.conditions.query.Query; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.query.interfaces.StringJoin; +import com.github.yulichang.toolkit.TableHelper; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; @@ -159,7 +159,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper select(Class entityClass, Predicate predicate) { - TableInfo info = TableInfoHelper.getTableInfo(entityClass); + TableInfo info = TableHelper.get(entityClass); Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName()); selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c -> alias + StringPool.DOT + c.getColumn()).collect(Collectors.toList())); @@ -184,7 +184,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper selectAll(Class clazz, String as) { - TableInfo info = TableInfoHelper.getTableInfo(clazz); + TableInfo info = TableHelper.get(clazz); Assert.notNull(info, "table not find by class <%s>", clazz.getSimpleName()); if (info.havePK()) { selectColumns.add(as + StringPool.DOT + info.getKeyColumn()); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java index 0e7d8ef..e4e7613 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJQueryWrapper.java @@ -6,11 +6,11 @@ import com.baomidou.mybatisplus.core.conditions.query.Query; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.*; import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.query.interfaces.StringJoin; import com.github.yulichang.toolkit.MPJWrappers; +import com.github.yulichang.toolkit.TableHelper; import java.util.ArrayList; import java.util.Arrays; @@ -134,7 +134,7 @@ public class MPJQueryWrapper extends AbstractWrapper select(Class entityClass, Predicate predicate) { - TableInfo info = TableInfoHelper.getTableInfo(entityClass); + TableInfo info = TableHelper.get(entityClass); Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName()); selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c -> alias + StringPool.DOT + c.getSqlSelect()).collect(Collectors.toList())); @@ -159,7 +159,7 @@ public class MPJQueryWrapper extends AbstractWrapper selectAll(Class clazz, String as) { - TableInfo info = TableInfoHelper.getTableInfo(clazz); + TableInfo info = TableHelper.get(clazz); Assert.notNull(info, "table not find by class <%s>", clazz); if (info.havePK()) { selectColumns.add(as + StringPool.DOT + info.getKeySqlSelect()); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java index 644c22a..1d2ff7c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java @@ -1,7 +1,6 @@ package com.github.yulichang.toolkit; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.Assert; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.StringPool; @@ -33,7 +32,7 @@ public class LogicInfoUtils implements Constants { private static String getLogicStr(String prefix, Class clazz) { String logicStr; - TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz); + TableInfo tableInfo = TableHelper.get(clazz); Assert.notNull(tableInfo, "table not find by class <%s>", clazz.getSimpleName()); if (tableInfo.isWithLogicDelete() && Objects.nonNull(tableInfo.getLogicDeleteFieldInfo())) { final String value = tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue(); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java index d9d92a8..1c051c7 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/ReflectionKit.java @@ -15,8 +15,10 @@ */ package com.github.yulichang.toolkit; -import com.baomidou.mybatisplus.core.toolkit.*; -import com.baomidou.mybatisplus.core.toolkit.reflect.GenericTypeUtils; +import com.baomidou.mybatisplus.core.toolkit.Assert; +import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; +import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils; +import com.github.yulichang.toolkit.reflect.GenericTypeUtils; import java.lang.reflect.AccessibleObject; import java.lang.reflect.Field; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SetAccessibleAction.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SetAccessibleAction.java new file mode 100644 index 0000000..cd83c56 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/SetAccessibleAction.java @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit; + +import java.lang.reflect.AccessibleObject; +import java.security.PrivilegedAction; + +/** + * Create by hcl at 2021/5/14 + */ +public class SetAccessibleAction implements PrivilegedAction { + private final T obj; + + public SetAccessibleAction(T obj) { + this.obj = obj; + } + + @Override + public T run() { + obj.setAccessible(true); + return obj; + } + +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/TableHelper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/TableHelper.java new file mode 100644 index 0000000..5ca5eaa --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/TableHelper.java @@ -0,0 +1,43 @@ +package com.github.yulichang.toolkit; + +import com.baomidou.mybatisplus.core.metadata.TableInfo; +import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; + +import java.util.Map; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; + +/** + * @author yulichang + * @since 1.4.3 + */ +public class TableHelper { + + private static final Map, TableInfo> TABLE_INFO_CACHE = new ConcurrentHashMap<>(); + + public static void init(Class newClass, Class oldClass) { + if (Objects.nonNull(newClass)) { + TableInfo info = TableInfoHelper.getTableInfo(newClass); + if (Objects.isNull(info)) { + if (Objects.nonNull(oldClass)) { + TableInfo oldInfo = TableInfoHelper.getTableInfo(oldClass); + if (Objects.nonNull(oldInfo)) { + TABLE_INFO_CACHE.put(newClass, oldInfo); + } + } + } + } + } + + public static TableInfo get(Class clazz) { + if (Objects.nonNull(clazz)) { + TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz); + if (Objects.nonNull(tableInfo)) { + return tableInfo; + } + return TABLE_INFO_CACHE.get(clazz); + } else { + return null; + } + } +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/GenericTypeUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/GenericTypeUtils.java new file mode 100644 index 0000000..7065aad --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/GenericTypeUtils.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit.reflect; + +/** + * 泛型类工具(用于隔离Spring的代码) + * + * @author noear + * @author hubin + * @since 2021-09-03 + */ +@SuppressWarnings("ALL") +public class GenericTypeUtils { + private static IGenericTypeResolver GENERIC_TYPE_RESOLVER; + + /** + * 获取泛型工具助手 + */ + public static Class[] resolveTypeArguments(final Class clazz, final Class genericIfc) { + if (null == GENERIC_TYPE_RESOLVER) { + // 直接使用 spring 静态方法,减少对象创建 + return SpringReflectionHelper.resolveTypeArguments(clazz, genericIfc); + } + return GENERIC_TYPE_RESOLVER.resolveTypeArguments(clazz, genericIfc); + } + + /** + * 设置泛型工具助手。如果不想使用Spring封装,可以使用前替换掉 + */ + public static void setGenericTypeResolver(IGenericTypeResolver genericTypeResolver) { + GENERIC_TYPE_RESOLVER = genericTypeResolver; + } +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/IGenericTypeResolver.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/IGenericTypeResolver.java new file mode 100644 index 0000000..0a72188 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/IGenericTypeResolver.java @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit.reflect; + +/** + * 泛型类助手(用于隔离Spring的代码) + * + * @author noear + * @author hubin + * @since 2021-09-03 + */ +public interface IGenericTypeResolver { + + Class[] resolveTypeArguments(final Class clazz, final Class genericIfc); +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/SpringReflectionHelper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/SpringReflectionHelper.java new file mode 100644 index 0000000..6af1ec6 --- /dev/null +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/reflect/SpringReflectionHelper.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2011-2022, baomidou (jobob@qq.com). + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.github.yulichang.toolkit.reflect; + +import org.springframework.core.GenericTypeResolver; + +/** + * Spring 反射辅助类 + * + * @author noear + * @author hubin + * @since 2021-09-03 + */ +public class SpringReflectionHelper { + + public static Class[] resolveTypeArguments(Class clazz, Class genericIfc) { + return GenericTypeResolver.resolveTypeArguments(clazz, genericIfc); + } +} diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java index 22f8a4b..9b1ac61 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/support/ColumnCache.java @@ -1,8 +1,8 @@ package com.github.yulichang.toolkit.support; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.Assert; +import com.github.yulichang.toolkit.TableHelper; import com.github.yulichang.wrapper.segments.SelectCache; import java.util.ArrayList; @@ -26,7 +26,7 @@ public class ColumnCache { public static List getListField(Class clazz) { return LIST_CACHE.computeIfAbsent(clazz, c -> { - TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz); + TableInfo tableInfo = TableHelper.get(clazz); Assert.notNull(tableInfo, "table not find by class <%s>", c.getSimpleName()); List list = new ArrayList<>(); if (tableInfo.havePK()) { 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 f3802b4..a862f21 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 @@ -3,13 +3,13 @@ package com.github.yulichang.wrapper; import com.baomidou.mybatisplus.core.conditions.SharedString; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.LogicInfoUtils; +import com.github.yulichang.toolkit.TableHelper; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.wrapper.interfaces.Query; import com.github.yulichang.wrapper.interfaces.QueryJoin; @@ -230,7 +230,7 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper wrapper : onWrappers) { if (StringUtils.isBlank(wrapper.from.getStringValue())) { - TableInfo info = TableInfoHelper.getTableInfo(wrapper.getJoinClass()); + TableInfo info = TableHelper.get(wrapper.getJoinClass()); Assert.notNull(info, "table not find by class <%s>", wrapper.getJoinClass().getSimpleName()); String tableName = info.getTableName(); value.append(StringPool.SPACE) diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java index 3126b4d..7fbcef9 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java @@ -2,12 +2,12 @@ package com.github.yulichang.wrapper.interfaces; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.Assert; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.MPJReflectionKit; +import com.github.yulichang.toolkit.TableHelper; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import com.github.yulichang.wrapper.enums.DefaultFuncEnum; @@ -50,7 +50,7 @@ public interface Query extends Serializable { * @return children */ default Children select(Class entityClass, Predicate predicate) { - TableInfo info = TableInfoHelper.getTableInfo(entityClass); + TableInfo info = TableHelper.get(entityClass); Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName()); Map cacheMap = ColumnCache.getMapField(entityClass); info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach( 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 cda4d1b..c73e849 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 @@ -1,12 +1,12 @@ package com.github.yulichang.wrapper.resultmap; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.Assert; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.MPJReflectionKit; +import com.github.yulichang.toolkit.TableHelper; import com.github.yulichang.toolkit.support.ColumnCache; import com.github.yulichang.wrapper.segments.SelectCache; import lombok.Getter; @@ -209,7 +209,7 @@ public class MybatisLabel { } private void autoBuild(boolean auto, Class entityClass, Class tagClass) { - TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); + TableInfo tableInfo = TableHelper.get(entityClass); Assert.notNull(tableInfo, "table not find by class <%s>", entityClass.getSimpleName()); Map tagMap = MPJReflectionKit.getFieldMap(tagClass); if (auto && !tagMap.isEmpty()) { 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 6eb2d44..98dc44f 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 @@ -2,9 +2,9 @@ package com.github.yulichang.wrapper.segments; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; -import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.Assert; import com.baomidou.mybatisplus.core.toolkit.StringUtils; +import com.github.yulichang.toolkit.TableHelper; import lombok.Getter; import org.apache.ibatis.session.Configuration; import org.apache.ibatis.type.TypeHandler; @@ -54,7 +54,7 @@ public class SelectCache { } else { this.hasTypeHandle = this.tableFieldInfo.getTypeHandler() != null && tableFieldInfo.getTypeHandler() != UnknownTypeHandler.class; if (this.hasTypeHandle) { - TableInfo info = TableInfoHelper.getTableInfo(clazz); + TableInfo info = TableHelper.get(clazz); Assert.notNull(info, "table not find by class <%s>", clazz.getSimpleName()); this.typeHandler = getTypeHandler(info.getConfiguration(), tableFieldInfo); } else {