1.2.5版本临时分支

This commit is contained in:
yulichang 2022-11-04 18:10:40 +08:00
parent f020d64af3
commit ece59bb9e5
3 changed files with 59 additions and 7 deletions

View File

@ -160,10 +160,10 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
builder = new Collection.Builder<>(dtoFieldName, child, field.getType());
} else {
Class<Z> ofType = (Class<Z>) genericType;
if (ReflectionKit.isPrimitiveOrWrapper(ofType) || fieldMap.isEmpty()) {
if (ReflectionKit.isPrimitiveOrWrapper(ofType)) {
throw new MPJException("collection 不支持基本数据类型");
}
builder = new Collection.Builder<>(dtoFieldName, child, field.getType(), ofType);
builder = new Collection.Builder<>(dtoFieldName, child, field.getType(), ofType, true);
}
this.resultMapCollection.add(builder.build());
return typedThis;
@ -178,7 +178,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
//获取集合泛型
Class<?> genericType = ReflectionKit.getGenericType(field);
Class<Z> ofType = (Class<Z>) genericType;
Collection.Builder<C, Z> builder = new Collection.Builder<>(dtoFieldName, child, field.getType(), ofType);
Collection.Builder<C, Z> builder = new Collection.Builder<>(dtoFieldName, child, field.getType(), ofType, false);
this.resultMapCollection.add(collection.apply(builder).build());
return typedThis;
}

View File

@ -1,9 +1,17 @@
package com.github.yulichang.wrapper.resultmap;
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfo;
import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.github.yulichang.toolkit.ReflectionKit;
import lombok.Getter;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* collection 标签 目前先支持这几个属性 后续在扩展
@ -45,7 +53,7 @@ public class Collection<E, T> {
collection.javaType = javaType;
collection.ofType = (Class<T>) entityClass;
collection.resultList = new ArrayList<>();
// TODO 构建entityClass所有的字段
autoBuild(true, entityClass, (Class<T>) entityClass);
}
/**
@ -55,14 +63,16 @@ public class Collection<E, T> {
* @param entityClass 数据库实体类
* @param javaType javaType
* @param ofType 映射类
* @param auto 自动映射数据库实体对应的字段
*/
public Builder(String property, Class<E> entityClass, Class<?> javaType, Class<T> ofType) {
public Builder(String property, Class<E> entityClass, Class<?> javaType, Class<T> ofType, boolean auto) {
this.collection = new Collection<>();
collection.property = property;
collection.entityClass = entityClass;
collection.javaType = javaType;
collection.ofType = ofType;
collection.resultList = new ArrayList<>();
autoBuild(auto, entityClass, ofType);
}
public Builder<E, T> id(MFunc<Result.Builder<E, T>> result) {
@ -80,5 +90,44 @@ public class Collection<E, T> {
public Collection<E, T> build() {
return collection;
}
private void autoBuild(boolean auto, Class<E> entityClass, Class<T> tagClass) {
TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass);
Map<String, Field> tagMap = ReflectionKit.getFieldMap(tagClass);
if (auto && !tagMap.isEmpty()) {
Function<TableFieldInfo, Result> build = field -> {
Result result = new Result();
result.setId(false);
result.setColumn(field.getColumn());
result.setProperty(field.getProperty());
result.setJavaType(field.getField().getType());
result.setJdbcType(field.getJdbcType());
result.setTypeHandle(field.getTypeHandler());
return result;
};
if (entityClass == tagClass) {
if (tableInfo.havePK()) {
collection.resultList.add(pkBuild(tableInfo));
}
collection.resultList.addAll(tableInfo.getFieldList().stream().map(build).collect(Collectors.toList()));
} else {
if (tableInfo.havePK() && tagMap.containsKey(tableInfo.getKeyProperty())) {
collection.resultList.add(pkBuild(tableInfo));
} else {
collection.resultList.addAll(tableInfo.getFieldList().stream().filter(i ->
tagMap.containsKey(i.getProperty())).map(build).collect(Collectors.toList()));
}
}
}
}
private Result pkBuild(TableInfo tableInfo) {
Result result = new Result();
result.setId(true);
result.setColumn(tableInfo.getKeyColumn());
result.setProperty(tableInfo.getKeyProperty());
result.setJavaType(tableInfo.getKeyType());
return result;
}
}
}

View File

@ -6,7 +6,9 @@ import com.baomidou.mybatisplus.core.metadata.TableInfoHelper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.github.yulichang.toolkit.LambdaUtils;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.TypeHandler;
@ -17,6 +19,7 @@ import org.apache.ibatis.type.TypeHandler;
* @since 1.2.5
*/
@Getter
@Setter(AccessLevel.PACKAGE)
public class Result {
private boolean isId;
@ -31,9 +34,9 @@ public class Result {
private JdbcType jdbcType;
private TypeHandler<?> typeHandle;
private Class<? extends TypeHandler<?>> typeHandle;
private Result() {
public Result() {
}
public static class Builder<E, T> {