From 5644383a4c4b3b6338c2684d131d8a9f121e2fdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=86=B0=E5=86=B0=E5=89=8D=E7=94=B7=E5=8F=8B?= <761206624@qq.com> Date: Fri, 5 Aug 2022 13:29:59 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0count=E5=8E=BB=E9=87=8D?= =?UTF-8?q?=E3=80=81=E5=A2=9E=E5=8A=A0select=E8=BF=94=E5=9B=9E=E5=88=97?= =?UTF-8?q?=E6=8C=89VO=E6=9F=A5=E8=AF=A2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../core/metadata/MPJResultHelper.java | 65 +++++++++++++++++++ .../yulichang/wrapper/MPJLambdaWrapper.java | 48 +++++++------- .../wrapper/enums/DefaultFuncEnum.java | 1 + .../yulichang/wrapper/interfaces/Query.java | 43 ++++++++++++ 5 files changed, 134 insertions(+), 27 deletions(-) create mode 100644 src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java diff --git a/pom.xml b/pom.xml index 40336d7..0d58016 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.5 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/baomidou/mybatisplus/core/metadata/MPJResultHelper.java b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java new file mode 100644 index 0000000..d02be92 --- /dev/null +++ b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java @@ -0,0 +1,65 @@ +package com.baomidou.mybatisplus.core.metadata; + +import com.baomidou.mybatisplus.core.toolkit.Assert; +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, Set> 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 Set getVoTableInfo(Class sourceEntityClass, Class resultEntityClass) { + if (resultEntityClass == null || ReflectionKit.isPrimitiveOrWrapper(resultEntityClass) || resultEntityClass == String.class || resultEntityClass.isInterface()) { + return null; + } + Set strings = VO_INFO_CACHE.get(resultEntityClass); + if (strings == null) { + Set set = new HashSet<>(); + MPJTableInfo info = MPJTableInfoHelper.getTableInfo(sourceEntityClass); + Assert.notNull(info, "table can not be find"); + List allFields = TableInfoHelper.getAllFields(resultEntityClass); + Assert.notNull(allFields, "table can not be find"); + Set fieldNames = allFields.stream().collect(Collectors.groupingBy(Field::getName)).keySet(); + info.getTableInfo().getFieldList().forEach( + i -> { + if (fieldNames.contains(i.getProperty())) { + set.add(i.getColumn()); + } + }); + /* 添加缓存 */ + VO_INFO_CACHE.put(resultEntityClass, set); + } + 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 dc432c6..539415d 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -2,12 +2,12 @@ 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; import com.baomidou.mybatisplus.core.toolkit.*; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; -import com.github.yulichang.query.MPJQueryWrapper; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.LambdaUtils; import com.github.yulichang.toolkit.MPJWrappers; @@ -18,10 +18,7 @@ import com.github.yulichang.wrapper.interfaces.on.OnFunction; import lombok.Data; import lombok.Getter; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Objects; +import java.util.*; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Predicate; import java.util.stream.Collectors; @@ -39,46 +36,38 @@ import java.util.stream.Collectors; public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper> implements Query>, LambdaJoin> { - /** - * 查询字段 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 使用 */ @@ -150,6 +139,15 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectAsClass(Class sourceEntityClass, Class resultEntityClass) { + TableInfo info = TableInfoHelper.getTableInfo(sourceEntityClass); + Assert.notNull(info, "table can not be find"); + Set voTableInfo = MPJResultHelper.getVoTableInfo(sourceEntityClass, resultEntityClass); + Assert.notNull(info, "table can not be find"); + voTableInfo.forEach(i -> selectColumns.add(SelectColumn.of(sourceEntityClass, i))); + 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/enums/DefaultFuncEnum.java b/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java index ac70fdf..74ff529 100644 --- a/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java +++ b/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java @@ -15,6 +15,7 @@ public enum DefaultFuncEnum implements BaseFuncEnum { 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/Query.java b/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java index 6557c10..d9bfa1a 100644 --- a/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java +++ b/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java @@ -190,6 +190,49 @@ 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() */ From 914349c640be92a6045b1564a056afc56da1e674 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=86=B0=E5=86=B0=E5=89=8D=E7=94=B7=E5=8F=8B?= <761206624@qq.com> Date: Fri, 5 Aug 2022 13:41:06 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0count=E5=8E=BB=E9=87=8D?= =?UTF-8?q?=E3=80=81=E5=A2=9E=E5=8A=A0select=E8=BF=94=E5=9B=9E=E5=88=97?= =?UTF-8?q?=E6=8C=89VO=E6=9F=A5=E8=AF=A2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 36 +++++++++++++++++++++++++++++++++++- 1 file changed, 35 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 9579df0..fdc78bb 100644 --- a/README.md +++ b/README.md @@ -97,7 +97,8 @@ WHERE ( * 默认主表别名是t,其他的表别名以先后调用的顺序使用t1,t2,t3.... * 条件查询,可以查询主表以及参与连接的所有表的字段,全部调用mp原生的方法,正常使用没有sql注入风险 -MPJLambdaWrapper其他功能 +MPJLambdaWrapper其他功能 + * [简单的SQL函数使用](https://gitee.com/best_handsome/mybatis-plus-join/wikis/selectFunc()?sort_id=4082479) * [ON语句多条件支持](https://gitee.com/best_handsome/mybatis-plus-join/wikis/leftJoin?sort_id=3496671) @@ -278,6 +279,39 @@ 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) From 2cb66ca22a35e7676262d3deb2cd53b8740c4914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=86=B0=E5=86=B0=E5=89=8D=E7=94=B7=E5=8F=8B?= <761206624@qq.com> Date: Wed, 10 Aug 2022 13:29:49 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=9E=E8=A1=A8selectA?= =?UTF-8?q?sClass=E8=BF=94=E5=9B=9E=E5=88=97=E6=8C=89VO=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../core/metadata/MPJResultHelper.java | 33 +++++++++++-------- .../yulichang/wrapper/MPJLambdaWrapper.java | 13 ++++---- 3 files changed, 27 insertions(+), 21 deletions(-) diff --git a/pom.xml b/pom.xml index 0d58016..74851ba 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.github.yulichang mybatis-plus-join - 1.2.5 + Gy.13.V.2.0.9 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/baomidou/mybatisplus/core/metadata/MPJResultHelper.java b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java index d02be92..fb1aaa7 100644 --- a/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java +++ b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java @@ -1,6 +1,7 @@ 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; @@ -28,7 +29,7 @@ public class MPJResultHelper { /** * 储存反射VO信息 */ - private static final Map, Set> VO_INFO_CACHE = new ConcurrentHashMap<>(); + private static final Map, Map>> VO_INFO_CACHE = new ConcurrentHashMap<>(); /** @@ -39,26 +40,30 @@ public class MPJResultHelper { * @return: java.util.Set * @Date: 2022/8/5 09:59 */ - public static Set getVoTableInfo(Class sourceEntityClass, Class resultEntityClass) { + public static Map> getVoTableInfo(Class resultEntityClass, Class... sourceEntityClass) { if (resultEntityClass == null || ReflectionKit.isPrimitiveOrWrapper(resultEntityClass) || resultEntityClass == String.class || resultEntityClass.isInterface()) { return null; } - Set strings = VO_INFO_CACHE.get(resultEntityClass); - if (strings == null) { - Set set = new HashSet<>(); - MPJTableInfo info = MPJTableInfoHelper.getTableInfo(sourceEntityClass); - Assert.notNull(info, "table can not be find"); + 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(); - info.getTableInfo().getFieldList().forEach( - i -> { - if (fieldNames.contains(i.getProperty())) { - set.add(i.getColumn()); - } - }); + 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, 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 539415d..95bcbea 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -139,12 +139,13 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper MPJLambdaWrapper selectAsClass(Class sourceEntityClass, Class resultEntityClass) { - TableInfo info = TableInfoHelper.getTableInfo(sourceEntityClass); - Assert.notNull(info, "table can not be find"); - Set voTableInfo = MPJResultHelper.getVoTableInfo(sourceEntityClass, resultEntityClass); - Assert.notNull(info, "table can not be find"); - voTableInfo.forEach(i -> selectColumns.add(SelectColumn.of(sourceEntityClass, i))); + public 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))); + } return typedThis; } From bbb64c51444177641f814199ab90c97ab9aac4f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=86=B0=E5=86=B0=E5=89=8D=E7=94=B7=E5=8F=8B?= <761206624@qq.com> Date: Wed, 10 Aug 2022 13:32:33 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=9E=E8=A1=A8selectA?= =?UTF-8?q?sClass=E8=BF=94=E5=9B=9E=E5=88=97=E6=8C=89VO=E6=9F=A5=E8=AF=A2?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 74851ba..dfaf71b 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.github.yulichang mybatis-plus-join - Gy.13.V.2.0.9 + 1.2.6 mybatis-plus-join An enhanced toolkit of Mybatis-Plus to simplify development. https://github.com/yulichang/mybatis-plus-join From 2674d6e7b9340948656422d9e4aaf64954949723 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E5=86=B0=E5=86=B0=E5=89=8D=E7=94=B7=E5=8F=8B?= <761206624@qq.com> Date: Thu, 11 Aug 2022 16:15:59 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E8=BF=9E=E8=A1=A8?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=88=86=E7=BB=84=E6=8C=87=E5=AE=9A=E5=88=AB?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E6=AE=B5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 2 +- .../github/yulichang/wrapper/MPJLambdaWrapper.java | 11 +++++++++++ .../yulichang/wrapper/enums/DefaultFuncEnum.java | 3 ++- .../com/github/yulichang/wrapper/interfaces/Func.java | 2 ++ 4 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index dfaf71b..0c388f4 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ 4.0.0 com.github.yulichang mybatis-plus-join - 1.2.6 + 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 95bcbea..5fe2382 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -23,6 +23,8 @@ 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 @@ -277,6 +279,15 @@ 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 74ff529..b40f39e 100644 --- a/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java +++ b/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java @@ -12,7 +12,8 @@ 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)"), 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 abbecba..3695498 100644 --- a/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java +++ b/src/main/java/com/github/yulichang/wrapper/interfaces/Func.java @@ -187,6 +187,8 @@ public interface Func extends Serializable { */ Children groupBy(boolean condition, List> columns); + Children groupBy(String... columns); + /** * ignore */ From 7b282fd8daede8d32004286beeedb38852c31c55 Mon Sep 17 00:00:00 2001 From: yulichang <570810310@qq.com> Date: Fri, 28 Oct 2022 17:28:53 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E5=90=88=E5=B9=B6&=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 33 --------- .../core/metadata/MPJResultHelper.java | 70 ------------------- .../yulichang/wrapper/MPJLambdaWrapper.java | 37 +++++----- .../wrapper/enums/DefaultFuncEnum.java | 4 +- .../yulichang/wrapper/interfaces/Func.java | 2 - .../yulichang/wrapper/interfaces/Query.java | 60 +++++----------- 6 files changed, 36 insertions(+), 170 deletions(-) delete mode 100644 src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java 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() */