mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
MPJLambdaWrapper支持select子查询
This commit is contained in:
parent
55d4a9eb59
commit
626a98601d
@ -29,6 +29,11 @@ public class MPJTableInfo {
|
||||
*/
|
||||
private String alias;
|
||||
|
||||
/**
|
||||
* 前缀
|
||||
*/
|
||||
private String aliasDOT;
|
||||
|
||||
/**
|
||||
* 是否包含映射注解
|
||||
*/
|
||||
|
@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.config.GlobalConfig;
|
||||
import com.baomidou.mybatisplus.core.toolkit.*;
|
||||
import com.github.yulichang.annotation.MPJMapping;
|
||||
import com.github.yulichang.annotation.MPJTableAlias;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
import org.apache.ibatis.builder.MapperBuilderAssistant;
|
||||
import org.apache.ibatis.logging.Log;
|
||||
import org.apache.ibatis.logging.LogFactory;
|
||||
@ -167,7 +168,8 @@ public class MPJTableInfoHelper {
|
||||
tableInfo.setAlias(tableAlias.value());
|
||||
return;
|
||||
}
|
||||
tableInfo.setAlias("t" + index.getAndIncrement());
|
||||
tableInfo.setAlias(Constant.TABLE_ALIAS + index.getAndIncrement());
|
||||
tableInfo.setAliasDOT(tableInfo.getAlias() + StringPool.DOT);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -5,6 +5,9 @@ import java.lang.annotation.*;
|
||||
/**
|
||||
* 关联查询时的表别名
|
||||
* 框架默认会随机生成 一般是 t1 t2 t3 ...
|
||||
* 不要在程序中使用随机别名,运行期间是不变的,但是重启就不一定了
|
||||
* 如需使用请配合此注解一起使用
|
||||
* <p>
|
||||
* 这个注解定义的表别名或者随机生成的别名只对MPJLambdaWrapper生效
|
||||
* 对MPJQueryWrapper不生效,
|
||||
*
|
||||
|
@ -9,7 +9,6 @@ import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
@ -27,7 +26,13 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
||||
|
||||
@Override
|
||||
protected <X> String columnToString(X column) {
|
||||
return columnToString((SFunction<?, ?>) column);
|
||||
return columnToString((SFunction<?, ?>) column, hasAlias || entityClass !=
|
||||
LambdaUtils.getEntityClass((SFunction<?, ?>) column));
|
||||
}
|
||||
|
||||
@Override
|
||||
protected <X> String columnToString(X column, boolean hasAlias) {
|
||||
return columnToString((SFunction<?, ?>) column, hasAlias);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -36,9 +41,9 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
||||
return Arrays.stream(columns).map(i -> columnToString((SFunction<?, ?>) i)).collect(joining(StringPool.COMMA));
|
||||
}
|
||||
|
||||
protected String columnToString(SFunction<?, ?> column) {
|
||||
return MPJTableInfoHelper.getTableInfo(LambdaUtils.getEntityClass(column)).getAlias() + StringPool.DOT +
|
||||
getCache(column).getColumn();
|
||||
protected String columnToString(SFunction<?, ?> column, boolean hasAlias) {
|
||||
return (hasAlias ? MPJTableInfoHelper.getTableInfo(LambdaUtils.getEntityClass(column)).getAliasDOT() :
|
||||
StringPool.EMPTY) + getCache(column).getColumn();
|
||||
}
|
||||
|
||||
protected ColumnCache getCache(SFunction<?, ?> fn) {
|
||||
@ -50,12 +55,4 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
||||
}
|
||||
return cacheMap.get(LambdaUtils.formatKey(LambdaUtils.getName(fn)));
|
||||
}
|
||||
|
||||
protected String getDefault(Integer i) {
|
||||
if (Objects.nonNull(i)) {
|
||||
return i.toString();
|
||||
}
|
||||
return StringPool.EMPTY;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -11,6 +11,7 @@ import com.baomidou.mybatisplus.core.toolkit.*;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.StringEscape;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import com.github.yulichang.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.wrapper.interfaces.Compare;
|
||||
import com.github.yulichang.wrapper.interfaces.Func;
|
||||
@ -35,10 +36,17 @@ import static java.util.stream.Collectors.joining;
|
||||
*
|
||||
* @author yulichang
|
||||
*/
|
||||
@SuppressWarnings("ALL")
|
||||
//@SuppressWarnings("ALL")
|
||||
@SuppressWarnings({"unchecked", "unused"})
|
||||
public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<T, Children>> extends Wrapper<T>
|
||||
implements Compare<Children>, Nested<Children, Children>, Join<Children>, Func<Children>, OnCompare<Children> {
|
||||
|
||||
|
||||
/**
|
||||
* 是否使用别名
|
||||
*/
|
||||
protected boolean hasAlias;
|
||||
|
||||
/**
|
||||
* 占位符
|
||||
*/
|
||||
@ -51,7 +59,7 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
/**
|
||||
* 其他
|
||||
*/
|
||||
/* mybatis plus 3.4.3新增 这个时wrapper的别名 不是MPJ的别名 */
|
||||
/* mybatis plus 3.4.3新增 这个是wrapper的别名 不是MPJ的别名 */
|
||||
protected SharedString paramAlias;
|
||||
protected SharedString lastSql;
|
||||
/**
|
||||
@ -71,32 +79,13 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
/**
|
||||
* 实体类型(主要用于确定泛型以及取TableInfo缓存)
|
||||
*/
|
||||
private Class<T> entityClass;
|
||||
protected Class<?> entityClass;
|
||||
|
||||
@Override
|
||||
public T getEntity() {
|
||||
return entity;
|
||||
}
|
||||
|
||||
public Children setEntity(T entity) {
|
||||
this.entity = entity;
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
public Class<T> getEntityClass() {
|
||||
if (entityClass == null && entity != null) {
|
||||
entityClass = (Class<T>) entity.getClass();
|
||||
}
|
||||
return entityClass;
|
||||
}
|
||||
|
||||
public Children setEntityClass(Class<T> entityClass) {
|
||||
if (entityClass != null) {
|
||||
this.entityClass = entityClass;
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <X, V> Children allEq(boolean condition, Map<SFunction<X, ?>, V> params, boolean null2IsNull) {
|
||||
if (condition && CollectionUtils.isNotEmpty(params)) {
|
||||
@ -329,7 +318,6 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
* 字段 SQL 注入过滤处理,子类重写实现过滤逻辑
|
||||
*
|
||||
* @param column 字段内容
|
||||
* @return
|
||||
*/
|
||||
protected <X> SFunction<X, ?> columnSqlInjectFilter(SFunction<X, ?> column) {
|
||||
return column;
|
||||
@ -384,9 +372,11 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
() -> formatParam(null, val)));
|
||||
}
|
||||
|
||||
protected <X, S> Children addCondition(boolean condition, SFunction<X, ?> column, SqlKeyword sqlKeyword, SFunction<S, ?> val) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), sqlKeyword,
|
||||
columnToSqlSegment(val)));
|
||||
protected <X, S> Children addCondition(boolean condition, SFunction<X, ?> column, SqlKeyword sqlKeyword,
|
||||
SFunction<S, ?> val) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column, hasAlias || entityClass !=
|
||||
LambdaUtils.getEntityClass(column)), sqlKeyword, columnToSqlSegment(val, hasAlias ||
|
||||
entityClass != LambdaUtils.getEntityClass(val))));
|
||||
}
|
||||
|
||||
/**
|
||||
@ -417,6 +407,7 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
* @param params 参数
|
||||
* @return sql片段
|
||||
*/
|
||||
@SuppressWarnings("SameParameterValue")
|
||||
protected final String formatSqlMaybeWithParam(String sqlStr, String mapping, Object... params) {
|
||||
if (StringUtils.isBlank(sqlStr)) {
|
||||
// todo 何时会这样?
|
||||
@ -572,6 +563,10 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
return () -> columnToString(column);
|
||||
}
|
||||
|
||||
protected final <X> ISqlSegment columnToSqlSegment(SFunction<X, ?> column, boolean hasAlias) {
|
||||
return () -> columnToString(column, hasAlias);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 columnName
|
||||
*/
|
||||
@ -579,6 +574,10 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
return (String) column;
|
||||
}
|
||||
|
||||
protected <X> String columnToString(X column, boolean hasAlias) {
|
||||
return (String) column;
|
||||
}
|
||||
|
||||
/**
|
||||
* 多字段转换为逗号 "," 分割字符串
|
||||
*
|
||||
@ -634,36 +633,4 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
public <R, S> Children le(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val) {
|
||||
return addCondition(condition, column, LE, val);
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, S, U> Children between(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<U, ?> val2) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN,
|
||||
columnToSqlSegment(val1), AND, columnToSqlSegment(val2)));
|
||||
}
|
||||
|
||||
public <R, S> Children between(boolean condition, SFunction<R, ?> column, Object val1, SFunction<S, ?> val2) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN,
|
||||
() -> formatParam(null, val1), AND, columnToSqlSegment(val2)));
|
||||
}
|
||||
|
||||
public <R, S> Children between(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, Object val2) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), BETWEEN,
|
||||
columnToSqlSegment(val1), AND, () -> formatParam(null, val2)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R, S, U> Children notBetween(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<U, ?> val2) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN,
|
||||
columnToSqlSegment(val1), AND, columnToSqlSegment(val2)));
|
||||
}
|
||||
|
||||
public <R, U> Children notBetween(boolean condition, SFunction<R, ?> column, Object val1, SFunction<U, ?> val2) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN,
|
||||
() -> formatParam(null, val1), AND, columnToSqlSegment(val2)));
|
||||
}
|
||||
|
||||
public <R, S> Children notBetween(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, Object val2) {
|
||||
return maybeDo(condition, () -> appendSqlSegments(columnToSqlSegment(column), NOT_BETWEEN,
|
||||
columnToSqlSegment(val1), AND, () -> formatParam(null, val2)));
|
||||
}
|
||||
}
|
||||
|
@ -2,10 +2,7 @@ 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.MPJTableInfoHelper;
|
||||
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.metadata.*;
|
||||
import com.baomidou.mybatisplus.core.toolkit.*;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
@ -14,13 +11,10 @@ import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import com.github.yulichang.wrapper.interfaces.LambdaJoin;
|
||||
import com.github.yulichang.wrapper.interfaces.Query;
|
||||
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.concurrent.atomic.AtomicInteger;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
@ -34,14 +28,14 @@ import java.util.stream.Collectors;
|
||||
* @author yulichang
|
||||
* @see com.github.yulichang.toolkit.Wrappers
|
||||
*/
|
||||
@SuppressWarnings("all")
|
||||
@SuppressWarnings("unused")
|
||||
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 sqlSelect = new SharedString();
|
||||
|
||||
/**
|
||||
* 查询表
|
||||
@ -51,34 +45,23 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
/**
|
||||
* 查询的字段
|
||||
*/
|
||||
private final List<SelectColumn> selectColumns = new ArrayList<>();
|
||||
private final List<String> selectColumns = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 忽略查询的字段
|
||||
*/
|
||||
private final List<SelectColumn> ignoreColumns = new ArrayList<>();
|
||||
private final List<String> ignoreColumns = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* ON sql wrapper集合
|
||||
*/
|
||||
private final List<MPJLambdaWrapper<?>> onWrappers = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 连表关键字 on 条件 func 使用
|
||||
*/
|
||||
@Getter
|
||||
private String keyWord;
|
||||
|
||||
/**
|
||||
* 连表实体类 on 条件 func 使用
|
||||
*/
|
||||
@Getter
|
||||
private Class<?> joinClass;
|
||||
private final List<String> joinSql = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery()
|
||||
*/
|
||||
public MPJLambdaWrapper() {
|
||||
this.hasAlias = true;
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
@ -86,21 +69,27 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 Wrappers.lambdaQuery(...)
|
||||
*/
|
||||
MPJLambdaWrapper(T entity, Class<T> entityClass, SharedString sqlSelect, AtomicInteger paramNameSeq,
|
||||
MPJLambdaWrapper(Class<?> entityClass, AtomicInteger paramNameSeq,
|
||||
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments,
|
||||
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst,
|
||||
String keyWord, Class<?> joinClass) {
|
||||
super.setEntity(entity);
|
||||
super.setEntityClass(entityClass);
|
||||
SharedString lastSql, SharedString sqlComment, SharedString sqlFirst, boolean hasAlias) {
|
||||
this.entityClass =entityClass;
|
||||
this.paramNameSeq = paramNameSeq;
|
||||
this.paramNameValuePairs = paramNameValuePairs;
|
||||
this.expression = mergeSegments;
|
||||
this.sqlSelect = sqlSelect;
|
||||
this.lastSql = lastSql;
|
||||
this.sqlComment = sqlComment;
|
||||
this.sqlFirst = sqlFirst;
|
||||
this.keyWord = keyWord;
|
||||
this.joinClass = joinClass;
|
||||
this.hasAlias = hasAlias;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected MPJLambdaWrapper<T> instance() {
|
||||
return instance(true, null);
|
||||
}
|
||||
|
||||
protected MPJLambdaWrapper<T> instance(boolean hasAlias, Class<?> entityClass) {
|
||||
return new MPJLambdaWrapper<>(entityClass, paramNameSeq, paramNameValuePairs, new MergeSegments(),
|
||||
SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(), hasAlias);
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -108,7 +97,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public final <S> MPJLambdaWrapper<T> select(SFunction<S, ?>... columns) {
|
||||
if (ArrayUtils.isNotEmpty(columns)) {
|
||||
for (SFunction<S, ?> s : columns) {
|
||||
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(s), getCache(s).getColumn()));
|
||||
selectColumns.add(getThisAlias(s) + getCache(s).getColumn());
|
||||
}
|
||||
}
|
||||
return typedThis;
|
||||
@ -118,20 +107,23 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public <E> MPJLambdaWrapper<T> select(Class<E> entityClass, Predicate<TableFieldInfo> predicate) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||
Assert.notNull(info, "table can not be find");
|
||||
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(
|
||||
i -> selectColumns.add(SelectColumn.of(entityClass, i.getColumn())));
|
||||
MPJTableInfo tableInfo = MPJTableInfoHelper.getTableInfo(entityClass);
|
||||
info.getFieldList().stream().filter(predicate).collect(Collectors.toList()).forEach(i ->
|
||||
selectColumns.add((hasAlias ? tableInfo.getAliasDOT() : StringPool.EMPTY) + i.getColumn()));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <S> MPJLambdaWrapper<T> selectAs(SFunction<S, ?> column, String alias) {
|
||||
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias));
|
||||
selectColumns.add(getThisAlias(column) + getCache(column).getColumn() + Constants.AS + alias);
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
public <S> MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column, String alias) {
|
||||
public <S> MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, SFunction<S, ?> column,
|
||||
String alias) {
|
||||
if (condition) {
|
||||
selectColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(column), getCache(column).getColumn(), alias, funcEnum));
|
||||
selectColumns.add(String.format(funcEnum.getSql(), getThisAlias(column) + getCache(column).getColumn())
|
||||
+ Constants.AS + alias);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
@ -139,7 +131,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
@Override
|
||||
public MPJLambdaWrapper<T> selectFunc(boolean condition, BaseFuncEnum funcEnum, Object column, String alias) {
|
||||
if (condition) {
|
||||
selectColumns.add(SelectColumn.of(null, column.toString(), alias, funcEnum));
|
||||
selectColumns.add(String.format(funcEnum.getSql(), column.toString()) + Constants.AS + alias);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
@ -147,11 +139,12 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public final MPJLambdaWrapper<T> selectAll(Class<?> clazz) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(clazz);
|
||||
Assert.notNull(info, "table can not be find -> %s", clazz);
|
||||
String dot = hasAlias ? MPJTableInfoHelper.getTableInfo(clazz).getAliasDOT() : StringPool.EMPTY;
|
||||
if (info.havePK()) {
|
||||
selectColumns.add(SelectColumn.of(clazz, info.getKeyColumn()));
|
||||
selectColumns.add(dot + info.getKeyColumn());
|
||||
}
|
||||
info.getFieldList().stream().filter(TableFieldInfo::isSelect).forEach(c ->
|
||||
selectColumns.add(SelectColumn.of(clazz, c.getColumn())));
|
||||
selectColumns.add(dot + c.getColumn()));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@ -160,12 +153,21 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public final <S> MPJLambdaWrapper<T> selectIgnore(SFunction<S, ?>... columns) {
|
||||
if (ArrayUtils.isNotEmpty(columns)) {
|
||||
for (SFunction<S, ?> s : columns) {
|
||||
ignoreColumns.add(SelectColumn.of(LambdaUtils.getEntityClass(s), getCache(s).getColumn()));
|
||||
ignoreColumns.add(getThisAlias(s) + getCache(s).getColumn());
|
||||
}
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public MPJLambdaWrapper<T> selectQuery(Class<?> clazz, OnFunction function, String alias) {
|
||||
MPJLambdaWrapper<?> apply = function.apply(instance(false, clazz));
|
||||
selectColumns.add(String.format("(SELECT %s FROM %s %s)", apply.getSqlSelect(),
|
||||
TableInfoHelper.getTableInfo(clazz).getTableName(), apply.getCustomSqlSegment()) + Constants.AS + alias);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 查询条件 SQL 片段
|
||||
*/
|
||||
@ -173,15 +175,9 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
public String getSqlSelect() {
|
||||
if (StringUtils.isBlank(sqlSelect.getStringValue())) {
|
||||
if (CollectionUtils.isNotEmpty(ignoreColumns)) {
|
||||
selectColumns.removeIf(c -> c.getFuncEnum() == null && ignoreColumns.stream().anyMatch(i ->
|
||||
i.getClazz() == c.getClazz() && Objects.equals(c.getColumnName(), i.getColumnName())));
|
||||
selectColumns.removeIf(ignoreColumns::contains);
|
||||
}
|
||||
String s = selectColumns.stream().map(i -> {
|
||||
String str = MPJTableInfoHelper.getTableInfo(i.getClazz()).getAlias() + StringPool.DOT + i.getColumnName();
|
||||
return (i.getFuncEnum() == null ? str : String.format(i.getFuncEnum().getSql(), str)) +
|
||||
(StringUtils.isBlank(i.getAlias()) ? StringPool.EMPTY : (Constant.AS + i.getAlias()));
|
||||
}).collect(Collectors.joining(StringPool.COMMA));
|
||||
sqlSelect.setStringValue(s);
|
||||
sqlSelect.setStringValue(String.join(StringPool.COMMA, selectColumns));
|
||||
}
|
||||
return sqlSelect.getStringValue();
|
||||
}
|
||||
@ -191,17 +187,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
*/
|
||||
public String getFrom() {
|
||||
if (StringUtils.isBlank(from.getStringValue())) {
|
||||
StringBuilder value = new StringBuilder();
|
||||
for (MPJLambdaWrapper<?> wrapper : onWrappers) {
|
||||
String tableName = TableInfoHelper.getTableInfo(wrapper.getJoinClass()).getTableName();
|
||||
value.append(wrapper.getKeyWord())
|
||||
.append(tableName)
|
||||
.append(Constants.SPACE)
|
||||
.append(MPJTableInfoHelper.getTableInfo(wrapper.getJoinClass()).getAlias())
|
||||
.append(Constant.ON)
|
||||
.append(wrapper.getExpression().getNormal().getSqlSegment());
|
||||
}
|
||||
from.setStringValue(value.toString());
|
||||
from.setStringValue(String.join(StringPool.SPACE, joinSql));
|
||||
}
|
||||
return from.getStringValue();
|
||||
}
|
||||
@ -210,20 +196,6 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 用于生成嵌套 sql
|
||||
* <p>故 sqlSelect 不向下传递</p>
|
||||
*/
|
||||
@Override
|
||||
protected MPJLambdaWrapper<T> instance() {
|
||||
return instance(null, null);
|
||||
}
|
||||
|
||||
protected MPJLambdaWrapper<T> instance(String keyWord, Class<?> joinClass) {
|
||||
return new MPJLambdaWrapper<>(getEntity(), getEntityClass(), null, paramNameSeq, paramNameValuePairs,
|
||||
new MergeSegments(), SharedString.emptyString(), SharedString.emptyString(), SharedString.emptyString(),
|
||||
keyWord, joinClass);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
@ -232,65 +204,21 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
from.toNull();
|
||||
selectColumns.clear();
|
||||
ignoreColumns.clear();
|
||||
joinSql.clear();
|
||||
}
|
||||
|
||||
@Override
|
||||
public <R> MPJLambdaWrapper<T> join(String keyWord, boolean condition, Class<R> clazz, OnFunction function) {
|
||||
if (condition) {
|
||||
MPJLambdaWrapper<?> apply = function.apply(instance(keyWord, clazz));
|
||||
onWrappers.add(apply);
|
||||
joinSql.add(keyWord + TableInfoHelper.getTableInfo(clazz).getTableName() +
|
||||
Constants.SPACE + MPJTableInfoHelper.getTableInfo(clazz).getAlias() +
|
||||
Constant.ON + function.apply(instance()).getExpression().getNormal().getSqlSegment());
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
/**
|
||||
* select字段
|
||||
*/
|
||||
@Data
|
||||
public static class SelectColumn {
|
||||
|
||||
/**
|
||||
* 字段实体类
|
||||
*/
|
||||
private Class<?> clazz;
|
||||
|
||||
/**
|
||||
* 数据库字段名
|
||||
*/
|
||||
private String columnName;
|
||||
|
||||
/**
|
||||
* 字段别名
|
||||
*/
|
||||
private String alias;
|
||||
|
||||
/**
|
||||
* 字段函数
|
||||
*/
|
||||
private BaseFuncEnum funcEnum;
|
||||
|
||||
/**
|
||||
* 自定义函数填充参数
|
||||
*/
|
||||
private List<SFunction<?, ?>> funcArgs;
|
||||
|
||||
private SelectColumn(Class<?> clazz, String columnName, String alias, BaseFuncEnum funcEnum) {
|
||||
this.clazz = clazz;
|
||||
this.columnName = columnName;
|
||||
this.alias = alias;
|
||||
this.funcEnum = funcEnum;
|
||||
}
|
||||
|
||||
public static SelectColumn of(Class<?> clazz, String columnName) {
|
||||
return new SelectColumn(clazz, columnName, null, null);
|
||||
}
|
||||
|
||||
public static SelectColumn of(Class<?> clazz, String columnName, String alias) {
|
||||
return new SelectColumn(clazz, columnName, alias, null);
|
||||
}
|
||||
|
||||
public static SelectColumn of(Class<?> clazz, String columnName, String alias, BaseFuncEnum funcEnum) {
|
||||
return new SelectColumn(clazz, columnName, alias, funcEnum);
|
||||
}
|
||||
private String getThisAlias(SFunction<?, ?> function) {
|
||||
return hasAlias ? MPJTableInfoHelper.getTableInfo(LambdaUtils.getEntityClass(function)).getAliasDOT() :
|
||||
StringPool.EMPTY;
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import com.github.yulichang.wrapper.enums.DefaultFuncEnum;
|
||||
import com.github.yulichang.wrapper.interfaces.on.OnFunction;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.function.Predicate;
|
||||
@ -51,6 +52,22 @@ public interface Query<Children> extends Serializable {
|
||||
*/
|
||||
<S> Children selectAs(SFunction<S, ?> column, String alias);
|
||||
|
||||
/**
|
||||
* 子查询
|
||||
*
|
||||
* @param clazz 查询的类
|
||||
* @param function 查询lambda
|
||||
* @param alias 别名
|
||||
*/
|
||||
Children selectQuery(Class<?> clazz, OnFunction function, String alias);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <S> Children selectQuery(Class<?> clazz, OnFunction function, SFunction<S, ?> alias) {
|
||||
return selectQuery(clazz, function, LambdaUtils.getName(alias));
|
||||
}
|
||||
|
||||
/**
|
||||
* 聚合函数查询
|
||||
*
|
||||
|
@ -12,6 +12,7 @@ import java.io.Serializable;
|
||||
*
|
||||
* @since 1.1.8
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public interface OnCompare<Children> extends Serializable {
|
||||
/**
|
||||
* ignore
|
||||
@ -114,40 +115,4 @@ public interface OnCompare<Children> extends Serializable {
|
||||
* @return children
|
||||
*/
|
||||
<R, S> Children le(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R, S, T> Children between(SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<T, ?> val2) {
|
||||
return between(true, column, val1, val2);
|
||||
}
|
||||
|
||||
/**
|
||||
* BETWEEN 值1 AND 值2
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val1 值1
|
||||
* @param val2 值2
|
||||
* @return children
|
||||
*/
|
||||
<R, S, T> Children between(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<T, ?> val2);
|
||||
|
||||
/**
|
||||
* ignore
|
||||
*/
|
||||
default <R, S, T> Children notBetween(SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<T, ?> val2) {
|
||||
return notBetween(true, column, val1, val2);
|
||||
}
|
||||
|
||||
/**
|
||||
* NOT BETWEEN 值1 AND 值2
|
||||
*
|
||||
* @param condition 执行条件
|
||||
* @param column 字段
|
||||
* @param val1 值1
|
||||
* @param val2 值2
|
||||
* @return children
|
||||
*/
|
||||
<R, S, T> Children notBetween(boolean condition, SFunction<R, ?> column, SFunction<S, ?> val1, SFunction<T, ?> val2);
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user