From ece59bb9e54057ab05f3171c7b361a3430a2d4a4 Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Fri, 4 Nov 2022 18:10:40 +0800 Subject: [PATCH] =?UTF-8?q?1.2.5=E7=89=88=E6=9C=AC=E4=B8=B4=E6=97=B6?= =?UTF-8?q?=E5=88=86=E6=94=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../yulichang/wrapper/MPJLambdaWrapper.java | 6 +-- .../wrapper/resultmap/Collection.java | 53 ++++++++++++++++++- .../yulichang/wrapper/resultmap/Result.java | 7 ++- 3 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java index 05b3fac..8cb7d6e 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -160,10 +160,10 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper(dtoFieldName, child, field.getType()); } else { Class ofType = (Class) 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 extends MPJAbstractLambdaWrapper genericType = ReflectionKit.getGenericType(field); Class ofType = (Class) genericType; - Collection.Builder builder = new Collection.Builder<>(dtoFieldName, child, field.getType(), ofType); + Collection.Builder builder = new Collection.Builder<>(dtoFieldName, child, field.getType(), ofType, false); this.resultMapCollection.add(collection.apply(builder).build()); return typedThis; } diff --git a/src/main/java/com/github/yulichang/wrapper/resultmap/Collection.java b/src/main/java/com/github/yulichang/wrapper/resultmap/Collection.java index 3f22600..7017ce2 100644 --- a/src/main/java/com/github/yulichang/wrapper/resultmap/Collection.java +++ b/src/main/java/com/github/yulichang/wrapper/resultmap/Collection.java @@ -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 { collection.javaType = javaType; collection.ofType = (Class) entityClass; collection.resultList = new ArrayList<>(); - // TODO 构建entityClass所有的字段 + autoBuild(true, entityClass, (Class) entityClass); } /** @@ -55,14 +63,16 @@ public class Collection { * @param entityClass 数据库实体类 * @param javaType javaType * @param ofType 映射类 + * @param auto 自动映射数据库实体对应的字段 */ - public Builder(String property, Class entityClass, Class javaType, Class ofType) { + public Builder(String property, Class entityClass, Class javaType, Class 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 id(MFunc> result) { @@ -80,5 +90,44 @@ public class Collection { public Collection build() { return collection; } + + private void autoBuild(boolean auto, Class entityClass, Class tagClass) { + TableInfo tableInfo = TableInfoHelper.getTableInfo(entityClass); + Map tagMap = ReflectionKit.getFieldMap(tagClass); + if (auto && !tagMap.isEmpty()) { + Function 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; + } } } diff --git a/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java b/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java index d67cd9d..da18258 100644 --- a/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java +++ b/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java @@ -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> typeHandle; - private Result() { + public Result() { } public static class Builder {