泛型优化

This commit is contained in:
yulichang 2023-03-27 10:59:46 +08:00
parent a229521dd7
commit b6dcc7c1c6
14 changed files with 147 additions and 113 deletions

View File

@ -9,6 +9,7 @@ import com.github.yulichang.query.MPJQueryWrapper;
import com.github.yulichang.toolkit.Constant;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.TableHelper;
import com.github.yulichang.toolkit.support.FieldCache;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.github.yulichang.wrapper.resultmap.Label;
import com.github.yulichang.wrapper.resultmap.Result;
@ -168,7 +169,7 @@ public class MPJInterceptor implements Interceptor {
return result;
}
MPJLambdaWrapper wrapper = (MPJLambdaWrapper) obj;
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(resultType);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(resultType);
List<Select> columnList = wrapper.getSelectColumns();
//移除对多查询列为了可重复使用wrapper
columnList.removeIf(Select::isLabel);
@ -176,19 +177,19 @@ public class MPJInterceptor implements Interceptor {
Set<String> columnSet = new HashSet<>();
for (Select i : columnList) {
if (i.isHasAlias()) {
Field field = fieldMap.get(i.getAlias());
FieldCache field = fieldMap.get(i.getAlias());
columnSet.add(i.getAlias());
if (Objects.nonNull(field)) {
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), i.getAlias(),
i.getAlias(), MPJReflectionKit.getFieldType(resultType, i.getAlias()));
i.getAlias(), field.getType());
resultMappings.add(selectToResult(wrapper.getEntityClass(), i, field.getType(), builder));
}
} else {
Field field = fieldMap.get(i.getColumProperty());
FieldCache field = fieldMap.get(i.getColumProperty());
columnSet.add(i.getTagColumn());
if (Objects.nonNull(field)) {
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), i.getColumProperty(),
i.getTagColumn(), MPJReflectionKit.getFieldType(resultType, i.getColumProperty()));
i.getTagColumn(), field.getType());
resultMappings.add(selectToResult(wrapper.getEntityClass(), i, field.getType(), builder));
}
}
@ -233,18 +234,18 @@ public class MPJInterceptor implements Interceptor {
List<ResultMapping> childMapping = new ArrayList<>(resultList.size());
for (Result r : resultList) {
childId.append("(");
Map<String, Field> ofTypeField = MPJReflectionKit.getFieldMap(mybatisLabel.getOfType());
Map<String, FieldCache> ofTypeField = MPJReflectionKit.getFieldMap(mybatisLabel.getOfType());
//列名去重
String columnName = StringUtils.getTargetColumn(r.getSelectNormal().getColumn());
SelectLabel label;
Field field = ofTypeField.get(r.getProperty());
FieldCache field = ofTypeField.get(r.getProperty());
String index = r.getIndex();
if (columnSet.contains(columnName)) {
columnName = getColumn(columnSet, columnName, 0);
label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, columnName, StringUtils.isNotBlank(index), index);
label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), columnName, StringUtils.isNotBlank(index), index);
} else {
columnSet.add(columnName);
label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), field, StringUtils.isNotBlank(index), index);
label = new SelectLabel(r.getSelectNormal(), null, mybatisLabel.getOfType(), StringUtils.isNotBlank(index), index);
}
columnList.add(label);
ResultMapping.Builder builder = new ResultMapping.Builder(ms.getConfiguration(), r.getProperty(), columnName, r.getJavaType());

View File

@ -18,14 +18,14 @@ public class JoinWrappers {
}
/**
* JoinWrappers.<UserDO>query(User.class)
* JoinWrappers.query(User.class)
*/
public static <T> MPJQueryWrapper<T> query(Class<T> clazz) {
return new MPJQueryWrapper<>(clazz);
}
/**
* JoinWrappers.<UserDO>query(user)
* JoinWrappers.query(user)
*/
public static <T> MPJQueryWrapper<T> query(T entity) {
return new MPJQueryWrapper<>(entity);
@ -39,14 +39,14 @@ public class JoinWrappers {
}
/**
* JoinWrappers.<UserDO>lambda(User.class)
* JoinWrappers.lambda(User.class)
*/
public static <T> MPJLambdaWrapper<T> lambda(Class<T> clazz) {
return new MPJLambdaWrapper<>(clazz);
}
/**
* JoinWrappers.<UserDO>lambda(user)
* JoinWrappers.lambda(user)
*/
public static <T> MPJLambdaWrapper<T> lambda(T entity) {
return new MPJLambdaWrapper<>(entity);

View File

@ -5,7 +5,6 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.support.*;
import org.apache.ibatis.reflection.property.PropertyNamer;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.Map;
@ -20,7 +19,7 @@ public final class LambdaUtils {
LambdaMeta extract = extract(fn);
String name = PropertyNamer.methodToProperty(extract.getImplMethodName());
if (Character.isUpperCase(name.charAt(0))) {
Map<String, Field> map = MPJReflectionKit.getFieldMap(extract.getInstantiatedClass());
Map<String, FieldCache> map = MPJReflectionKit.getFieldMap(extract.getInstantiatedClass());
if (map.containsKey(name)) {
return name;
} else {

View File

@ -1,6 +1,7 @@
package com.github.yulichang.toolkit;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.github.yulichang.toolkit.support.FieldCache;
import org.apache.ibatis.reflection.Reflector;
import java.lang.reflect.Field;
@ -10,6 +11,7 @@ import java.lang.reflect.WildcardType;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
/**
@ -21,12 +23,9 @@ import java.util.concurrent.ConcurrentHashMap;
@SuppressWarnings("unused")
public final class MPJReflectionKit {
private static final Map<Class<?>, Map<String, Field>> CLASS_FIELD_CACHE = new ConcurrentHashMap<>();
private static final Map<Class<?>, Map<String, FieldCache>> CLASS_FIELD_CACHE = new ConcurrentHashMap<>();
//mybatis 缓存
private static final Map<Class<?>, Reflector> CLASS_REFLECTOR_CACHE = new ConcurrentHashMap<>();
private static final Map<String, Field> EMPTY_MAP = new HashMap<>();
private static final Map<String, FieldCache> EMPTY_MAP = new HashMap<>();
@Deprecated
@ -77,29 +76,30 @@ public final class MPJReflectionKit {
*
* @param clazz 反射类
*/
public static Map<String, Field> getFieldMap(Class<?> clazz) {
public static Map<String, FieldCache> getFieldMap(Class<?> clazz) {
if (clazz == null) {
return EMPTY_MAP;
}
Map<String, Field> fieldMap = CLASS_FIELD_CACHE.get(clazz);
Map<String, FieldCache> fieldMap = CLASS_FIELD_CACHE.get(clazz);
if (fieldMap != null) {
return fieldMap;
}
Map<String, Field> map = ReflectionKit.getFieldMap(clazz);
CLASS_FIELD_CACHE.put(clazz, map);
return map;
}
public static Class<?> getFieldType(Class<?> clazz, String name) {
if (clazz == null) {
return null;
}
Reflector reflector = CLASS_REFLECTOR_CACHE.computeIfAbsent(clazz, Reflector::new);
Class<?> getterType = reflector.getGetterType(name);
if (getterType != null) {
return getterType;
}
return getFieldMap(clazz).get(name).getType();
Map<String, FieldCache> cache = new HashMap<>();
map.forEach((key, value) -> {
FieldCache fieldCache = new FieldCache();
fieldCache.setField(value);
try {
Reflector reflector = new Reflector(clazz);
Class<?> getterType = reflector.getGetterType(key);
fieldCache.setType(Objects.isNull(getterType) ? value.getType() : getterType);
} catch (Throwable throwable) {
fieldCache.setType(value.getType());
}
cache.put(key, fieldCache);
});
CLASS_FIELD_CACHE.put(clazz, cache);
return cache;
}
public static boolean isPrimitiveOrWrapper(Class<?> clazz) {

View File

@ -1,5 +1,11 @@
package com.github.yulichang.toolkit;
/**
* 版本工具类
*
* @author yulichang
* @since 1.4.4
*/
public class VersionUtils {
public static int compare(String v1, String v2) {

View File

@ -0,0 +1,19 @@
package com.github.yulichang.toolkit.support;
import lombok.Data;
import java.lang.reflect.Field;
/**
* 反射字段缓存
*
* @author yulichang
* @since 1.4.5
*/
@Data
public class FieldCache {
private Field field;
private Class<?> type;
}

View File

@ -9,12 +9,12 @@ import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.TableHelper;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.toolkit.support.FieldCache;
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
import com.github.yulichang.wrapper.enums.DefaultFuncEnum;
import com.github.yulichang.wrapper.segments.*;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
@ -112,7 +112,7 @@ public interface Query<Children> extends Serializable {
*/
default <E> Children selectAsClass(Class<E> source, Class<?> tag) {
List<SelectCache> normalList = ColumnCache.getListField(source);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(tag);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(tag);
for (SelectCache cache : normalList) {
if (fieldMap.containsKey(cache.getColumProperty())) {
getSelectColum().add(new SelectNormal(cache, getIndex(), isHasAlias(), getAlias()));

View File

@ -5,12 +5,12 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.support.FieldCache;
import com.github.yulichang.wrapper.resultmap.Label;
import com.github.yulichang.wrapper.resultmap.MFunc;
import com.github.yulichang.wrapper.resultmap.MybatisLabel;
import com.github.yulichang.wrapper.resultmap.MybatisLabelFree;
import java.lang.reflect.Field;
import java.util.Collection;
import java.util.Map;
@ -20,7 +20,7 @@ import java.util.Map;
* @author yulichang
* @since 1.3.0
*/
@SuppressWarnings({"unchecked", "unused"})
@SuppressWarnings({"unchecked", "unused", "DuplicatedCode"})
public interface QueryLabel<Children> {
void addLabel(Label<?> label);
@ -57,9 +57,9 @@ public interface QueryLabel<Children> {
default <S, C, Z, F extends java.util.Collection<?>> Children selectCollection(String prefix, Class<C> child, SFunction<S, F> dtoField) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Field field = fieldMap.get(dtoFieldName);
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
FieldCache field = fieldMap.get(dtoFieldName);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
MybatisLabel.Builder<C, Z> builder;
if (genericType == null || genericType.isAssignableFrom(child)) {
//找不到集合泛型 List List<?> List<Object> 直接查询数据库实体
@ -111,9 +111,9 @@ public interface QueryLabel<Children> {
//自由映射必须存在泛型Z
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
//获取集合泛型
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
Class<Z> ofType = (Class<Z>) genericType;
MybatisLabelFree.Builder<Z> builder = new MybatisLabelFree.Builder<>(dtoFieldName, field.getType(), ofType);
MybatisLabelFree.Builder<Z> czBuilder = collection.apply(builder);
@ -127,9 +127,9 @@ public interface QueryLabel<Children> {
MFunc<MybatisLabel.Builder<C, Z>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
//获取集合泛型
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
Class<Z> ofType = (Class<Z>) genericType;
MybatisLabel.Builder<C, Z> builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType(), ofType, false);
MybatisLabel.Builder<C, Z> czBuilder = collection.apply(builder);
@ -149,8 +149,8 @@ public interface QueryLabel<Children> {
default <S, C, F> Children selectAssociation(String prefix, Class<C> child, SFunction<S, F> dtoField) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Field field = fieldMap.get(dtoFieldName);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
FieldCache field = fieldMap.get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<C, F> builder;
builder = new MybatisLabel.Builder<>(StringUtils.isBlank(prefix) ? null : prefix,
@ -173,7 +173,7 @@ public interface QueryLabel<Children> {
MFunc<MybatisLabelFree.Builder<F>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabelFree.Builder<F> builder = new MybatisLabelFree.Builder<>(dtoFieldName, field.getType(), (Class<F>) field.getType());
MybatisLabelFree.Builder<F> cfBuilder = collection.apply(builder);
@ -185,7 +185,7 @@ public interface QueryLabel<Children> {
MFunc<MybatisLabel.Builder<C, F>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<C, F> builder = new MybatisLabel.Builder<>(StringUtils.isBlank(prefix) ? null : prefix,
dtoFieldName, child, field.getType(), (Class<F>) field.getType(), false);

View File

@ -8,10 +8,10 @@ import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.TableHelper;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.toolkit.support.FieldCache;
import com.github.yulichang.wrapper.segments.SelectCache;
import lombok.Getter;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
@ -44,7 +44,7 @@ public class MybatisLabel<E, T> implements Label<T> {
private MybatisLabel() {
}
@SuppressWarnings({"unused", "unchecked"})
@SuppressWarnings({"unused", "unchecked", "DuplicatedCode"})
public static class Builder<E, T> {
private final MybatisLabel<E, T> mybatisLabel;
@ -124,9 +124,9 @@ public class MybatisLabel<E, T> implements Label<T> {
public <A, R, B extends Collection<R>> Builder<E, T> collection(String prefix, Class<A> entityClass, SFunction<T, B> func) {
String dtoFieldName = LambdaUtils.getName(func);
Class<T> dtoClass = LambdaUtils.getEntityClass(func);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Field field = fieldMap.get(dtoFieldName);
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
FieldCache field = fieldMap.get(dtoFieldName);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
MybatisLabel.Builder<A, R> builder;
if (genericType == null || genericType.isAssignableFrom(entityClass)) {
//找不到集合泛型 List List<?> List<Object> 直接查询数据库实体
@ -150,9 +150,9 @@ public class MybatisLabel<E, T> implements Label<T> {
MFunc<MybatisLabelFree.Builder<R>> mFunc) {
String dtoFieldName = LambdaUtils.getName(func);
Class<T> dtoClass = LambdaUtils.getEntityClass(func);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
//获取集合泛型
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
Class<R> ofType = (Class<R>) genericType;
MybatisLabelFree.Builder<R> builder = new MybatisLabelFree.Builder<>(dtoFieldName, field.getType(), ofType);
mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build());
@ -168,9 +168,9 @@ public class MybatisLabel<E, T> implements Label<T> {
MFunc<Builder<A, R>> mFunc) {
String dtoFieldName = LambdaUtils.getName(func);
Class<T> dtoClass = LambdaUtils.getEntityClass(func);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
//获取集合泛型
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
Class<R> ofType = (Class<R>) genericType;
MybatisLabel.Builder<A, R> builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, entityClass, field.getType(), ofType, false);
mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build());
@ -186,9 +186,9 @@ public class MybatisLabel<E, T> implements Label<T> {
*/
public <A, B> Builder<E, T> association(String index, Class<A> child, SFunction<T, B> dtoField) {
Class<T> dtoClass = LambdaUtils.getEntityClass(dtoField);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
String dtoFieldName = LambdaUtils.getName(dtoField);
Field field = fieldMap.get(dtoFieldName);
FieldCache field = fieldMap.get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<A, B> builder;
builder = new MybatisLabel.Builder<>(index, dtoFieldName, child, field.getType(), (Class<B>) field.getType(), true);
@ -208,7 +208,7 @@ public class MybatisLabel<E, T> implements Label<T> {
MFunc<MybatisLabelFree.Builder<B>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<T> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabelFree.Builder<B> builder = new MybatisLabelFree.Builder<>(dtoFieldName, field.getType(), (Class<B>) field.getType());
mybatisLabel.mybatisLabels.add(collection.apply(builder).build());
@ -222,7 +222,7 @@ public class MybatisLabel<E, T> implements Label<T> {
MFunc<MybatisLabel.Builder<A, B>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<T> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<A, B> builder = new MybatisLabel.Builder<>(index, dtoFieldName, child, field.getType(), (Class<B>) field.getType(), false);
mybatisLabel.mybatisLabels.add(collection.apply(builder).build());
@ -239,7 +239,7 @@ public class MybatisLabel<E, T> implements Label<T> {
private void autoBuild(boolean auto, Class<E> entityClass, Class<T> tagClass) {
TableInfo tableInfo = TableHelper.get(entityClass);
Assert.notNull(tableInfo, "table not find by class <%s>", entityClass.getSimpleName());
Map<String, Field> tagMap = MPJReflectionKit.getFieldMap(tagClass);
Map<String, FieldCache> tagMap = MPJReflectionKit.getFieldMap(tagClass);
if (auto && !tagMap.isEmpty()) {
List<SelectCache> listField = ColumnCache.getListField(entityClass);
if (entityClass.isAssignableFrom(tagClass)) {
@ -255,7 +255,7 @@ public class MybatisLabel<E, T> implements Label<T> {
}).collect(Collectors.toList()));
} else {
for (SelectCache s : listField) {
Field field = tagMap.get(s.getColumProperty());
FieldCache field = tagMap.get(s.getColumProperty());
if (Objects.nonNull(field)) {
Result result = new Result();
result.setId(s.isPk());

View File

@ -8,10 +8,10 @@ import com.github.yulichang.toolkit.LambdaUtils;
import com.github.yulichang.toolkit.MPJReflectionKit;
import com.github.yulichang.toolkit.TableHelper;
import com.github.yulichang.toolkit.support.ColumnCache;
import com.github.yulichang.toolkit.support.FieldCache;
import com.github.yulichang.wrapper.segments.SelectCache;
import lombok.Getter;
import java.lang.reflect.Field;
import java.util.*;
/**
@ -136,9 +136,9 @@ public class MybatisLabelFree<T> implements Label<T> {
public <A, R, B extends Collection<R>> Builder<T> collection(String prefix, Class<A> entityClass, SFunction<T, B> func) {
String dtoFieldName = LambdaUtils.getName(func);
Class<T> dtoClass = LambdaUtils.getEntityClass(func);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Field field = fieldMap.get(dtoFieldName);
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
FieldCache field = fieldMap.get(dtoFieldName);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
MybatisLabel.Builder<A, R> builder;
if (genericType == null || genericType.isAssignableFrom(entityClass)) {
//找不到集合泛型 List List<?> List<Object> 直接查询数据库实体
@ -159,9 +159,9 @@ public class MybatisLabelFree<T> implements Label<T> {
MFunc<MybatisLabelFree.Builder<R>> mFunc) {
String dtoFieldName = LambdaUtils.getName(func);
Class<T> dtoClass = LambdaUtils.getEntityClass(func);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
//获取集合泛型
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
Class<R> ofType = (Class<R>) genericType;
MybatisLabelFree.Builder<R> builder = new MybatisLabelFree.Builder<>(dtoFieldName, field.getType(), ofType);
mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build());
@ -177,9 +177,9 @@ public class MybatisLabelFree<T> implements Label<T> {
MFunc<MybatisLabel.Builder<A, R>> mFunc) {
String dtoFieldName = LambdaUtils.getName(func);
Class<T> dtoClass = LambdaUtils.getEntityClass(func);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
//获取集合泛型
Class<?> genericType = MPJReflectionKit.getGenericType(field);
Class<?> genericType = MPJReflectionKit.getGenericType(field.getField());
Class<R> ofType = (Class<R>) genericType;
MybatisLabel.Builder<A, R> builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, entityClass, field.getType(), ofType, false);
mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build());
@ -195,9 +195,9 @@ public class MybatisLabelFree<T> implements Label<T> {
*/
public <A, B> Builder<T> association(String index, Class<A> child, SFunction<T, B> dtoField) {
Class<T> dtoClass = LambdaUtils.getEntityClass(dtoField);
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
Map<String, FieldCache> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
String dtoFieldName = LambdaUtils.getName(dtoField);
Field field = fieldMap.get(dtoFieldName);
FieldCache field = fieldMap.get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<A, B> builder;
builder = new MybatisLabel.Builder<>(index, dtoFieldName, child, field.getType(), (Class<B>) field.getType(), true);
@ -217,7 +217,7 @@ public class MybatisLabelFree<T> implements Label<T> {
MFunc<MybatisLabel.Builder<A, B>> collection) {
String dtoFieldName = LambdaUtils.getName(dtoField);
Class<T> dtoClass = LambdaUtils.getEntityClass(dtoField);
Field field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
FieldCache field = MPJReflectionKit.getFieldMap(dtoClass).get(dtoFieldName);
Assert.isFalse(Collection.class.isAssignableFrom(field.getType()), "association 不支持集合类");
MybatisLabel.Builder<A, B> builder = new MybatisLabel.Builder<>(index, dtoFieldName, child, field.getType(), (Class<B>) field.getType(), false);
mybatisLabel.mybatisLabels.add(collection.apply(builder).build());
@ -236,10 +236,10 @@ public class MybatisLabelFree<T> implements Label<T> {
}
private void allBuild(String prefix, Class<?> entityClass) {
Map<String, Field> tagMap = MPJReflectionKit.getFieldMap(mybatisLabel.getOfType());
Map<String, FieldCache> tagMap = MPJReflectionKit.getFieldMap(mybatisLabel.getOfType());
List<SelectCache> listField = ColumnCache.getListField(entityClass);
for (SelectCache s : listField) {
Field field = tagMap.get(s.getColumProperty());
FieldCache field = tagMap.get(s.getColumProperty());
if (Objects.nonNull(field)) {
Result result = new Result();
result.setIndex(prefix);

View File

@ -7,8 +7,6 @@ import com.github.yulichang.wrapper.enums.BaseFuncEnum;
import lombok.Getter;
import org.apache.ibatis.type.TypeHandler;
import java.lang.reflect.Field;
/**
* 对多或对一查询列
*
@ -24,8 +22,6 @@ public class SelectLabel implements Select {
private final Class<?> tagClass;
private final Field tagField;
private final boolean hasAlias;
private final String alias;
@ -34,22 +30,20 @@ public class SelectLabel implements Select {
private final String tableAlias;
public SelectLabel(SelectCache cache, Integer index, Class<?> tagClass, Field tagField, boolean hasTableAlias, String tableAlias) {
public SelectLabel(SelectCache cache, Integer index, Class<?> tagClass, boolean hasTableAlias, String tableAlias) {
this.cache = cache;
this.index = index;
this.tagClass = tagClass;
this.tagField = tagField;
this.hasAlias = false;
this.alias = null;
this.hasTableAlias = hasTableAlias;
this.tableAlias = tableAlias;
}
public SelectLabel(SelectCache cache, Integer index, Class<?> tagClass, Field tagField, String column, boolean hasTableAlias, String tableAlias) {
public SelectLabel(SelectCache cache, Integer index, Class<?> tagClass, String column, boolean hasTableAlias, String tableAlias) {
this.cache = cache;
this.index = index;
this.tagClass = tagClass;
this.tagField = tagField;
this.hasAlias = true;
this.alias = column;
this.hasTableAlias = hasTableAlias;

View File

@ -1,33 +1,14 @@
package com.github.yulichang.test.join.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import com.baomidou.mybatisplus.annotation.TableName;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;
import lombok.experimental.Accessors;
import java.io.Serializable;
@Data
@ToString
@Accessors(chain = true)
@EqualsAndHashCode
@TableName("address")
public class AddressDO implements Serializable {
public class AddressDO extends AddressGeneric<Integer, Integer, Integer, String, String, Boolean> implements Serializable {
@TableId
private Integer id;
private Integer userId;
private Integer areaId;
private String tel;
private String address;
@TableLogic
private Boolean del;
}

View File

@ -0,0 +1,24 @@
package com.github.yulichang.test.join.entity;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Data;
@Data
public class AddressGeneric<ID, USER_ID, AREA_ID, TEL, ADDRESS, DEL> {
@TableId
private ID id;
private USER_ID userId;
private AREA_ID areaId;
private TEL tel;
private ADDRESS address;
@TableLogic
private DEL del;
}

View File

@ -6,7 +6,11 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.test.join.dto.AddressDTO;
import com.github.yulichang.test.join.dto.UserDTO;
import com.github.yulichang.test.join.entity.*;
import com.github.yulichang.test.join.entity.AddressDO;
import com.github.yulichang.test.join.entity.AreaDO;
import com.github.yulichang.test.join.entity.UserDO;
import com.github.yulichang.test.join.entity.UserDto;
import com.github.yulichang.test.join.mapper.AddressMapper;
import com.github.yulichang.test.join.mapper.UserDTOMapper;
import com.github.yulichang.test.join.mapper.UserMapper;
import com.github.yulichang.test.util.ThreadLocalUtils;
@ -21,6 +25,7 @@ import org.springframework.jdbc.BadSqlGrammarException;
import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* 连表测试类
@ -39,6 +44,9 @@ class LambdaWrapperTest {
@Autowired
private UserDTOMapper userDTOMapper;
@Autowired
private AddressMapper addressMapper;
@Test
void testJoin() {
@ -694,11 +702,13 @@ class LambdaWrapperTest {
*/
@Test
void testGeneric() {
MPJLambdaWrapper<UserDO> wrapper = new MPJLambdaWrapper<UserDO>()
.selectAll(UserDO.class)
.le(UserDO::getId, 10000)
.orderByDesc(UserDO::getId);
List<UserTTT> list = userMapper.selectJoinList(UserTTT.class, wrapper);
MPJLambdaWrapper<AddressDO> wrapper = new MPJLambdaWrapper<AddressDO>()
.selectAll(AddressDO.class)
.le(AddressDO::getId, 10000)
.orderByDesc(AddressDO::getId);
List<AddressDTO> list = addressMapper.selectJoinList(AddressDTO.class, wrapper);
assert Objects.equals("[AddressDTO(id=22, userId=22, areaId=10022, tel=10000000022, address=朝阳22, del=false, areaList=null, area=null), AddressDTO(id=21, userId=21, areaId=10021, tel=10000000021, address=朝阳21, del=false, areaList=null, area=null), AddressDTO(id=20, userId=20, areaId=10020, tel=10000000020, address=朝阳20, del=false, areaList=null, area=null), AddressDTO(id=19, userId=19, areaId=10019, tel=10000000019, address=朝阳19, del=false, areaList=null, area=null), AddressDTO(id=18, userId=18, areaId=10018, tel=10000000018, address=朝阳18, del=false, areaList=null, area=null), AddressDTO(id=17, userId=17, areaId=10017, tel=10000000017, address=朝阳17, del=false, areaList=null, area=null), AddressDTO(id=16, userId=16, areaId=10016, tel=10000000016, address=朝阳16, del=false, areaList=null, area=null), AddressDTO(id=15, userId=15, areaId=10015, tel=10000000015, address=朝阳15, del=false, areaList=null, area=null), AddressDTO(id=14, userId=14, areaId=10014, tel=10000000014, address=朝阳14, del=false, areaList=null, area=null), AddressDTO(id=13, userId=13, areaId=10013, tel=10000000013, address=朝阳13, del=false, areaList=null, area=null), AddressDTO(id=12, userId=12, areaId=10012, tel=10000000012, address=朝阳12, del=false, areaList=null, area=null), AddressDTO(id=11, userId=11, areaId=10011, tel=10000000011, address=朝阳11, del=false, areaList=null, area=null), AddressDTO(id=10, userId=10, areaId=10010, tel=10000000010, address=朝阳10, del=false, areaList=null, area=null), AddressDTO(id=5, userId=1, areaId=10005, tel=10000000005, address=朝阳05, del=false, areaList=null, area=null), AddressDTO(id=4, userId=1, areaId=10004, tel=10000000004, address=朝阳04, del=false, areaList=null, area=null), AddressDTO(id=3, userId=1, areaId=10003, tel=10000000003, address=朝阳03, del=false, areaList=null, area=null), AddressDTO(id=2, userId=1, areaId=10002, tel=10000000002, address=朝阳02, del=false, areaList=null, area=null), AddressDTO(id=1, userId=1, areaId=10001, tel=10000000001, address=朝阳01, del=false, areaList=null, area=null)]"
, list.toString());
}
/**