diff --git a/README.md b/README.md index fdc78bb..af29b90 100644 --- a/README.md +++ b/README.md @@ -279,39 +279,6 @@ ORDER BY addr.id DESC ``` -#### 指定返回列实体类,按需要返回列 - -```java - -class test { - @Resource - private UserMapper userMapper; - - void testJoin() { - IPage page = userMapper.selectJoinPage(new Page<>(1, 10), UserVo.class, - new MPJQueryWrapper() - .selectAsClass(UserDO.class, UserVo.class); - } -} -``` - -说明: -比如我们需要查询用户表有10个字段,然而我们只需要3个就够了,用mybatis-plus提供的select -需要一个属性一个属性填入很不优雅,现在我们可以用selectAsClass(UserDO.class, UserVo.class) -;即可按所需的UserVo返回,前提是UserVo.class中的属性必须是UserDO.class中存在的 - -对应sql - -``` -SELECT - t.id, - t.name, - t.sex -FROM - user t -LIMIT ?,? -``` - # [wiki](https://gitee.com/best_handsome/mybatis-plus-join/wikis) diff --git a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java deleted file mode 100644 index fb1aaa7..0000000 --- a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java +++ /dev/null @@ -1,70 +0,0 @@ -package com.baomidou.mybatisplus.core.metadata; - -import com.baomidou.mybatisplus.core.toolkit.Assert; -import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; -import com.baomidou.mybatisplus.core.toolkit.ReflectionKit; -import org.apache.ibatis.logging.Log; -import org.apache.ibatis.logging.LogFactory; - -import java.lang.reflect.Field; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.ConcurrentHashMap; -import java.util.stream.Collectors; - -/** - * @Author Gy.13 - * @Description: 用于构建查询返回列,由于mybatis-plus条件构造的select无法实现通过传入VO实体类查询想要的列,需要一个一个指定 - * @Date: 2022/8/5 09:39 - */ -@SuppressWarnings("deprecation") -public class MPJResultHelper { - - - private static final Log logger = LogFactory.getLog(MPJResultHelper.class); - - - /** - * 储存反射VO信息 - */ - private static final Map, Map>> VO_INFO_CACHE = new ConcurrentHashMap<>(); - - - /** - * @param sourceEntityClass - * @param resultEntityClass - * @Author Gy.13 - * @Description: 获取VO实体映射表信息 - * @return: java.util.Set - * @Date: 2022/8/5 09:59 - */ - public static Map> getVoTableInfo(Class resultEntityClass, Class... sourceEntityClass) { - if (resultEntityClass == null || ReflectionKit.isPrimitiveOrWrapper(resultEntityClass) || resultEntityClass == String.class || resultEntityClass.isInterface()) { - return null; - } - Map> maps = VO_INFO_CACHE.get(resultEntityClass); - if (maps == null) { - maps = CollectionUtils.newHashMap(); - List allFields = TableInfoHelper.getAllFields(resultEntityClass); - Assert.notNull(allFields, "table can not be find"); - Set fieldNames = allFields.stream().collect(Collectors.groupingBy(Field::getName)).keySet(); - for (Class entityClass : sourceEntityClass) { - Set set = new HashSet<>(); - MPJTableInfo info = MPJTableInfoHelper.getTableInfo(entityClass); - Assert.notNull(info, "table can not be find"); - info.getTableInfo().getFieldList().forEach( - i -> { - if (fieldNames.contains(i.getProperty())) { - set.add(i.getColumn()); - } - }); - maps.put(entityClass.getName(), set); - } - /* 添加缓存 */ - VO_INFO_CACHE.put(resultEntityClass, maps); - } - return VO_INFO_CACHE.get(resultEntityClass); - } -} diff --git a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java index 5fe2382..e2185d9 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -2,7 +2,6 @@ package com.github.yulichang.wrapper; import com.baomidou.mybatisplus.core.conditions.SharedString; import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments; -import com.baomidou.mybatisplus.core.metadata.MPJResultHelper; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.metadata.TableInfoHelper; @@ -11,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; @@ -18,13 +18,15 @@ import com.github.yulichang.wrapper.interfaces.on.OnFunction; import lombok.Data; import lombok.Getter; -import java.util.*; +import java.lang.reflect.Field; +import java.util.ArrayList; +import java.util.List; +import java.util.Map; +import java.util.Objects; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Predicate; import java.util.stream.Collectors; -import static com.baomidou.mybatisplus.core.enums.SqlKeyword.GROUP_BY; - /** * 参考 {@link com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper} * Lambda 语法使用 Wrapper @@ -141,12 +143,18 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectAsClass(Class resultEntityClass, Class... sourceEntityClass) { - Map> voTableInfo = MPJResultHelper.getVoTableInfo(resultEntityClass, sourceEntityClass); - Assert.notNull(voTableInfo, "table can not be find"); - for (Class entityClass : sourceEntityClass) { - Set columns = voTableInfo.get(entityClass.getName()); - columns.forEach(i -> selectColumns.add(SelectColumn.of(entityClass, i))); + @Override + public 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; } @@ -279,15 +287,6 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper groupBy(String... columns) { - return maybeDo(true, () -> { - final String finalOne = String.join(StringPool.COMMA, columns); - ; - appendSqlSegments(GROUP_BY, () -> finalOne); - }); - } - /** * select字段 */ diff --git a/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java b/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java index b40f39e..ac70fdf 100644 --- a/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java +++ b/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java @@ -12,11 +12,9 @@ package com.github.yulichang.wrapper.enums; * @author yulichang */ public enum DefaultFuncEnum implements BaseFuncEnum { - DATE_FORMAT_Y_M_D("DATE_FORMAT(%s,'%%Y-%%m-%%d')"), - DATE_FORMAT_Y_M("DATE_FORMAT(%s,'%%Y-%%m')"), + SUM("SUM(%s)"), COUNT("COUNT(%s)"), - COUNT_DISTINCT("COUNT(DISTINCT %s)"), MAX("MAX(%s)"), MIN("MIN(%s)"), AVG("AVG(%s)"), diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java b/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java index 3695498..abbecba 100644 --- a/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java +++ b/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java @@ -187,8 +187,6 @@ public interface Func extends Serializable { */ Children groupBy(boolean condition, List> columns); - Children groupBy(String... columns); - /** * ignore */ 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 d9bfa1a..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 函数作用的字段 @@ -190,49 +207,6 @@ public interface Query extends Serializable { return selectFunc(condition, DefaultFuncEnum.COUNT, column, alias); } - /** - * COUNT(DISTINCT) - */ - default Children selectCountDistinct(SFunction column) { - return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column); - } - - default Children selectCountDistinct(Object column, SFunction alias) { - return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column, alias); - } - - default Children selectCountDistinct(Object column, String alias) { - return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column, alias); - } - - default Children selectCountDistinct(SFunction column, SFunction alias) { - return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column, alias); - } - - default Children selectCountDistinct(SFunction column, String alias) { - return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column, alias); - } - - default Children selectCountDistinct(boolean condition, SFunction column) { - return selectFunc(condition, DefaultFuncEnum.COUNT_DISTINCT, column); - } - - default Children selectCountDistinct(boolean condition, Object column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.COUNT_DISTINCT, column, alias); - } - - default Children selectCountDistinct(boolean condition, Object column, String alias) { - return selectFunc(condition, DefaultFuncEnum.COUNT_DISTINCT, column, alias); - } - - default Children selectCountDistinct(boolean condition, SFunction column, SFunction alias) { - return selectFunc(condition, DefaultFuncEnum.COUNT_DISTINCT, column, alias); - } - - default Children selectCountDistinct(boolean condition, SFunction column, String alias) { - return selectFunc(condition, DefaultFuncEnum.COUNT_DISTINCT, column, alias); - } - /** * MAX() */