diff --git a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java index c72771b..c07bb27 100644 --- a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java +++ b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJTableInfoHelper.java @@ -11,6 +11,7 @@ import org.apache.ibatis.session.Configuration; import java.lang.reflect.Field; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; import static java.util.stream.Collectors.toList; @@ -30,11 +31,33 @@ public class MPJTableInfoHelper { private static final Log logger = LogFactory.getLog(TableInfoHelper.class); + + /** + * 储存反射类表信息 + */ + private static final Map, TableInfo> TABLE_INFO_CACHE = new ConcurrentHashMap<>(); + /** * 默认表主键名称 */ private static final String DEFAULT_ID_NAME = "id"; + + /** + *

+ * 获取实体映射表信息 + *

+ * + * @param clazz 反射实体类 + * @return 数据库表反射信息 + */ + public static TableInfo getTableInfo(Class clazz) { + if (clazz == null || ReflectionKit.isPrimitiveOrWrapper(clazz) || clazz == String.class || clazz.isInterface()) { + return null; + } + return TABLE_INFO_CACHE.get(clazz); + } + /** *

* 实体类反射获取表信息【初始化】 @@ -61,6 +84,9 @@ public class MPJTableInfoHelper { /* 自动构建 resultMap */ tableInfo.initResultMapIfNeed(); + /* 添加缓存 */ + TABLE_INFO_CACHE.put(clazz, tableInfo); + /* 缓存 lambda */ LambdaUtils.installCache(tableInfo); return tableInfo; diff --git a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index fc983d9..3f961a3 100644 --- a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.StringPool; -import com.github.yulichang.exception.MPJException; import com.github.yulichang.method.MPJResultType; import com.github.yulichang.toolkit.Constant; import org.apache.ibatis.executor.Executor; @@ -117,8 +116,11 @@ public class MPJInterceptor implements Interceptor { if (tableInfo != null && tableInfo.isAutoInitResultMap() && tableInfo.getEntityType() == resultType) { return ms.getConfiguration().getResultMap(tableInfo.getResultMap()); } - TableInfo infoDTO = MPJTableInfoHelper.initTableInfo(ms.getConfiguration(), - ms.getId().substring(0, ms.getId().lastIndexOf(".")), resultType); + TableInfo infoDTO = MPJTableInfoHelper.getTableInfo(resultType); + if (infoDTO == null) { + infoDTO = MPJTableInfoHelper.initTableInfo(ms.getConfiguration(), + ms.getId().substring(0, ms.getId().lastIndexOf(".")), resultType); + } if (infoDTO.isAutoInitResultMap()) { return ms.getConfiguration().getResultMap(infoDTO.getResultMap()); }