yulichang 2023-11-02 01:08:57 +08:00
parent 289127c0f6
commit b83963a61c
6 changed files with 86 additions and 30 deletions

View File

@ -28,7 +28,7 @@ import java.util.stream.Collectors;
*
* @author yulichang
*/
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "DuplicatedCode"})
public interface Query<Children> extends Serializable {
@ -129,11 +129,12 @@ public interface Query<Children> extends Serializable {
* @return children
*/
default Children selectAsClass(Class<?> source, Class<?> tag) {
List<SelectCache> normalList = ColumnCache.getListField(source);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(tag);
for (SelectCache cache : normalList) {
if (fieldMap.containsKey(cache.getColumProperty())) {
getSelectColum().add(new SelectNormal(cache, getIndex(), isHasAlias(), getAlias()));
Map<String, SelectCache> normalMap = ColumnCache.getMapField(source);
List<FieldCache> 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();

View File

@ -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<Class<?>, Map<String, FieldCache>> CLASS_FIELD_CACHE = new ConcurrentHashMap<>();
private static final Map<Class<?>, List<FieldCache>> CLASS_FIELD_LIST_CACHE = new ConcurrentHashMap<>();
private static final Map<String, FieldCache> EMPTY_MAP = new HashMap<>();
@ -77,28 +77,32 @@ public final class MPJReflectionKit {
* @param clazz 反射类
*/
public static Map<String, FieldCache> 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<FieldCache> getFieldList(Class<?> clazz) {
if (clazz == null) {
return EMPTY_MAP;
return Collections.emptyList();
}
Map<String, FieldCache> fieldMap = CLASS_FIELD_CACHE.get(clazz);
if (fieldMap != null) {
return fieldMap;
List<FieldCache> fieldList = CLASS_FIELD_LIST_CACHE.get(clazz);
if (fieldList != null) {
return fieldList;
}
Map<String, Field> map = ReflectionKit.getFieldMap(clazz);
Map<String, FieldCache> cache = new HashMap<>();
map.forEach((key, value) -> {
List<Field> list = ReflectionKit.getFieldList(clazz);
List<FieldCache> 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;
}

View File

@ -27,7 +27,7 @@ import java.util.stream.Collectors;
*
* @author yulichang
*/
@SuppressWarnings("unused")
@SuppressWarnings({"unused", "DuplicatedCode"})
public interface Query<Children> extends Serializable {
@ -129,11 +129,12 @@ public interface Query<Children> extends Serializable {
* @return children
*/
default <E> Children selectAsClass(Class<E> source, Class<?> tag) {
List<SelectCache> normalList = ColumnCache.getListField(source);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(tag);
for (SelectCache cache : normalList) {
if (fieldMap.containsKey(cache.getColumProperty())) {
getSelectColum().add(new SelectNormal(cache, getIndex(), isHasAlias(), getAlias()));
Map<String, SelectCache> normalMap = ColumnCache.getMapField(source);
List<FieldCache> 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();

View File

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

View File

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

View File

@ -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<UserTenantDO> lambda = JoinWrappers.lambda(UserTenantDO.class);
lambda.selectAsClass(UserTenantDO.class, UserTenantDTO.class);
List<UserTenantDO> 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<UserTenantDO> lambda1 = JoinWrappers.lambda(UserTenantDO.class);
lambda1.selectAsClass(UserTenantDO.class, UserTenantDescDTO.class);
List<UserTenantDO> list1 = userTenantMapper.selectJoinList(UserTenantDO.class,lambda1);
assert list1.size() == 5 && list1.get(0).getIdea() != null;
}
@Test
void testSimple() {
MPJLambdaWrapper<UserTenantDO> lambda = JoinWrappers.lambda(UserTenantDO.class);