diff --git a/README.md b/README.md index 9579df0112d3a212daf6a5a9739bf4a60a8f4dc3..fdc78bb85e4e22c190cfd667d8a7e8e19c14c599 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) diff --git a/pom.xml b/pom.xml index 40336d7ee66ccca4f1c36c997e99efd6165d6452..0c388f4b689cde5804c856e8242bba660047d441 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/baomidou/mybatisplus/core/metadata/MPJResultHelper.java b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java new file mode 100644 index 0000000000000000000000000000000000000000..fb1aaa7d6d1bd5689cf5bbf9b1f94524a8523233 --- /dev/null +++ b/src/main/java/com/baomidou/mybatisplus/core/metadata/MPJResultHelper.java @@ -0,0 +1,70 @@ +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 dc432c6302387daf6fc5a17b3c8653ecd634684d..5fe2382d2e5c276228e9aa588e34ba8ed4f30406 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,14 +18,13 @@ 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; +import static com.baomidou.mybatisplus.core.enums.SqlKeyword.GROUP_BY; + /** * 参考 {@link com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper} * Lambda 语法使用 Wrapper @@ -39,46 +38,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<>(); - + /** + * 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 sql wrapper集合 - */ - private final List> onWrappers = new ArrayList<>(); - /** * 连表关键字 on 条件 func 使用 */ @@ -150,6 +141,16 @@ 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))); + } + return typedThis; + } + @Override public MPJLambdaWrapper selectAs(SFunction column, String alias) { selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias)); @@ -278,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 ac70fdf7482eb9d840866d4bca5945ec2b56f28a..b40f39e4f012b7ab035715b2176225993a403b61 100644 --- a/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java +++ b/src/main/java/com/github/yulichang/wrapper/enums/DefaultFuncEnum.java @@ -12,9 +12,11 @@ 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 abbecbaab6038741fe82973f62d0583c30a9c6f0..3695498aaa6518cd84e725b50a32f6fbf444322d 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 */ 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 6557c108d6cbd96bb9f0d87f4775a5c8317857cb..d9bfa1a5d7feedcb376853a664eb6f1454b54503 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() */