优化resultMap

This commit is contained in:
admin 2021-06-09 17:58:08 +08:00
parent d35cd46d60
commit b7bf356d33
2 changed files with 31 additions and 3 deletions

View File

@ -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<Class<?>, TableInfo> TABLE_INFO_CACHE = new ConcurrentHashMap<>();
/**
* 默认表主键名称
*/
private static final String DEFAULT_ID_NAME = "id";
/**
* <p>
* 获取实体映射表信息
* </p>
*
* @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);
}
/**
* <p>
* 实体类反射获取表信息初始化
@ -61,6 +84,9 @@ public class MPJTableInfoHelper {
/* 自动构建 resultMap */
tableInfo.initResultMapIfNeed();
/* 添加缓存 */
TABLE_INFO_CACHE.put(clazz, tableInfo);
/* 缓存 lambda */
LambdaUtils.installCache(tableInfo);
return tableInfo;

View File

@ -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());
}