diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java index 2e19ade..6d87703 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java @@ -14,6 +14,7 @@ import com.github.yulichang.wrapper.segments.SelectCache; import lombok.Getter; import java.util.*; +import java.util.function.Predicate; import java.util.stream.Collectors; /** @@ -87,6 +88,25 @@ public class MybatisLabel implements Label { autoBuild(auto, entityClass, ofType); } + /** + * 映射实体全部字段 + */ + public Builder all() { + autoBuild(true, mybatisLabel.entityClass, mybatisLabel.ofType); + return this; + } + + /** + * 映射实体字段过滤(含主键) + */ + public Builder filter(Predicate predicate) { + Map fieldMap = MPJReflectionKit.getFieldMap(mybatisLabel.ofType); + ColumnCache.getListField(mybatisLabel.entityClass).stream().filter(predicate) + .filter(p -> fieldMap.containsKey(p.getColumProperty())).forEach(c -> + mybatisLabel.resultList.add(new Result.Builder(c.isPk(), mybatisLabel.index, c).build())); + return this; + } + public Builder id(SFunction entity, SFunction tag) { Result.Builder builder = new Result.Builder<>(true, mybatisLabel.index); builder.column(entity).property(tag); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabelFree.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabelFree.java index e6a5d2e..40824cc 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabelFree.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabelFree.java @@ -13,6 +13,7 @@ import com.github.yulichang.wrapper.segments.SelectCache; import lombok.Getter; import java.util.*; +import java.util.function.Predicate; /** * 无泛型约束 实现自由映射 @@ -60,16 +61,38 @@ public class MybatisLabelFree implements Label { mybatisLabel.mybatisLabels = new ArrayList<>(); } - public Builder all(Class entityClass) { + public Builder all(Class entityClass) { allBuild(null, entityClass); return this; } - public Builder all(String prefix, Class entityClass) { + public Builder all(String prefix, Class entityClass) { allBuild(prefix, entityClass); return this; } + /** + * 映射实体字段过滤(含主键) + */ + public Builder filter(Class entityClass, Predicate predicate) { + Map fieldMap = MPJReflectionKit.getFieldMap(mybatisLabel.ofType); + ColumnCache.getListField(entityClass).stream().filter(predicate) + .filter(p -> fieldMap.containsKey(p.getColumProperty())).forEach(c -> + mybatisLabel.resultList.add(new Result.Builder(false, null, c).build())); + return this; + } + + /** + * 映射实体字段过滤(含主键) + */ + public Builder filter(String prefix, Class entityClass, Predicate predicate) { + Map fieldMap = MPJReflectionKit.getFieldMap(mybatisLabel.ofType); + ColumnCache.getListField(entityClass).stream().filter(predicate) + .filter(p -> fieldMap.containsKey(p.getColumProperty())).forEach(c -> + mybatisLabel.resultList.add(new Result.Builder(false, prefix, c).build())); + return this; + } + public Builder id(SFunction entity, SFunction tag) { Result.Builder builder = new Result.Builder<>(true, null); builder.column(entity).property(tag); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java index dd715b0..28d4399 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/Result.java @@ -1,5 +1,6 @@ package com.github.yulichang.wrapper.resultmap; +import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.LambdaUtils; @@ -12,6 +13,7 @@ import org.apache.ibatis.type.JdbcType; import java.util.Map; import java.util.Objects; +import java.util.Optional; /** * result 标签 @@ -50,6 +52,16 @@ public class Result { result.index = index; } + public Builder(boolean isId, String index, SelectCache selectCache) { + this.result = new Result(); + result.isId = isId; + result.index = index; + result.selectNormal = selectCache; + result.property = selectCache.getColumProperty(); + result.javaType = selectCache.getColumnType(); + result.jdbcType = Optional.ofNullable(selectCache.getTableFieldInfo()).map(TableFieldInfo::getJdbcType).orElse(null); + } + public Builder property(SFunction property) { result.property = LambdaUtils.getName(property); return this;