优化原生方法别名支持

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.MybatisPlusVersion;
import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.toolkit.ReflectionKit;
import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.*;
import com.github.yulichang.adapter.base.tookit.VersionUtils; import com.github.yulichang.adapter.base.tookit.VersionUtils;
import com.github.yulichang.config.ConfigProperties; import com.github.yulichang.config.ConfigProperties;
import com.github.yulichang.interfaces.MPJBaseJoin;
import com.github.yulichang.method.MPJResultType; import com.github.yulichang.method.MPJResultType;
import com.github.yulichang.query.MPJQueryWrapper; 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.toolkit.support.FieldCache;
import com.github.yulichang.wrapper.interfaces.SelectWrapper; import com.github.yulichang.wrapper.interfaces.SelectWrapper;
import com.github.yulichang.wrapper.resultmap.IResult; import com.github.yulichang.wrapper.resultmap.IResult;
import com.github.yulichang.wrapper.resultmap.Label; import com.github.yulichang.wrapper.resultmap.Label;
import com.github.yulichang.wrapper.segments.Select; import com.github.yulichang.wrapper.segments.Select;
import com.github.yulichang.wrapper.segments.SelectLabel; import com.github.yulichang.wrapper.segments.SelectLabel;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.apache.ibatis.executor.Executor; import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.MappedStatement; import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.mapping.ResultFlag; 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, 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 @Override
@SuppressWarnings("Java8MapApi") @SuppressWarnings("Java8MapApi")
@ -60,14 +68,27 @@ public class MPJInterceptor implements Interceptor {
if (args[1] instanceof Map) { if (args[1] instanceof Map) {
Map<String, Object> map = (Map<String, Object>) args[1]; Map<String, Object> map = (Map<String, Object>) args[1];
Object ew = map.containsKey(Constants.WRAPPER) ? map.get(Constants.WRAPPER) : null; Object ew = map.containsKey(Constants.WRAPPER) ? map.get(Constants.WRAPPER) : null;
if (CollectionUtils.isNotEmpty(map) && map.containsKey(Constant.CLAZZ)) { if (Objects.nonNull(ew) && ew instanceof MPJBaseJoin) {
Class<?> clazz = (Class<?>) map.get(Constant.CLAZZ); if (CollectionUtils.isNotEmpty(map)) {
if (Objects.nonNull(clazz)) { Class<?> rt = null;
List<ResultMap> list = ms.getResultMaps(); if (map.containsKey(Constant.CLAZZ)) {
if (CollectionUtils.isNotEmpty(list)) { rt = (Class<?>) map.get(Constant.CLAZZ);
ResultMap resultMap = list.get(0); } else {
if (resultMap.getType() == MPJResultType.class) { if (CollectionUtils.isNotEmpty(ms.getResultMaps())) {
args[0] = getMappedStatement(ms, clazz, ew); 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) { if (ew instanceof SelectWrapper) {
SelectWrapper wrapper = (SelectWrapper) ew; SelectWrapper wrapper = (SelectWrapper) ew;
if (wrapper.getEntityClass() == null) { 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) { if (wrapper.getSelectColumns().isEmpty() && wrapper.getEntityClass() != null) {
wrapper.selectAll(wrapper.getEntityClass()); wrapper.selectAll(wrapper.getEntityClass());
@ -360,29 +381,37 @@ public class MPJInterceptor implements Interceptor {
} }
} }
private Class<?> getEntity(String id, String resource) { private Class<?> getMapper(String id, String resource) {
Class<?> clazz = null; Class<?> clazz = MS_MAPPER_CACHE.computeIfAbsent(id, key -> {
try {
String className = id.substring(0, id.lastIndexOf(StringPool.DOT));
try { try {
clazz = Class.forName(className); String className = key.substring(0, key.lastIndexOf(StringPool.DOT));
} catch (ClassNotFoundException e) { try {
clazz = MPJTableMapperHelper.getMapperForName(className); 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)) { if (Objects.nonNull(clazz)) {
return clazz; return clazz;
} }
try {
String className = resource.substring(0, id.lastIndexOf(StringPool.DOT)).replaceAll("/", StringPool.DOT); clazz = RES_MAPPER_CACHE.computeIfAbsent(resource, key -> {
try { try {
clazz = Class.forName(className); String className = key.substring(0, key.lastIndexOf(StringPool.DOT)).replaceAll("/", StringPool.DOT);
} catch (ClassNotFoundException e) { try {
clazz = MPJTableMapperHelper.getMapperForName(className); 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; return clazz;
} }
@ -410,4 +439,10 @@ public class MPJInterceptor implements Interceptor {
} catch (Exception ignored) { } 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; package com.github.yulichang.test.join.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName; import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data; import lombok.Data;
@ -13,11 +14,11 @@ import lombok.experimental.FieldNameConstants;
@TableName(value = "user_tenant") @TableName(value = "user_tenant")
public class UserTenantDO { public class UserTenantDO {
@TableId @TableId("id")
private Integer id; private Integer idea;
@TableField("user_id")
private Integer userId; private Integer uuid;
private Integer tenantId; 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.AddressDTO;
import com.github.yulichang.test.join.dto.UserDTO; import com.github.yulichang.test.join.dto.UserDTO;
import com.github.yulichang.test.join.entity.*; import com.github.yulichang.test.join.entity.*;
import com.github.yulichang.test.join.mapper.AddressMapper; import com.github.yulichang.test.join.mapper.*;
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.util.Reset; import com.github.yulichang.test.util.Reset;
import com.github.yulichang.test.util.ThreadLocalUtils; import com.github.yulichang.test.util.ThreadLocalUtils;
import com.github.yulichang.toolkit.JoinWrappers; import com.github.yulichang.toolkit.JoinWrappers;
@ -54,11 +51,23 @@ class LambdaWrapperTest {
@Autowired @Autowired
private OrderMapper orderMapper; private OrderMapper orderMapper;
@Autowired
private UserTenantMapper userTenantMapper;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
Reset.reset(); 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 @Test
void testJoin() { void testJoin() {
ThreadLocalUtils.set("SELECT t.id,\n" + ThreadLocalUtils.set("SELECT t.id,\n" +
@ -985,7 +994,6 @@ class LambdaWrapperTest {
.selectAll(OrderDO.class) .selectAll(OrderDO.class)
.selectAs(UserDO::getName, OrderDO::getUserName) .selectAs(UserDO::getName, OrderDO::getUserName)
.leftJoin(UserDO.class, UserDO::getId, OrderDO::getUserId); .leftJoin(UserDO.class, UserDO::getId, OrderDO::getUserId);
System.out.println(wrapper.getFrom());
List<OrderDO> l = w.list(); List<OrderDO> l = w.list();
} }