mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
This commit is contained in:
parent
289127c0f6
commit
b83963a61c
@ -28,7 +28,7 @@ import java.util.stream.Collectors;
|
|||||||
*
|
*
|
||||||
* @author yulichang
|
* @author yulichang
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings({"unused", "DuplicatedCode"})
|
||||||
public interface Query<Children> extends Serializable {
|
public interface Query<Children> extends Serializable {
|
||||||
|
|
||||||
|
|
||||||
@ -129,11 +129,12 @@ public interface Query<Children> extends Serializable {
|
|||||||
* @return children
|
* @return children
|
||||||
*/
|
*/
|
||||||
default Children selectAsClass(Class<?> source, Class<?> tag) {
|
default Children selectAsClass(Class<?> source, Class<?> tag) {
|
||||||
List<SelectCache> normalList = ColumnCache.getListField(source);
|
Map<String, SelectCache> normalMap = ColumnCache.getMapField(source);
|
||||||
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(tag);
|
List<FieldCache> fieldList = MPJReflectionKit.getFieldList(tag);
|
||||||
for (SelectCache cache : normalList) {
|
for (FieldCache cache : fieldList) {
|
||||||
if (fieldMap.containsKey(cache.getColumProperty())) {
|
if (normalMap.containsKey(cache.getField().getName())) {
|
||||||
getSelectColum().add(new SelectNormal(cache, getIndex(), isHasAlias(), getAlias()));
|
SelectCache selectCache = normalMap.get(cache.getField().getName());
|
||||||
|
getSelectColum().add(new SelectNormal(selectCache, getIndex(), isHasAlias(), getAlias()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return getChildren();
|
return getChildren();
|
||||||
|
@ -8,11 +8,10 @@ import java.lang.reflect.Field;
|
|||||||
import java.lang.reflect.ParameterizedType;
|
import java.lang.reflect.ParameterizedType;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.lang.reflect.WildcardType;
|
import java.lang.reflect.WildcardType;
|
||||||
import java.util.HashMap;
|
import java.util.*;
|
||||||
import java.util.IdentityHashMap;
|
|
||||||
import java.util.Map;
|
|
||||||
import java.util.Objects;
|
|
||||||
import java.util.concurrent.ConcurrentHashMap;
|
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 {
|
public final class MPJReflectionKit {
|
||||||
|
|
||||||
private static final Map<Class<?>, Map<String, FieldCache>> CLASS_FIELD_CACHE = new ConcurrentHashMap<>();
|
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<>();
|
private static final Map<String, FieldCache> EMPTY_MAP = new HashMap<>();
|
||||||
|
|
||||||
@ -77,28 +77,32 @@ public final class MPJReflectionKit {
|
|||||||
* @param clazz 反射类
|
* @param clazz 反射类
|
||||||
*/
|
*/
|
||||||
public static Map<String, FieldCache> getFieldMap(Class<?> 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) {
|
if (clazz == null) {
|
||||||
return EMPTY_MAP;
|
return Collections.emptyList();
|
||||||
}
|
}
|
||||||
Map<String, FieldCache> fieldMap = CLASS_FIELD_CACHE.get(clazz);
|
List<FieldCache> fieldList = CLASS_FIELD_LIST_CACHE.get(clazz);
|
||||||
if (fieldMap != null) {
|
if (fieldList != null) {
|
||||||
return fieldMap;
|
return fieldList;
|
||||||
}
|
}
|
||||||
Map<String, Field> map = ReflectionKit.getFieldMap(clazz);
|
List<Field> list = ReflectionKit.getFieldList(clazz);
|
||||||
Map<String, FieldCache> cache = new HashMap<>();
|
List<FieldCache> cache = list.stream().map(f -> {
|
||||||
map.forEach((key, value) -> {
|
|
||||||
FieldCache fieldCache = new FieldCache();
|
FieldCache fieldCache = new FieldCache();
|
||||||
fieldCache.setField(value);
|
fieldCache.setField(f);
|
||||||
try {
|
try {
|
||||||
Reflector reflector = new Reflector(clazz);
|
Reflector reflector = new Reflector(clazz);
|
||||||
Class<?> getterType = reflector.getGetterType(key);
|
Class<?> getterType = reflector.getGetterType(f.getName());
|
||||||
fieldCache.setType(Objects.isNull(getterType) ? value.getType() : getterType);
|
fieldCache.setType(Objects.isNull(getterType) ? f.getType() : getterType);
|
||||||
} catch (Throwable throwable) {
|
} catch (Throwable throwable) {
|
||||||
fieldCache.setType(value.getType());
|
fieldCache.setType(f.getType());
|
||||||
}
|
}
|
||||||
cache.put(key, fieldCache);
|
return fieldCache;
|
||||||
});
|
}).collect(Collectors.toList());
|
||||||
CLASS_FIELD_CACHE.put(clazz, cache);
|
CLASS_FIELD_LIST_CACHE.put(clazz, cache);
|
||||||
return cache;
|
return cache;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,7 +27,7 @@ import java.util.stream.Collectors;
|
|||||||
*
|
*
|
||||||
* @author yulichang
|
* @author yulichang
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unused")
|
@SuppressWarnings({"unused", "DuplicatedCode"})
|
||||||
public interface Query<Children> extends Serializable {
|
public interface Query<Children> extends Serializable {
|
||||||
|
|
||||||
|
|
||||||
@ -129,11 +129,12 @@ public interface Query<Children> extends Serializable {
|
|||||||
* @return children
|
* @return children
|
||||||
*/
|
*/
|
||||||
default <E> Children selectAsClass(Class<E> source, Class<?> tag) {
|
default <E> Children selectAsClass(Class<E> source, Class<?> tag) {
|
||||||
List<SelectCache> normalList = ColumnCache.getListField(source);
|
Map<String, SelectCache> normalMap = ColumnCache.getMapField(source);
|
||||||
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(tag);
|
List<FieldCache> fieldList = MPJReflectionKit.getFieldList(tag);
|
||||||
for (SelectCache cache : normalList) {
|
for (FieldCache cache : fieldList) {
|
||||||
if (fieldMap.containsKey(cache.getColumProperty())) {
|
if (normalMap.containsKey(cache.getField().getName())) {
|
||||||
getSelectColum().add(new SelectNormal(cache, getIndex(), isHasAlias(), getAlias()));
|
SelectCache selectCache = normalMap.get(cache.getField().getName());
|
||||||
|
getSelectColum().add(new SelectNormal(selectCache, getIndex(), isHasAlias(), getAlias()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return getChildren();
|
return getChildren();
|
||||||
|
@ -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;
|
||||||
|
}
|
@ -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;
|
||||||
|
}
|
@ -8,6 +8,8 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|||||||
import com.github.yulichang.adapter.base.tookit.VersionUtils;
|
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.dto.UserTenantDTO;
|
||||||
|
import com.github.yulichang.test.join.dto.UserTenantDescDTO;
|
||||||
import com.github.yulichang.test.join.entity.*;
|
import com.github.yulichang.test.join.entity.*;
|
||||||
import com.github.yulichang.test.join.mapper.*;
|
import com.github.yulichang.test.join.mapper.*;
|
||||||
import com.github.yulichang.test.util.Reset;
|
import com.github.yulichang.test.util.Reset;
|
||||||
@ -59,6 +61,22 @@ class LambdaWrapperTest {
|
|||||||
Reset.reset();
|
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
|
@Test
|
||||||
void testSimple() {
|
void testSimple() {
|
||||||
MPJLambdaWrapper<UserTenantDO> lambda = JoinWrappers.lambda(UserTenantDO.class);
|
MPJLambdaWrapper<UserTenantDO> lambda = JoinWrappers.lambda(UserTenantDO.class);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user