mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 17:18:24 +08:00
feat: field query 像 join query 一样支持集合子类型。
This commit is contained in:
parent
d2f22dae60
commit
14a7af8f89
@ -105,6 +105,10 @@ public class ClassUtil {
|
|||||||
|| clazz == double[].class;
|
|| clazz == double[].class;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static boolean canInstance(int mod) {
|
||||||
|
return !Modifier.isAbstract(mod) || !Modifier.isInterface(mod);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static <T> T newInstance(Class<T> clazz) {
|
public static <T> T newInstance(Class<T> clazz) {
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -30,6 +30,10 @@ import java.util.function.Consumer;
|
|||||||
|
|
||||||
public class MapperUtil {
|
public class MapperUtil {
|
||||||
|
|
||||||
|
private MapperUtil() {
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public static <R> void queryFields(BaseMapper<?> mapper, List<R> list, Consumer<FieldQueryBuilder<R>>[] consumers) {
|
public static <R> void queryFields(BaseMapper<?> mapper, List<R> list, Consumer<FieldQueryBuilder<R>>[] consumers) {
|
||||||
if (CollectionUtil.isEmpty(list) || ArrayUtil.isEmpty(consumers) || consumers[0] == null) {
|
if (CollectionUtil.isEmpty(list) || ArrayUtil.isEmpty(consumers) || consumers[0] == null) {
|
||||||
return;
|
return;
|
||||||
@ -47,11 +51,18 @@ public class MapperUtil {
|
|||||||
Class<?> mappingType = fieldWrapper.getMappingType();
|
Class<?> mappingType = fieldWrapper.getMappingType();
|
||||||
|
|
||||||
Object value;
|
Object value;
|
||||||
if (fieldType.isAssignableFrom(List.class)) {
|
if (Set.class.isAssignableFrom(fieldType)) {
|
||||||
value = mapper.selectListByQueryAs(childQuery, mappingType);
|
value = mapper.selectListByQueryAs(childQuery, mappingType);
|
||||||
} else if (fieldType.isAssignableFrom(Set.class)) {
|
if (ClassUtil.canInstance(fieldType.getModifiers())) {
|
||||||
|
value = copyValue(fieldType, value);
|
||||||
|
} else {
|
||||||
|
value = new HashSet<>((Collection<?>) value);
|
||||||
|
}
|
||||||
|
} else if (Collection.class.isAssignableFrom(fieldType)) {
|
||||||
value = mapper.selectListByQueryAs(childQuery, mappingType);
|
value = mapper.selectListByQueryAs(childQuery, mappingType);
|
||||||
value = new HashSet<>((Collection<?>) value);
|
if (ClassUtil.canInstance(fieldType.getModifiers())) {
|
||||||
|
value = copyValue(fieldType, value);
|
||||||
|
}
|
||||||
} else if (fieldType.isArray()) {
|
} else if (fieldType.isArray()) {
|
||||||
value = mapper.selectListByQueryAs(childQuery, mappingType);
|
value = mapper.selectListByQueryAs(childQuery, mappingType);
|
||||||
value = ((List<?>) value).toArray();
|
value = ((List<?>) value).toArray();
|
||||||
@ -63,6 +74,12 @@ public class MapperUtil {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings({"unchecked", "rawtypes"})
|
||||||
|
private static Object copyValue(Class<?> fieldType, Object value) {
|
||||||
|
Collection collection = (Collection) ClassUtil.newInstance(fieldType);
|
||||||
|
collection.addAll((Collection) value);
|
||||||
|
return collection;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user