mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
优化resultMap
This commit is contained in:
parent
d35cd46d60
commit
b7bf356d33
@ -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;
|
||||
|
@ -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());
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user