diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/interfaces/Query.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/interfaces/Query.java index 5b76e76..77c5a85 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/interfaces/Query.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/kt/interfaces/Query.java @@ -28,7 +28,7 @@ import java.util.stream.Collectors; * * @author yulichang */ -@SuppressWarnings("unused") +@SuppressWarnings({"unused", "DuplicatedCode"}) public interface Query extends Serializable { @@ -129,11 +129,12 @@ public interface Query extends Serializable { * @return children */ default Children selectAsClass(Class source, Class tag) { - List normalList = ColumnCache.getListField(source); - Map fieldMap = MPJReflectionKit.getFieldMap(tag); - for (SelectCache cache : normalList) { - if (fieldMap.containsKey(cache.getColumProperty())) { - getSelectColum().add(new SelectNormal(cache, getIndex(), isHasAlias(), getAlias())); + Map normalMap = ColumnCache.getMapField(source); + List fieldList = MPJReflectionKit.getFieldList(tag); + for (FieldCache cache : fieldList) { + if (normalMap.containsKey(cache.getField().getName())) { + SelectCache selectCache = normalMap.get(cache.getField().getName()); + getSelectColum().add(new SelectNormal(selectCache, getIndex(), isHasAlias(), getAlias())); } } return getChildren(); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java index 26ee29f..3276d5f 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/MPJReflectionKit.java @@ -8,11 +8,10 @@ import java.lang.reflect.Field; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import java.lang.reflect.WildcardType; -import java.util.HashMap; -import java.util.IdentityHashMap; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.concurrent.ConcurrentHashMap; +import java.util.function.Function; +import java.util.stream.Collectors; /** * 反射工具类 @@ -24,6 +23,7 @@ import java.util.concurrent.ConcurrentHashMap; public final class MPJReflectionKit { private static final Map, Map> CLASS_FIELD_CACHE = new ConcurrentHashMap<>(); + private static final Map, List> CLASS_FIELD_LIST_CACHE = new ConcurrentHashMap<>(); private static final Map EMPTY_MAP = new HashMap<>(); @@ -77,28 +77,32 @@ public final class MPJReflectionKit { * @param clazz 反射类 */ public static Map getFieldMap(Class clazz) { + return CLASS_FIELD_CACHE.computeIfAbsent(clazz, key -> getFieldList(key).stream().collect(Collectors.toMap(f -> + f.getField().getName(), Function.identity()))); + } + + public static List getFieldList(Class clazz) { if (clazz == null) { - return EMPTY_MAP; + return Collections.emptyList(); } - Map fieldMap = CLASS_FIELD_CACHE.get(clazz); - if (fieldMap != null) { - return fieldMap; + List fieldList = CLASS_FIELD_LIST_CACHE.get(clazz); + if (fieldList != null) { + return fieldList; } - Map map = ReflectionKit.getFieldMap(clazz); - Map cache = new HashMap<>(); - map.forEach((key, value) -> { + List list = ReflectionKit.getFieldList(clazz); + List cache = list.stream().map(f -> { FieldCache fieldCache = new FieldCache(); - fieldCache.setField(value); + fieldCache.setField(f); try { Reflector reflector = new Reflector(clazz); - Class getterType = reflector.getGetterType(key); - fieldCache.setType(Objects.isNull(getterType) ? value.getType() : getterType); + Class getterType = reflector.getGetterType(f.getName()); + fieldCache.setType(Objects.isNull(getterType) ? f.getType() : getterType); } catch (Throwable throwable) { - fieldCache.setType(value.getType()); + fieldCache.setType(f.getType()); } - cache.put(key, fieldCache); - }); - CLASS_FIELD_CACHE.put(clazz, cache); + return fieldCache; + }).collect(Collectors.toList()); + CLASS_FIELD_LIST_CACHE.put(clazz, cache); return cache; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java index d393a14..f49f7be 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java @@ -27,7 +27,7 @@ import java.util.stream.Collectors; * * @author yulichang */ -@SuppressWarnings("unused") +@SuppressWarnings({"unused", "DuplicatedCode"}) public interface Query extends Serializable { @@ -129,11 +129,12 @@ public interface Query extends Serializable { * @return children */ default Children selectAsClass(Class source, Class tag) { - List normalList = ColumnCache.getListField(source); - Map fieldMap = MPJReflectionKit.getFieldMap(tag); - for (SelectCache cache : normalList) { - if (fieldMap.containsKey(cache.getColumProperty())) { - getSelectColum().add(new SelectNormal(cache, getIndex(), isHasAlias(), getAlias())); + Map normalMap = ColumnCache.getMapField(source); + List fieldList = MPJReflectionKit.getFieldList(tag); + for (FieldCache cache : fieldList) { + if (normalMap.containsKey(cache.getField().getName())) { + SelectCache selectCache = normalMap.get(cache.getField().getName()); + getSelectColum().add(new SelectNormal(selectCache, getIndex(), isHasAlias(), getAlias())); } } return getChildren(); diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserTenantDTO.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserTenantDTO.java new file mode 100644 index 0000000..1aa0b3c --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserTenantDTO.java @@ -0,0 +1,16 @@ +package com.github.yulichang.test.join.dto; + + +import lombok.Data; +import lombok.ToString; + +@Data +@ToString +public class UserTenantDTO { + + private Integer idea; + + private Integer uuid; + + private Integer tenantId; +} diff --git a/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserTenantDescDTO.java b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserTenantDescDTO.java new file mode 100644 index 0000000..5f8945f --- /dev/null +++ b/mybatis-plus-join-test/test-join/src/main/java/com/github/yulichang/test/join/dto/UserTenantDescDTO.java @@ -0,0 +1,16 @@ +package com.github.yulichang.test.join.dto; + + +import lombok.Data; +import lombok.ToString; + +@Data +@ToString +public class UserTenantDescDTO { + + private Integer tenantId; + + private Integer uuid; + + private Integer idea; +} diff --git a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java index df12348..c6a7f22 100644 --- a/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java +++ b/mybatis-plus-join-test/test-join/src/test/java/com/github/yulichang/test/join/LambdaWrapperTest.java @@ -8,6 +8,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; 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.dto.UserTenantDTO; +import com.github.yulichang.test.join.dto.UserTenantDescDTO; import com.github.yulichang.test.join.entity.*; import com.github.yulichang.test.join.mapper.*; import com.github.yulichang.test.util.Reset; @@ -59,6 +61,22 @@ class LambdaWrapperTest { Reset.reset(); } + @Test + void testSelectSort(){ + ThreadLocalUtils.set("SELECT t.id, t.user_id, t.tenant_id FROM user_tenant t WHERE t.tenant_id = 1"); + MPJLambdaWrapper lambda = JoinWrappers.lambda(UserTenantDO.class); + lambda.selectAsClass(UserTenantDO.class, UserTenantDTO.class); + List list = userTenantMapper.selectJoinList(UserTenantDO.class,lambda); + assert list.size() == 5 && list.get(0).getIdea() != null; + + + ThreadLocalUtils.set("SELECT t.tenant_id, t.user_id, t.id FROM user_tenant t WHERE t.tenant_id = 1"); + MPJLambdaWrapper lambda1 = JoinWrappers.lambda(UserTenantDO.class); + lambda1.selectAsClass(UserTenantDO.class, UserTenantDescDTO.class); + List list1 = userTenantMapper.selectJoinList(UserTenantDO.class,lambda1); + assert list1.size() == 5 && list1.get(0).getIdea() != null; + } + @Test void testSimple() { MPJLambdaWrapper lambda = JoinWrappers.lambda(UserTenantDO.class);