diff --git a/pom.xml b/pom.xml index 642c7d9..a09ff56 100644 --- a/pom.xml +++ b/pom.xml @@ -1,10 +1,10 @@ - 4.0.0 com.github.yulichang mybatis-plus-join - 1.2.4 + 1.2.7 mybatis-plus-join An enhanced toolkit of Mybatis-Plus to simplify development. https://github.com/yulichang/mybatis-plus-join diff --git a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java index fcdfbde..d380d18 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -10,6 +10,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.MPJWrappers; +import com.github.yulichang.toolkit.ReflectionKit; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import com.github.yulichang.wrapper.interfaces.LambdaJoin; import com.github.yulichang.wrapper.interfaces.Query; @@ -17,6 +18,7 @@ import com.github.yulichang.wrapper.interfaces.on.OnFunction; import lombok.Data; import lombok.Getter; +import java.lang.reflect.Field; import java.util.ArrayList; import java.util.List; import java.util.Map; @@ -38,46 +40,38 @@ import java.util.stream.Collectors; public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper> implements Query>, LambdaJoin, T> { - /** - * 查询字段 sql - */ - private SharedString sqlSelect = new SharedString(); - /** * 查询表 */ private final SharedString from = new SharedString(); - /** * 主表别名 */ private final SharedString alias = new SharedString(Constant.TABLE_ALIAS); - /** * 查询的字段 */ private final List selectColumns = new ArrayList<>(); - /** * 忽略查询的字段 */ private final List ignoreColumns = new ArrayList<>(); - - /** - * 是否 select distinct - */ - private boolean selectDistinct = false; - - /** - * 表序号 - */ - private int tableIndex = 1; - /** * ON sql wrapper集合 */ private final List> onWrappers = new ArrayList<>(); - + /** + * 查询字段 sql + */ + private SharedString sqlSelect = new SharedString(); + /** + * 是否 select distinct + */ + private boolean selectDistinct = false; + /** + * 表序号 + */ + private int tableIndex = 1; /** * 连表关键字 on 条件 func 使用 */ @@ -149,6 +143,22 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectAsClass(Class source, Class tag) { + TableInfo tableInfo = TableInfoHelper.getTableInfo(source); + Assert.notNull(tableInfo, "table can not be find"); + List tagFields = ReflectionKit.getFieldList(tag); + tableInfo.getFieldList().forEach(i -> { + if (tagFields.stream().anyMatch(f -> f.getName().equals(i.getProperty()))) { + selectColumns.add(SelectColumn.of(source, i.getColumn())); + } + }); + if (tableInfo.havePK() && tagFields.stream().anyMatch(i -> i.getName().equals(tableInfo.getKeyProperty()))) { + selectColumns.add(SelectColumn.of(source, tableInfo.getKeyProperty())); + } + return typedThis; + } + @Override public MPJLambdaWrapper selectAs(SFunction column, String alias) { selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias)); diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java b/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java index 6557c10..ba2d9f4 100644 --- a/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java +++ b/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java @@ -39,6 +39,18 @@ public interface Query extends Serializable { */ Children select(Class entityClass, Predicate predicate); + /** + * 说明: + * 比如我们需要查询用户表有10个字段,然而我们只需要3个就够了,用mybatis-plus提供的select

+ * 需要一个属性一个属性填入很不优雅,现在我们可以用selectAsClass(UserDO.class, UserVo.class)

+ * 即可按所需的UserVo返回,前提是UserVo.class中的属性必须是UserDO.class中存在的 + * + * @param source 数据源实体类 + * @param tag 目标类 + * @return children + */ + Children selectAsClass(Class source, Class tag); + /** * ignore */ @@ -53,6 +65,11 @@ public interface Query extends Serializable { /** * 聚合函数查询 + *

+ * wrapper.selectFunc(() -> "COUNT(%s)", "t.id", "total"); + *

+ * lambda + * wrapper.selectFunc(() -> "COUNT(%s)", UserDO::getId, UserDTO::getTotal); * * @param funcEnum 函数枚举 {@link com.github.yulichang.wrapper.enums.DefaultFuncEnum} * @param column 函数作用的字段