优化原生方法别名支持

This commit is contained in:
yulichang 2023-11-01 23:27:06 +08:00
parent 4bf4d25c9c
commit 289127c0f6
3 changed files with 80 additions and 36 deletions

View File

@ -2,19 +2,24 @@ package com.github.yulichang.interceptor;
import com.baomidou.mybatisplus.core.MybatisPlusVersion;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.*;
import com.github.yulichang.adapter.base.tookit.VersionUtils;
import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.method.MPJResultType;
import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.toolkit.*;
import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.MPJTableMapperHelper;
import com.github.yulichang.toolkit.TableHelper;
import com.github.yulichang.toolkit.support.FieldCache;
import com.github.yulichang.wrapper.interfaces.SelectWrapper;
import com.github.yulichang.wrapper.resultmap.IResult;
import com.github.yulichang.wrapper.resultmap.Label;
import com.github.yulichang.wrapper.segments.Select;
import com.github.yulichang.wrapper.segments.SelectLabel;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultFlag;
@ -50,6 +55,9 @@ public class MPJInterceptor implements Interceptor {
*/
private static final Map<String, Map<Configuration, MappedStatement>> MS_CACHE = new ConcurrentHashMap<>();
private static final Map<String, Val> MS_MAPPER_CACHE = new ConcurrentHashMap<>();
private static final Map<String, Val> RES_MAPPER_CACHE = new ConcurrentHashMap<>();
@Override
@SuppressWarnings("Java8MapApi")
@ -60,14 +68,27 @@ public class MPJInterceptor implements Interceptor {
if (args[1] instanceof Map) {
Map<String, Object> map = (Map<String, Object>) args[1];
Object ew = map.containsKey(Constants.WRAPPER) ? map.get(Constants.WRAPPER) : null;
if (CollectionUtils.isNotEmpty(map) && map.containsKey(Constant.CLAZZ)) {
Class<?> clazz = (Class<?>) map.get(Constant.CLAZZ);
if (Objects.nonNull(clazz)) {
List<ResultMap> list = ms.getResultMaps();
if (CollectionUtils.isNotEmpty(list)) {
ResultMap resultMap = list.get(0);
if (resultMap.getType() == MPJResultType.class) {
args[0] = getMappedStatement(ms, clazz, ew);
if (Objects.nonNull(ew) && ew instanceof MPJBaseJoin) {
if (CollectionUtils.isNotEmpty(map)) {
Class<?> rt = null;
if (map.containsKey(Constant.CLAZZ)) {
rt = (Class<?>) map.get(Constant.CLAZZ);
} else {
if (CollectionUtils.isNotEmpty(ms.getResultMaps())) {
Class<?> entity = MPJTableMapperHelper.getEntity(getMapper(ms.getId(), ms.getResource()));
Class<?> type = ms.getResultMaps().get(0).getType();
if (Objects.nonNull(entity) && Objects.nonNull(type) && entity == type) {
rt = type;
}
}
}
if (Objects.nonNull(rt)) {
List<ResultMap> list = ms.getResultMaps();
if (CollectionUtils.isNotEmpty(list)) {
ResultMap resultMap = list.get(0);
if (resultMap.getType() == MPJResultType.class) {
args[0] = getMappedStatement(ms, rt, ew);
}
}
}
}
@ -87,7 +108,7 @@ public class MPJInterceptor implements Interceptor {
if (ew instanceof SelectWrapper) {
SelectWrapper wrapper = (SelectWrapper) ew;
if (wrapper.getEntityClass() == null) {
wrapper.setEntityClass(MPJTableMapperHelper.getEntity(getEntity(ms.getId(), ms.getResource())));
wrapper.setEntityClass(MPJTableMapperHelper.getEntity(getMapper(ms.getId(), ms.getResource())));
}
if (wrapper.getSelectColumns().isEmpty() && wrapper.getEntityClass() != null) {
wrapper.selectAll(wrapper.getEntityClass());
@ -360,29 +381,37 @@ public class MPJInterceptor implements Interceptor {
}
}
private Class<?> getEntity(String id, String resource) {
Class<?> clazz = null;
try {
String className = id.substring(0, id.lastIndexOf(StringPool.DOT));
private Class<?> getMapper(String id, String resource) {
Class<?> clazz = MS_MAPPER_CACHE.computeIfAbsent(id, key -> {
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
clazz = MPJTableMapperHelper.getMapperForName(className);
String className = key.substring(0, key.lastIndexOf(StringPool.DOT));
try {
return new Val(Class.forName(className));
} catch (ClassNotFoundException e) {
return new Val(MPJTableMapperHelper.getMapperForName(className));
}
} catch (Exception ignored) {
return new Val(null);
}
} catch (Exception ignored) {
}
}).getVal();
if (Objects.nonNull(clazz)) {
return clazz;
}
try {
String className = resource.substring(0, id.lastIndexOf(StringPool.DOT)).replaceAll("/", StringPool.DOT);
clazz = RES_MAPPER_CACHE.computeIfAbsent(resource, key -> {
try {
clazz = Class.forName(className);
} catch (ClassNotFoundException e) {
clazz = MPJTableMapperHelper.getMapperForName(className);
String className = key.substring(0, key.lastIndexOf(StringPool.DOT)).replaceAll("/", StringPool.DOT);
try {
return new Val(Class.forName(className));
} catch (ClassNotFoundException e) {
return new Val(MPJTableMapperHelper.getMapperForName(className));
}
} catch (Exception ignored) {
return new Val(null);
}
} catch (Exception ignored) {
}
}).getVal();
return clazz;
}
@ -410,4 +439,10 @@ public class MPJInterceptor implements Interceptor {
} catch (Exception ignored) {
}
}
@Data
@AllArgsConstructor
public static class Val {
private Class<?> val;
}
}

View File

@ -1,6 +1,7 @@
package com.github.yulichang.test.join.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
@ -13,11 +14,11 @@ import lombok.experimental.FieldNameConstants;
@TableName(value = "user_tenant")
public class UserTenantDO {
@TableId
private Integer id;
@TableId("id")
private Integer idea;
private Integer userId;
@TableField("user_id")
private Integer uuid;
private Integer tenantId;
}

View File

@ -9,10 +9,7 @@ import com.github.yulichang.adapter.base.tookit.VersionUtils;
import com.github.yulichang.test.join.dto.AddressDTO;
import com.github.yulichang.test.join.dto.UserDTO;
import com.github.yulichang.test.join.entity.*;
import com.github.yulichang.test.join.mapper.AddressMapper;
import com.github.yulichang.test.join.mapper.OrderMapper;
import com.github.yulichang.test.join.mapper.UserDTOMapper;
import com.github.yulichang.test.join.mapper.UserMapper;
import com.github.yulichang.test.join.mapper.*;
import com.github.yulichang.test.util.Reset;
import com.github.yulichang.test.util.ThreadLocalUtils;
import com.github.yulichang.toolkit.JoinWrappers;
@ -54,11 +51,23 @@ class LambdaWrapperTest {
@Autowired
private OrderMapper orderMapper;
@Autowired
private UserTenantMapper userTenantMapper;
@BeforeEach
void setUp() {
Reset.reset();
}
@Test
void testSimple() {
MPJLambdaWrapper<UserTenantDO> lambda = JoinWrappers.lambda(UserTenantDO.class);
lambda.selectAs(UserTenantDO::getIdea, UserTenantDO::getIdea);
List<UserTenantDO> list = userTenantMapper.selectList(lambda);
assert list.size() == 5 && list.get(0).getIdea() != null;
}
@Test
void testJoin() {
ThreadLocalUtils.set("SELECT t.id,\n" +
@ -985,7 +994,6 @@ class LambdaWrapperTest {
.selectAll(OrderDO.class)
.selectAs(UserDO::getName, OrderDO::getUserName)
.leftJoin(UserDO.class, UserDO::getId, OrderDO::getUserId);
System.out.println(wrapper.getFrom());
List<OrderDO> l = w.list();
}