mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
增加count去重、增加select返回列按VO查询。
This commit is contained in:
parent
e1a8930a31
commit
5644383a4c
4
pom.xml
4
pom.xml
@ -1,10 +1,10 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.github.yulichang</groupId>
|
||||
<artifactId>mybatis-plus-join</artifactId>
|
||||
<version>1.2.4</version>
|
||||
<version>1.2.5</version>
|
||||
<name>mybatis-plus-join</name>
|
||||
<description>An enhanced toolkit of Mybatis-Plus to simplify development.</description>
|
||||
<url>https://github.com/yulichang/mybatis-plus-join</url>
|
||||
|
@ -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<Class<?>, Set<String>> VO_INFO_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
/**
|
||||
* @param sourceEntityClass
|
||||
* @param resultEntityClass
|
||||
* @Author Gy.13
|
||||
* @Description: 获取VO实体映射表信息
|
||||
* @return: java.util.Set<java.lang.String>
|
||||
* @Date: 2022/8/5 09:59
|
||||
*/
|
||||
public static Set<String> getVoTableInfo(Class<?> sourceEntityClass, Class<?> resultEntityClass) {
|
||||
if (resultEntityClass == null || ReflectionKit.isPrimitiveOrWrapper(resultEntityClass) || resultEntityClass == String.class || resultEntityClass.isInterface()) {
|
||||
return null;
|
||||
}
|
||||
Set<String> strings = VO_INFO_CACHE.get(resultEntityClass);
|
||||
if (strings == null) {
|
||||
Set<String> set = new HashSet<>();
|
||||
MPJTableInfo info = MPJTableInfoHelper.getTableInfo(sourceEntityClass);
|
||||
Assert.notNull(info, "table can not be find");
|
||||
List<Field> allFields = TableInfoHelper.getAllFields(resultEntityClass);
|
||||
Assert.notNull(allFields, "table can not be find");
|
||||
Set<String> 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);
|
||||
}
|
||||
}
|
@ -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<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWrapper<T>>
|
||||
implements Query<MPJLambdaWrapper<T>>, LambdaJoin<MPJLambdaWrapper<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<SelectColumn> selectColumns = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 忽略查询的字段
|
||||
*/
|
||||
private final List<SelectColumn> ignoreColumns = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 是否 select distinct
|
||||
*/
|
||||
private boolean selectDistinct = false;
|
||||
|
||||
/**
|
||||
* 表序号
|
||||
*/
|
||||
private int tableIndex = 1;
|
||||
|
||||
/**
|
||||
* ON sql wrapper集合
|
||||
*/
|
||||
private final List<MPJLambdaWrapper<?>> 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<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
public <E> MPJLambdaWrapper<T> selectAsClass(Class<E> sourceEntityClass, Class<?> resultEntityClass) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(sourceEntityClass);
|
||||
Assert.notNull(info, "table can not be find");
|
||||
Set<String> 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 <S> MPJLambdaWrapper<T> selectAs(SFunction<S, ?> column, String alias) {
|
||||
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias));
|
||||
|
@ -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)"),
|
||||
|
@ -190,6 +190,49 @@ public interface Query<Children> extends Serializable {
|
||||
return selectFunc(condition, DefaultFuncEnum.COUNT, column, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* COUNT(DISTINCT)
|
||||
*/
|
||||
default <S> Children selectCountDistinct(SFunction<S, ?> column) {
|
||||
return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column);
|
||||
}
|
||||
|
||||
default <X> Children selectCountDistinct(Object column, SFunction<X, ?> alias) {
|
||||
return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column, alias);
|
||||
}
|
||||
|
||||
default Children selectCountDistinct(Object column, String alias) {
|
||||
return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectCountDistinct(SFunction<S, ?> column, SFunction<X, ?> alias) {
|
||||
return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectCountDistinct(SFunction<S, ?> column, String alias) {
|
||||
return selectFunc(DefaultFuncEnum.COUNT_DISTINCT, column, alias);
|
||||
}
|
||||
|
||||
default <S> Children selectCountDistinct(boolean condition, SFunction<S, ?> column) {
|
||||
return selectFunc(condition, DefaultFuncEnum.COUNT_DISTINCT, column);
|
||||
}
|
||||
|
||||
default <X> Children selectCountDistinct(boolean condition, Object column, SFunction<X, ?> 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 <S, X> Children selectCountDistinct(boolean condition, SFunction<S, ?> column, SFunction<X, ?> alias) {
|
||||
return selectFunc(condition, DefaultFuncEnum.COUNT_DISTINCT, column, alias);
|
||||
}
|
||||
|
||||
default <S, X> Children selectCountDistinct(boolean condition, SFunction<S, ?> column, String alias) {
|
||||
return selectFunc(condition, DefaultFuncEnum.COUNT_DISTINCT, column, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
* MAX()
|
||||
*/
|
||||
|
Loading…
x
Reference in New Issue
Block a user