mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
自定义表别名
This commit is contained in:
parent
f558466fb2
commit
693a18e5b9
@ -58,6 +58,7 @@ public class MybatisPlusJoinAutoConfiguration {
|
||||
this.properties = properties;
|
||||
ConfigProperties.subTableLogic = properties.getSubTableLogic();
|
||||
ConfigProperties.msCache = properties.isMsCache();
|
||||
ConfigProperties.tableAlias = properties.getTableAlias();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -20,6 +20,11 @@ public class MybatisPlusJoinProperties {
|
||||
*/
|
||||
private Boolean banner = true;
|
||||
|
||||
/**
|
||||
* 表别名
|
||||
*/
|
||||
private String tableAlias = "t";
|
||||
|
||||
/**
|
||||
* 连表查询副表是否启用逻辑删除(前提是MP配置了逻辑删除)
|
||||
*/
|
||||
|
@ -24,6 +24,12 @@
|
||||
"defaultValue": true,
|
||||
"type": "java.lang.Boolean",
|
||||
"description": "MappedStatement缓存开关."
|
||||
},
|
||||
{
|
||||
"name": "mybatis-plus-join.table-alias",
|
||||
"defaultValue": "t",
|
||||
"type": "java.lang.String",
|
||||
"description": "表别名."
|
||||
}
|
||||
]
|
||||
}
|
@ -7,4 +7,6 @@ package com.github.yulichang.config;
|
||||
public class ConfigProperties {
|
||||
public static boolean subTableLogic = true;
|
||||
public static boolean msCache = true;
|
||||
|
||||
public static String tableAlias = "t";
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ import com.baomidou.mybatisplus.core.injector.AbstractMethod;
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@ -43,7 +43,7 @@ public abstract class MPJAbstractMethod extends AbstractMethod implements MPJBas
|
||||
String[] columns = selectColumns.split(StringPool.COMMA);
|
||||
List<String> selectColumnList = new ArrayList<>();
|
||||
for (String c : columns) {
|
||||
selectColumnList.add(Constant.TABLE_ALIAS + StringPool.DOT + c);
|
||||
selectColumnList.add(ConfigProperties.tableAlias + StringPool.DOT + c);
|
||||
}
|
||||
selectColumns = String.join(StringPool.COMMA, selectColumnList);
|
||||
}
|
||||
|
@ -7,7 +7,6 @@ import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@ -69,7 +68,7 @@ public interface MPJBaseMethod extends Constants {
|
||||
return filedSqlScript;
|
||||
}
|
||||
String newKeyProperty = newPrefix + tableInfo.getKeyProperty();
|
||||
String keySqlScript = Constant.TABLE_ALIAS + DOT + tableInfo.getKeyColumn() + EQUALS + SqlScriptUtils.safeParam(newKeyProperty);
|
||||
String keySqlScript = ConfigProperties.tableAlias + DOT + tableInfo.getKeyColumn() + EQUALS + SqlScriptUtils.safeParam(newKeyProperty);
|
||||
return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty), false)
|
||||
+ NEWLINE + filedSqlScript;
|
||||
}
|
||||
@ -77,7 +76,7 @@ public interface MPJBaseMethod extends Constants {
|
||||
default String getSqlWhere(TableFieldInfo tableFieldInfo, final String prefix) {
|
||||
final String newPrefix = prefix == null ? EMPTY : prefix;
|
||||
// 默认: AND column=#{prefix + el}
|
||||
String sqlScript = " AND " + String.format(tableFieldInfo.getCondition(), Constant.TABLE_ALIAS + DOT + tableFieldInfo.getColumn(), newPrefix + tableFieldInfo.getEl());
|
||||
String sqlScript = " AND " + String.format(tableFieldInfo.getCondition(), ConfigProperties.tableAlias + DOT + tableFieldInfo.getColumn(), newPrefix + tableFieldInfo.getEl());
|
||||
// 查询的时候只判非空
|
||||
return convertIf(tableFieldInfo, sqlScript, convertIfProperty(newPrefix, tableFieldInfo.getProperty()), tableFieldInfo.getWhereStrategy());
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ 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.interfaces.MPJJoin;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.query.interfaces.StringJoin;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
@ -31,7 +31,7 @@ import java.util.stream.Collectors;
|
||||
@Deprecated
|
||||
@SuppressWarnings("unused")
|
||||
public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambdaQueryWrapper<T>>
|
||||
implements Query<MPJLambdaQueryWrapper<T>, T, SFunction<T, ?>>, MPJJoin<MPJLambdaQueryWrapper<T>, T> {
|
||||
implements Query<MPJLambdaQueryWrapper<T>, T, SFunction<T, ?>>, StringJoin<MPJLambdaQueryWrapper<T>, T> {
|
||||
|
||||
/**
|
||||
* 查询字段
|
||||
@ -46,7 +46,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
/**
|
||||
* 主表别名
|
||||
*/
|
||||
private final SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||
private final String alias = ConfigProperties.tableAlias;
|
||||
|
||||
/**
|
||||
* 查询的列
|
||||
@ -126,7 +126,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
public final MPJLambdaQueryWrapper<T> selectIgnore(SFunction<T, ?>... columns) {
|
||||
if (ArrayUtils.isNotEmpty(columns)) {
|
||||
for (SFunction<T, ?> s : columns) {
|
||||
ignoreColumns.add(Constant.TABLE_ALIAS + StringPool.DOT + columnToString(s));
|
||||
ignoreColumns.add(alias + StringPool.DOT + columnToString(s));
|
||||
}
|
||||
}
|
||||
return typedThis;
|
||||
@ -134,7 +134,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
|
||||
@Override
|
||||
protected String columnToString(SFunction<T, ?> column, boolean onlyColumn) {
|
||||
return Constant.TABLE_ALIAS + StringPool.DOT + super.columnToString(column, onlyColumn);
|
||||
return alias + StringPool.DOT + super.columnToString(column, onlyColumn);
|
||||
}
|
||||
|
||||
public MPJLambdaQueryWrapper<T> select(String... columns) {
|
||||
@ -162,7 +162,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
TableInfo info = TableInfoHelper.getTableInfo(entityClass);
|
||||
Assert.notNull(info, "table not find by class <%s>", entityClass.getSimpleName());
|
||||
selectColumns.addAll(info.getFieldList().stream().filter(predicate).map(c ->
|
||||
Constant.TABLE_ALIAS + StringPool.DOT + c.getColumn()).collect(Collectors.toList()));
|
||||
alias + StringPool.DOT + c.getColumn()).collect(Collectors.toList()));
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
@ -173,7 +173,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
* @param clazz 主表class
|
||||
*/
|
||||
public final MPJLambdaQueryWrapper<T> selectAll(Class<T> clazz) {
|
||||
return selectAll(clazz, Constant.TABLE_ALIAS);
|
||||
return selectAll(clazz, alias);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -219,7 +219,7 @@ public class MPJLambdaQueryWrapper<T> extends AbstractLambdaWrapper<T, MPJLambda
|
||||
|
||||
|
||||
public String getAlias() {
|
||||
return alias.getStringValue();
|
||||
return alias;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -8,8 +8,8 @@ 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.github.yulichang.query.interfaces.MPJJoin;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.query.interfaces.StringJoin;
|
||||
import com.github.yulichang.toolkit.MPJWrappers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
@ -29,7 +29,7 @@ import java.util.stream.Collectors;
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapper<T>>
|
||||
implements Query<MPJQueryWrapper<T>, T, String>, MPJJoin<MPJQueryWrapper<T>, T> {
|
||||
implements Query<MPJQueryWrapper<T>, T, String>, StringJoin<MPJQueryWrapper<T>, T> {
|
||||
|
||||
/**
|
||||
* 查询字段
|
||||
@ -44,7 +44,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
||||
/**
|
||||
* 主表别名
|
||||
*/
|
||||
private String alias = Constant.TABLE_ALIAS;
|
||||
private String alias = ConfigProperties.tableAlias;
|
||||
|
||||
/**
|
||||
* 查询的列
|
||||
@ -270,7 +270,7 @@ public class MPJQueryWrapper<T> extends AbstractWrapper<T, String, MPJQueryWrapp
|
||||
@Override
|
||||
public MPJQueryWrapper<T> join(String keyWord, boolean condition, String joinSql) {
|
||||
if (condition) {
|
||||
from.setStringValue(from.getStringValue() + StringPool.EMPTY + keyWord + StringPool.EMPTY + joinSql);
|
||||
from.setStringValue(from.getStringValue() + StringPool.SPACE + keyWord + StringPool.SPACE + joinSql);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
|
@ -7,7 +7,7 @@ import com.github.yulichang.toolkit.Constant;
|
||||
* @author yulichang
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public interface MPJJoin<Children, T> extends MPJBaseJoin<T> {
|
||||
public interface StringJoin<Children, T> extends MPJBaseJoin<T> {
|
||||
|
||||
/**
|
||||
* left join
|
@ -6,10 +6,6 @@ import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
||||
* @author yulichang
|
||||
*/
|
||||
public interface Constant {
|
||||
/**
|
||||
* 表别名
|
||||
*/
|
||||
String TABLE_ALIAS = "t";
|
||||
|
||||
String AS = " AS ";
|
||||
|
||||
@ -28,9 +24,9 @@ public interface Constant {
|
||||
String CLAZZ = "resultTypeClass_Eg1sG";
|
||||
|
||||
/**
|
||||
* " LEFT JOIN "
|
||||
* "LEFT JOIN"
|
||||
*/
|
||||
String LEFT_JOIN = StringPool.SPACE + LEFT + StringPool.SPACE + JOIN + StringPool.SPACE;
|
||||
String LEFT_JOIN = LEFT + StringPool.SPACE + JOIN;
|
||||
|
||||
/**
|
||||
* "RIGHT JOIN"
|
||||
@ -46,9 +42,4 @@ public interface Constant {
|
||||
* "FULL JOIN"
|
||||
*/
|
||||
String FULL_JOIN = FULL + StringPool.SPACE + JOIN;
|
||||
|
||||
/**
|
||||
* " t"
|
||||
*/
|
||||
String SPACE_TABLE_ALIAS = StringPool.SPACE + Constant.TABLE_ALIAS;
|
||||
}
|
||||
|
@ -21,25 +21,26 @@ public class LogicInfoUtils implements Constants {
|
||||
private static final Map<Class<?>, Map<String, String>> LOGIC_CACHE = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
public static String getLogicInfo(String tableIndex, Class<?> clazz) {
|
||||
public static String getLogicInfo(String tableIndex, Class<?> clazz, boolean hasAlias, String alias) {
|
||||
Map<String, String> absent = LOGIC_CACHE.get(clazz);
|
||||
if (absent == null) {
|
||||
absent = new ConcurrentHashMap<>();
|
||||
LOGIC_CACHE.put(clazz, absent);
|
||||
}
|
||||
return absent.computeIfAbsent(tableIndex, key -> getLogicStr(key, clazz));
|
||||
return absent.computeIfAbsent(hasAlias ? alias : (alias + tableIndex), key -> getLogicStr(key, clazz));
|
||||
}
|
||||
|
||||
private static String getLogicStr(String tableIndex, Class<?> clazz) {
|
||||
private static String getLogicStr(String prefix, Class<?> clazz) {
|
||||
|
||||
String logicStr;
|
||||
TableInfo tableInfo = TableInfoHelper.getTableInfo(clazz);
|
||||
Assert.notNull(tableInfo, "table not find by class <%s>", clazz.getSimpleName());
|
||||
if (tableInfo.isWithLogicDelete() && Objects.nonNull(tableInfo.getLogicDeleteFieldInfo())) {
|
||||
final String value = tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue();
|
||||
if (NULL.equalsIgnoreCase(value)) {
|
||||
logicStr = " AND " + Constant.TABLE_ALIAS + tableIndex + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL";
|
||||
logicStr = " AND " + prefix + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL";
|
||||
} else {
|
||||
logicStr = " AND " + Constant.TABLE_ALIAS + tableIndex + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
|
||||
logicStr = " AND " + prefix + DOT + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
|
||||
}
|
||||
} else {
|
||||
logicStr = StringPool.EMPTY;
|
||||
|
@ -1,8 +1,10 @@
|
||||
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.toolkit.StringPool;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import com.github.yulichang.toolkit.support.ColumnCache;
|
||||
import com.github.yulichang.wrapper.segments.Select;
|
||||
@ -10,8 +12,10 @@ import com.github.yulichang.wrapper.segments.SelectCache;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.EqualsAndHashCode;
|
||||
import lombok.Getter;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import static java.util.stream.Collectors.joining;
|
||||
@ -25,10 +29,23 @@ import static java.util.stream.Collectors.joining;
|
||||
public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLambdaWrapper<T, Children>>
|
||||
extends MPJAbstractWrapper<T, Children> {
|
||||
|
||||
/**
|
||||
* 主表别名
|
||||
*/
|
||||
protected String alias = ConfigProperties.tableAlias;
|
||||
/**
|
||||
* 是否构建是否存在一对多
|
||||
*/
|
||||
@Getter
|
||||
protected boolean resultMap = false;
|
||||
/**
|
||||
* 表序号
|
||||
*/
|
||||
protected int tableIndex = 1;
|
||||
/**
|
||||
* 关联的表
|
||||
*/
|
||||
protected TableList tableList = new TableList();
|
||||
protected TableList tableList;
|
||||
|
||||
|
||||
@Override
|
||||
@ -44,7 +61,7 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
||||
|
||||
protected String columnToString(String index, int node, SFunction<?, ?> column, boolean isJoin, Class<?> parent) {
|
||||
Class<?> entityClass = LambdaUtils.getEntityClass(column);
|
||||
return Constant.TABLE_ALIAS + getDefault(index, node, entityClass, isJoin, parent) + StringPool.DOT +
|
||||
return getDefault(index, node, entityClass, isJoin, parent) + StringPool.DOT +
|
||||
getCache(column).getTagColumn();
|
||||
}
|
||||
|
||||
@ -54,34 +71,41 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
||||
return cacheMap.get(LambdaUtils.getName(fn));
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回前缀
|
||||
*/
|
||||
protected String getDefault(String index, int node, Class<?> clazz, boolean isJoin, Class<?> parent) {
|
||||
//外层where条件
|
||||
if (Objects.isNull(index)) {
|
||||
if (!isJoin && Objects.equals(clazz, getEntityClass())) {
|
||||
return StringPool.EMPTY;
|
||||
return this.alias;
|
||||
}
|
||||
//正序
|
||||
Table table = tableList.getPositive(clazz);
|
||||
return Objects.isNull(table.index) ? StringPool.EMPTY : table.index;
|
||||
return table.hasAlias ? table.alias : (table.alias + (Objects.isNull(table.index) ? StringPool.EMPTY : table.index));
|
||||
}
|
||||
Table table = tableList.get(clazz, index);
|
||||
if (table.hasAlias) {
|
||||
return table.alias;
|
||||
}
|
||||
if (Objects.nonNull(table.getIndex())) {
|
||||
if (isJoin && (Objects.equals(clazz, getEntityClass()) || Objects.equals(parent, clazz))) {
|
||||
if (node == -1) {
|
||||
return StringPool.EMPTY;
|
||||
return table.alias;
|
||||
} else if (node == 0) {
|
||||
//除自己以外的倒序第一个
|
||||
Table t = tableList.getOrElse(clazz, index);
|
||||
if (Objects.isNull(t.getIndex())) {
|
||||
return StringPool.EMPTY;
|
||||
return t.alias;
|
||||
}
|
||||
return t.getIndex();
|
||||
return t.alias + t.getIndex();
|
||||
} else {
|
||||
return String.valueOf(node);
|
||||
return table.alias + node;
|
||||
}
|
||||
}
|
||||
return table.getIndex();
|
||||
return table.alias + table.getIndex();
|
||||
}
|
||||
return StringPool.EMPTY;
|
||||
return table.alias;
|
||||
}
|
||||
|
||||
protected String getDefaultSelect(String index, Class<?> clazz, Select s) {
|
||||
@ -111,12 +135,16 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
||||
|
||||
public static class TableList {
|
||||
|
||||
private static final Table DEFAULT_TABLE = new Table(null, StringPool.EMPTY);
|
||||
private final Table DEFAULT_TABLE;
|
||||
|
||||
public TableList(Class<?> clazz, String index, String alias) {
|
||||
DEFAULT_TABLE = new Table(clazz, index, false, alias);
|
||||
}
|
||||
|
||||
private final List<Table> list = new ArrayList<>();
|
||||
|
||||
public void add(Class<?> clazz, String index) {
|
||||
this.list.add(new Table(clazz, index));
|
||||
public void add(Class<?> clazz, String index, boolean hasAlias, String alias) {
|
||||
this.list.add(new Table(clazz, index, hasAlias, alias));
|
||||
}
|
||||
|
||||
public Table get(Class<?> clazz) {
|
||||
@ -217,7 +245,24 @@ public abstract class MPJAbstractLambdaWrapper<T, Children extends MPJAbstractLa
|
||||
private final Class<?> clazz;
|
||||
|
||||
private final String index;
|
||||
|
||||
private boolean hasAlias;
|
||||
|
||||
private final String alias;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 必要的初始化
|
||||
*/
|
||||
protected void initNeed() {
|
||||
paramNameSeq = new AtomicInteger(0);
|
||||
paramNameValuePairs = new HashMap<>(16);
|
||||
expression = new MergeSegments();
|
||||
lastSql = SharedString.emptyString();
|
||||
sqlComment = SharedString.emptyString();
|
||||
sqlFirst = SharedString.emptyString();
|
||||
node = ROOT_NODE;
|
||||
tableList = new TableList(getEntityClass(), null, alias);
|
||||
}
|
||||
}
|
||||
|
@ -572,19 +572,6 @@ public abstract class MPJAbstractWrapper<T, Children extends MPJAbstractWrapper<
|
||||
.collect(joining(StringPool.COMMA, StringPool.LEFT_BRACKET, StringPool.RIGHT_BRACKET));
|
||||
}
|
||||
|
||||
/**
|
||||
* 必要的初始化
|
||||
*/
|
||||
protected void initNeed() {
|
||||
paramNameSeq = new AtomicInteger(0);
|
||||
paramNameValuePairs = new HashMap<>(16);
|
||||
expression = new MergeSegments();
|
||||
lastSql = SharedString.emptyString();
|
||||
sqlComment = SharedString.emptyString();
|
||||
sqlFirst = SharedString.emptyString();
|
||||
node = ROOT_NODE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clear() {
|
||||
entity = null;
|
||||
|
@ -10,7 +10,6 @@ import com.github.yulichang.config.ConfigProperties;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import com.github.yulichang.toolkit.LogicInfoUtils;
|
||||
import com.github.yulichang.toolkit.MPJWrappers;
|
||||
import com.github.yulichang.toolkit.support.ColumnCache;
|
||||
import com.github.yulichang.wrapper.interfaces.Query;
|
||||
import com.github.yulichang.wrapper.interfaces.QueryJoin;
|
||||
@ -21,10 +20,7 @@ import com.github.yulichang.wrapper.segments.SelectCache;
|
||||
import com.github.yulichang.wrapper.segments.SelectNormal;
|
||||
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.BiConsumer;
|
||||
import java.util.stream.Collectors;
|
||||
@ -34,7 +30,6 @@ import java.util.stream.Collectors;
|
||||
* Lambda 语法使用 Wrapper
|
||||
*
|
||||
* @author yulichang
|
||||
* @see MPJWrappers
|
||||
*/
|
||||
@SuppressWarnings({"unused"})
|
||||
public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWrapper<T>>
|
||||
@ -44,10 +39,6 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
* 查询表
|
||||
*/
|
||||
private final SharedString from = new SharedString();
|
||||
/**
|
||||
* 主表别名
|
||||
*/
|
||||
private final SharedString alias = new SharedString(Constant.TABLE_ALIAS);
|
||||
/**
|
||||
* 查询的字段
|
||||
*/
|
||||
@ -59,10 +50,9 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
@Getter
|
||||
private final List<MybatisLabel<?, ?>> resultMapMybatisLabel = new ArrayList<>();
|
||||
/**
|
||||
* 是否构建是否存在一对多
|
||||
* 是否有表别名
|
||||
*/
|
||||
@Getter
|
||||
private boolean resultMap = false;
|
||||
private boolean hasAlias;
|
||||
/**
|
||||
* 查询字段 sql
|
||||
*/
|
||||
@ -71,10 +61,6 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
* 是否 select distinct
|
||||
*/
|
||||
private boolean selectDistinct = false;
|
||||
/**
|
||||
* 表序号
|
||||
*/
|
||||
private int tableIndex = 1;
|
||||
/**
|
||||
* 连表关键字 on 条件 func 使用
|
||||
*/
|
||||
@ -104,6 +90,23 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
setEntityClass(clazz);
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义主表别名
|
||||
*/
|
||||
public MPJLambdaWrapper(String alias) {
|
||||
this.alias = alias;
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
/**
|
||||
* 推荐使用此构造方法
|
||||
*/
|
||||
public MPJLambdaWrapper(Class<T> clazz, String alias) {
|
||||
this.alias = alias;
|
||||
setEntityClass(clazz);
|
||||
super.initNeed();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 不建议直接 new 该实例,使用 MPJWrappers.<UserDO>lambdaQuery()
|
||||
@ -185,8 +188,34 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
if (i.isStr()) {
|
||||
return i.getColumn();
|
||||
}
|
||||
String str = Constant.TABLE_ALIAS + getDefaultSelect(i.getIndex(), i.getClazz(), i) + StringPool.DOT + i.getColumn();
|
||||
return i.isFunc() ? (String.format(i.getFunc().getSql(), str) + Constant.AS + i.getAlias()) : (i.isHasAlias() ? (str + Constant.AS + i.getAlias()) : str);
|
||||
Table t = tableList.get(i.getClazz());
|
||||
String str;
|
||||
if (t.isHasAlias()) {
|
||||
str = t.getAlias() + StringPool.DOT + i.getColumn();
|
||||
} else {
|
||||
if (i.isLabel() && Objects.nonNull(i.getIndex())) {
|
||||
str = i.getIndex() + StringPool.DOT + i.getColumn();
|
||||
} else {
|
||||
str = t.getAlias() + getDefaultSelect(i.getIndex(), i.getClazz(), i) + StringPool.DOT + i.getColumn();
|
||||
}
|
||||
}
|
||||
if (i.isFunc()) {
|
||||
SFunction<?, ?>[] args = i.getArgs();
|
||||
if (Objects.isNull(args) || args.length == 0) {
|
||||
return String.format(i.getFunc().getSql(), str) + Constant.AS + i.getAlias();
|
||||
} else {
|
||||
return String.format(i.getFunc().getSql(), Arrays.stream(args).map(arg -> {
|
||||
Class<?> entityClass = LambdaUtils.getEntityClass(arg);
|
||||
Table table = tableList.getPositive(entityClass);
|
||||
Assert.notNull(table, "table not find by class <%s>", entityClass.getSimpleName());
|
||||
Map<String, SelectCache> mapField = ColumnCache.getMapField(entityClass);
|
||||
SelectCache cache = mapField.get(LambdaUtils.getName(arg));
|
||||
return tableList.get(cache.getClazz()).getAlias() + (Objects.isNull(table.getIndex()) ? StringPool.EMPTY : table.getIndex()) + StringPool.DOT + cache.getColumn();
|
||||
}).toArray()) + Constant.AS + i.getAlias();
|
||||
}
|
||||
} else {
|
||||
return i.isHasAlias() ? (str + Constant.AS + i.getAlias()) : str;
|
||||
}
|
||||
}).collect(Collectors.joining(StringPool.COMMA));
|
||||
sqlSelect.setStringValue(s);
|
||||
}
|
||||
@ -200,17 +229,25 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
if (StringUtils.isBlank(from.getStringValue())) {
|
||||
StringBuilder value = new StringBuilder();
|
||||
for (MPJLambdaWrapper<?> wrapper : onWrappers) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(wrapper.getJoinClass());
|
||||
Assert.notNull(info, "table not find by class <%s>", wrapper.getJoinClass().getSimpleName());
|
||||
String tableName = info.getTableName();
|
||||
value.append(StringPool.SPACE)
|
||||
.append(wrapper.getKeyWord())
|
||||
.append(StringPool.SPACE)
|
||||
.append(tableName)
|
||||
.append(Constant.SPACE_TABLE_ALIAS)
|
||||
.append(tableList.get(wrapper.getJoinClass(), wrapper.getIndex()).getIndex())
|
||||
.append(Constant.ON)
|
||||
.append(wrapper.getExpression().getNormal().getSqlSegment());
|
||||
if (StringUtils.isBlank(wrapper.from.getStringValue())) {
|
||||
TableInfo info = TableInfoHelper.getTableInfo(wrapper.getJoinClass());
|
||||
Assert.notNull(info, "table not find by class <%s>", wrapper.getJoinClass().getSimpleName());
|
||||
String tableName = info.getTableName();
|
||||
value.append(StringPool.SPACE)
|
||||
.append(wrapper.getKeyWord())
|
||||
.append(StringPool.SPACE)
|
||||
.append(tableName)
|
||||
.append(StringPool.SPACE)
|
||||
.append(wrapper.hasAlias ? wrapper.alias : (wrapper.alias + (tableList.get(wrapper.getJoinClass(), wrapper.getIndex()).getIndex())))
|
||||
.append(Constant.ON)
|
||||
.append(wrapper.getExpression().getNormal().getSqlSegment());
|
||||
} else {
|
||||
value.append(StringPool.EMPTY)
|
||||
.append(wrapper.getKeyWord())
|
||||
.append(StringPool.EMPTY)
|
||||
.append(wrapper.from.getStringValue())
|
||||
.append(StringPool.EMPTY);
|
||||
}
|
||||
}
|
||||
from.setStringValue(value.toString());
|
||||
}
|
||||
@ -218,7 +255,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
}
|
||||
|
||||
public String getAlias() {
|
||||
return alias.getStringValue();
|
||||
return alias;
|
||||
}
|
||||
|
||||
|
||||
@ -289,7 +326,7 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
return StringPool.EMPTY;
|
||||
}
|
||||
return tableList.stream().map(t -> LogicInfoUtils.getLogicInfo(t.getIndex(),
|
||||
t.getClazz())).collect(Collectors.joining(StringPool.SPACE));
|
||||
t.getClazz(), t.isHasAlias(), t.getAlias())).collect(Collectors.joining(StringPool.SPACE));
|
||||
}
|
||||
return StringPool.EMPTY;
|
||||
}
|
||||
@ -302,21 +339,48 @@ public class MPJLambdaWrapper<T> extends MPJAbstractLambdaWrapper<T, MPJLambdaWr
|
||||
}
|
||||
|
||||
/**
|
||||
* 调用此方法 keyword 前后需要带空格 比如 " LEFT JOIN " " RIGHT JOIN "
|
||||
* 内部调用, 不建议使用
|
||||
*/
|
||||
@Override
|
||||
public <R> MPJLambdaWrapper<T> join(String keyWord, Class<R> clazz, BiConsumer<MPJAbstractLambdaWrapper<T, ?>, MPJLambdaWrapper<T>> consumer) {
|
||||
public <R> MPJLambdaWrapper<T> join(String keyWord, Class<R> clazz, String tableAlias, BiConsumer<MPJAbstractLambdaWrapper<T, ?>, MPJLambdaWrapper<T>> consumer) {
|
||||
String oldIndex = this.getIndex();
|
||||
String newIndex = String.valueOf(tableIndex);
|
||||
Node n = Objects.isNull(oldIndex) ? new Node(clazz, tableIndex, ROOT_NODE) : new Node(clazz, tableIndex, this.node);
|
||||
MPJLambdaWrapper<T> instance = instance(newIndex, keyWord, clazz, n);
|
||||
this.node = n;
|
||||
tableList.add(clazz, newIndex);
|
||||
onWrappers.add(instance);
|
||||
tableIndex++;
|
||||
if (StringUtils.isBlank(tableAlias)) {
|
||||
tableList.add(clazz, newIndex, false, ConfigProperties.tableAlias);
|
||||
instance.alias = ConfigProperties.tableAlias;
|
||||
instance.hasAlias = false;
|
||||
tableIndex++;
|
||||
} else {
|
||||
tableList.add(clazz, null, true, tableAlias);
|
||||
instance.alias = tableAlias;
|
||||
instance.hasAlias = true;
|
||||
}
|
||||
this.index = newIndex;
|
||||
consumer.accept(instance, typedThis);
|
||||
this.index = oldIndex;
|
||||
return typedThis;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 自定义关键词连接
|
||||
*
|
||||
* @param keyWord 连表关键词
|
||||
* @param condition 条件
|
||||
* @param joinSql sql
|
||||
*/
|
||||
@Override
|
||||
public MPJLambdaWrapper<T> join(String keyWord, boolean condition, String joinSql) {
|
||||
if (condition) {
|
||||
MPJLambdaWrapper<T> wrapper = new MPJLambdaWrapper<>();
|
||||
wrapper.from.setStringValue(joinSql);
|
||||
wrapper.keyWord = StringPool.SPACE + keyWord + StringPool.SPACE;
|
||||
onWrappers.add(wrapper);
|
||||
}
|
||||
return typedThis;
|
||||
}
|
||||
}
|
||||
|
@ -17,6 +17,7 @@ import java.lang.reflect.Field;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -161,6 +162,16 @@ public interface Query<Children> extends Serializable {
|
||||
}
|
||||
|
||||
|
||||
default <X> Children selectFunc(String sql, Function<SelectFunc.Func, SFunction<?, ?>[]> column, String alias) {
|
||||
getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(new SelectFunc.Func())));
|
||||
return getChildren();
|
||||
}
|
||||
|
||||
default <X, S> Children selectFunc(String sql, Function<SelectFunc.Func, SFunction<?, ?>[]> column, SFunction<S, ?> alias) {
|
||||
getSelectColum().add(new SelectFunc(LambdaUtils.getName(alias), getIndex(), () -> sql, column.apply(new SelectFunc.Func())));
|
||||
return getChildren();
|
||||
}
|
||||
|
||||
/* 默认聚合函数扩展 */
|
||||
|
||||
/**
|
||||
|
@ -2,8 +2,8 @@ package com.github.yulichang.wrapper.interfaces;
|
||||
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.interfaces.MPJBaseJoin;
|
||||
import com.github.yulichang.query.interfaces.StringJoin;
|
||||
import com.github.yulichang.toolkit.Constant;
|
||||
import com.github.yulichang.toolkit.LambdaUtils;
|
||||
import com.github.yulichang.wrapper.MPJAbstractLambdaWrapper;
|
||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
||||
|
||||
@ -13,7 +13,7 @@ import java.util.function.BiConsumer;
|
||||
* @author yulichang
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity>, StringJoin<Children, Entity> {
|
||||
|
||||
/**
|
||||
* left join
|
||||
@ -26,23 +26,13 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
return join(Constant.LEFT_JOIN, clazz, left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
* left join
|
||||
*
|
||||
* @param left 条件
|
||||
* @param right 条件
|
||||
*/
|
||||
default <T, X> Children leftJoin(SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.LEFT_JOIN, left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
* left join 多条件
|
||||
* <p>
|
||||
* 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...)
|
||||
*
|
||||
* @param clazz 关联实体类
|
||||
* @param function 条件
|
||||
* @param function 条件`
|
||||
*/
|
||||
default <T> Children leftJoin(Class<T> clazz, WrapperFunction<MPJAbstractLambdaWrapper<Entity, ?>> function) {
|
||||
return join(Constant.LEFT_JOIN, clazz, function);
|
||||
@ -59,16 +49,6 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
return join(Constant.LEFT_JOIN, clazz, left, right, ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* left join
|
||||
*
|
||||
* @param left 条件
|
||||
* @param right 条件
|
||||
*/
|
||||
default <T, X> Children leftJoin(SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(Constant.LEFT_JOIN, left, right, ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* left join 多条件
|
||||
* <p>
|
||||
@ -82,17 +62,56 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
* left join
|
||||
*
|
||||
* @param clazz 关联的实体类
|
||||
* @param left 条件
|
||||
* @param right 条件
|
||||
*/
|
||||
default <T, X> Children rightJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.RIGHT_JOIN, clazz, left, right);
|
||||
default <T, X> Children leftJoin(Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.LEFT_JOIN, clazz,alias, left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
* left join 多条件
|
||||
* <p>
|
||||
* 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...)
|
||||
*
|
||||
* @param clazz 关联实体类
|
||||
* @param function 条件
|
||||
*/
|
||||
default <T> Children leftJoin(Class<T> clazz, String alias, WrapperFunction<MPJAbstractLambdaWrapper<Entity, ?>> function) {
|
||||
return join(Constant.LEFT_JOIN, clazz,alias, function);
|
||||
}
|
||||
|
||||
/**
|
||||
* left join
|
||||
*
|
||||
* @param clazz 关联的实体类
|
||||
* @param left 条件
|
||||
* @param right 条件
|
||||
*/
|
||||
default <T, X> Children leftJoin(Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(Constant.LEFT_JOIN, clazz,alias, left, right, ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* left join 多条件
|
||||
* <p>
|
||||
* 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...)
|
||||
*
|
||||
* @param clazz 关联实体类
|
||||
* @param consumer 条件
|
||||
*/
|
||||
default <T> Children leftJoin(Class<T> clazz, String alias, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(Constant.LEFT_JOIN, clazz,alias, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children rightJoin(SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.RIGHT_JOIN, left, right);
|
||||
default <T, X> Children rightJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.RIGHT_JOIN, clazz, left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -112,15 +131,36 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children rightJoin(SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(Constant.RIGHT_JOIN, left, right, ext);
|
||||
default <T, X> Children rightJoin(Class<T> clazz, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(Constant.RIGHT_JOIN, clazz, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children rightJoin(Class<T> clazz, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(Constant.RIGHT_JOIN, clazz, consumer);
|
||||
default <T, X> Children rightJoin(Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.RIGHT_JOIN, clazz,alias, left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T> Children rightJoin(Class<T> clazz, String alias, WrapperFunction<MPJAbstractLambdaWrapper<Entity, ?>> function) {
|
||||
return join(Constant.RIGHT_JOIN, clazz,alias, function);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children rightJoin(Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(Constant.RIGHT_JOIN, clazz,alias, left, right, ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children rightJoin(Class<T> clazz, String alias, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(Constant.RIGHT_JOIN, clazz,alias, consumer);
|
||||
}
|
||||
|
||||
|
||||
@ -131,13 +171,6 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
return join(Constant.INNER_JOIN, clazz, on -> on.eq(left, right));
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children innerJoin(SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.INNER_JOIN, LambdaUtils.getEntityClass(left), on -> on.eq(left, right));
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
@ -152,13 +185,6 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
return join(Constant.INNER_JOIN, clazz, left, right, ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children innerJoin(SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(Constant.INNER_JOIN, left, right, ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
@ -170,15 +196,36 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children fullJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.FULL_JOIN, clazz, left, right);
|
||||
default <T, X> Children innerJoin(Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.INNER_JOIN, clazz,alias, on -> on.eq(left, right));
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children fullJoin(SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.FULL_JOIN, left, right);
|
||||
default <T> Children innerJoin(Class<T> clazz, String alias, WrapperFunction<MPJAbstractLambdaWrapper<Entity, ?>> function) {
|
||||
return join(Constant.INNER_JOIN, clazz,alias, function);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children innerJoin(Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(Constant.INNER_JOIN, clazz,alias, left, right, ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T> Children innerJoin(Class<T> clazz, String alias, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(Constant.INNER_JOIN, clazz,alias, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children fullJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.FULL_JOIN, clazz, left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -198,15 +245,36 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children fullJoin(SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(Constant.FULL_JOIN, left, right, ext);
|
||||
default <T> Children fullJoin(Class<T> clazz, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(Constant.FULL_JOIN, clazz, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T> Children fullJoin(Class<T> clazz, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(Constant.FULL_JOIN, clazz, consumer);
|
||||
default <T, X> Children fullJoin(Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(Constant.FULL_JOIN, clazz,alias, left, right);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T> Children fullJoin(Class<T> clazz, String alias, WrapperFunction<MPJAbstractLambdaWrapper<Entity, ?>> function) {
|
||||
return join(Constant.FULL_JOIN, clazz,alias, function);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T, X> Children fullJoin(Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(Constant.FULL_JOIN, clazz,alias, left, right, ext);
|
||||
}
|
||||
|
||||
/**
|
||||
* ignore 参考 left join
|
||||
*/
|
||||
default <T> Children fullJoin(Class<T> clazz, String alias, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(Constant.FULL_JOIN, clazz,alias, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -224,16 +292,6 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
return join(keyWord, clazz, on -> on.eq(left, right));
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义连表关键词
|
||||
*
|
||||
* @param left 条件
|
||||
* @param right 条件
|
||||
*/
|
||||
default <T, X> Children join(String keyWord, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(keyWord, LambdaUtils.getEntityClass(left), on -> on.eq(left, right));
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义连表关键词
|
||||
* <p>
|
||||
@ -262,16 +320,54 @@ public interface QueryJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
||||
|
||||
/**
|
||||
* 自定义连表关键词
|
||||
* 调用此方法 keyword 前后需要带空格 比如 " LEFT JOIN " " RIGHT JOIN "
|
||||
* <p>
|
||||
* 查询基类 可以直接调用此方法实现以上所有功能
|
||||
*
|
||||
* @param keyWord 连表关键字
|
||||
* @param clazz 连表实体类
|
||||
* @param left 关联条件
|
||||
* @param right 扩展 用于关联表的 select 和 where
|
||||
*/
|
||||
default <T, X> Children join(String keyWord, Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||
return join(keyWord, clazz,alias, on -> on.eq(left, right));
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义连表关键词
|
||||
* <p>
|
||||
* 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...)
|
||||
*
|
||||
* @param clazz 关联实体类
|
||||
* @param function 条件
|
||||
*/
|
||||
default <T> Children join(String keyWord, Class<T> clazz, String alias, WrapperFunction<MPJAbstractLambdaWrapper<Entity, ?>> function) {
|
||||
return join(keyWord, clazz,alias, (on, e) -> function.apply(on));
|
||||
}
|
||||
|
||||
/**
|
||||
* 自定义连表关键词
|
||||
*
|
||||
* @param clazz 关联的实体类
|
||||
* @param left 条件
|
||||
* @param right 条件
|
||||
*/
|
||||
default <T, X> Children join(String keyWord, SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(keyWord, LambdaUtils.getEntityClass(left), left, right, ext);
|
||||
default <T, X> Children join(String keyWord, Class<T> clazz, String alias, SFunction<T, ?> left, SFunction<X, ?> right, WrapperFunction<MPJLambdaWrapper<Entity>> ext) {
|
||||
return join(keyWord, clazz,alias, (on, e) -> {
|
||||
on.eq(left, right);
|
||||
ext.apply(e);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部使用, 不建议直接调用
|
||||
*/
|
||||
<T> Children join(String keyWord, Class<T> clazz, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer);
|
||||
default <T> Children join(String keyWord, Class<T> clazz, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer) {
|
||||
return join(keyWord, clazz, null, consumer);
|
||||
}
|
||||
|
||||
/**
|
||||
* 内部使用, 不建议直接调用
|
||||
*/
|
||||
<T> Children join(String keyWord, Class<T> clazz, String alias, BiConsumer<MPJAbstractLambdaWrapper<Entity, ?>, MPJLambdaWrapper<Entity>> consumer);
|
||||
}
|
||||
|
@ -46,20 +46,19 @@ public interface QueryLabel<Children> {
|
||||
return selectCollection(null, child, dtoField);
|
||||
}
|
||||
|
||||
default <S, C, Z, F extends java.util.Collection<?>> Children selectCollection(Integer index, Class<C> child, SFunction<S, F> dtoField) {
|
||||
default <S, C, Z, F extends java.util.Collection<?>> Children selectCollection(String prefix, Class<C> child, SFunction<S, F> dtoField) {
|
||||
String dtoFieldName = LambdaUtils.getName(dtoField);
|
||||
Class<S> dtoClass = LambdaUtils.getEntityClass(dtoField);
|
||||
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
|
||||
Field field = fieldMap.get(dtoFieldName);
|
||||
Class<?> genericType = MPJReflectionKit.getGenericType(field);
|
||||
MybatisLabel.Builder<C, Z> builder;
|
||||
String s = Objects.isNull(index) ? null : index.toString();
|
||||
if (genericType == null || genericType.isAssignableFrom(child)) {
|
||||
//找不到集合泛型 List List<?> List<Object> , 直接查询数据库实体
|
||||
builder = new MybatisLabel.Builder<>(s, dtoFieldName, child, field.getType());
|
||||
builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType());
|
||||
} else {
|
||||
Class<Z> ofType = (Class<Z>) genericType;
|
||||
builder = new MybatisLabel.Builder<>(s, dtoFieldName, child, field.getType(), ofType, true);
|
||||
builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType(), ofType, true);
|
||||
}
|
||||
addLabel(builder.build());
|
||||
return getChildren();
|
||||
@ -99,7 +98,7 @@ public interface QueryLabel<Children> {
|
||||
return selectCollection(null, child, dtoField, collection);
|
||||
}
|
||||
|
||||
default <S, C, Z, F extends java.util.Collection<Z>> Children selectCollection(Integer index,
|
||||
default <S, C, Z, F extends java.util.Collection<Z>> Children selectCollection(String prefix,
|
||||
Class<C> child,
|
||||
SFunction<S, F> dtoField,
|
||||
MFunc<MybatisLabel.Builder<C, Z>> collection) {
|
||||
@ -109,8 +108,7 @@ public interface QueryLabel<Children> {
|
||||
//获取集合泛型
|
||||
Class<?> genericType = MPJReflectionKit.getGenericType(field);
|
||||
Class<Z> ofType = (Class<Z>) genericType;
|
||||
MybatisLabel.Builder<C, Z> builder = new MybatisLabel.Builder<>(Objects.isNull(index) ? null : index.toString(),
|
||||
dtoFieldName, child, field.getType(), ofType, false);
|
||||
MybatisLabel.Builder<C, Z> builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType(), ofType, false);
|
||||
MybatisLabel.Builder<C, Z> czBuilder = collection.apply(builder);
|
||||
addLabel(czBuilder.build());
|
||||
return getChildren();
|
||||
|
@ -121,20 +121,19 @@ public class MybatisLabel<E, T> {
|
||||
/**
|
||||
* 嵌套
|
||||
*/
|
||||
public <A, R, B extends Collection<R>> Builder<E, T> collection(Integer index, Class<A> entityClass, SFunction<T, B> func) {
|
||||
public <A, R, B extends Collection<R>> Builder<E, T> collection(String prefix, Class<A> entityClass, SFunction<T, B> func) {
|
||||
String dtoFieldName = LambdaUtils.getName(func);
|
||||
Class<T> dtoClass = LambdaUtils.getEntityClass(func);
|
||||
Map<String, Field> fieldMap = MPJReflectionKit.getFieldMap(dtoClass);
|
||||
Field field = fieldMap.get(dtoFieldName);
|
||||
Class<?> genericType = MPJReflectionKit.getGenericType(field);
|
||||
MybatisLabel.Builder<A, R> builder;
|
||||
String s = Objects.isNull(index) ? null : index.toString();
|
||||
if (genericType == null || genericType.isAssignableFrom(entityClass)) {
|
||||
//找不到集合泛型 List List<?> List<Object> , 直接查询数据库实体
|
||||
builder = new Builder<>(s, dtoFieldName, entityClass, field.getType());
|
||||
builder = new Builder<>(prefix, dtoFieldName, entityClass, field.getType());
|
||||
} else {
|
||||
Class<R> ofType = (Class<R>) genericType;
|
||||
builder = new Builder<>(s, dtoFieldName, entityClass, field.getType(), ofType, true);
|
||||
builder = new Builder<>(prefix, dtoFieldName, entityClass, field.getType(), ofType, true);
|
||||
}
|
||||
mybatisLabel.mybatisLabels.add(builder.build());
|
||||
return this;
|
||||
@ -147,7 +146,7 @@ public class MybatisLabel<E, T> {
|
||||
/**
|
||||
* 嵌套
|
||||
*/
|
||||
public <A, R, B extends Collection<R>> Builder<E, T> collection(Integer index,
|
||||
public <A, R, B extends Collection<R>> Builder<E, T> collection(String prefix,
|
||||
Class<A> entityClass,
|
||||
SFunction<T, B> func,
|
||||
MFunc<Builder<A, R>> mFunc) {
|
||||
@ -157,8 +156,7 @@ public class MybatisLabel<E, T> {
|
||||
//获取集合泛型
|
||||
Class<?> genericType = MPJReflectionKit.getGenericType(field);
|
||||
Class<R> ofType = (Class<R>) genericType;
|
||||
MybatisLabel.Builder<A, R> builder = new MybatisLabel.Builder<>(Objects.isNull(index) ? null : index.toString(),
|
||||
dtoFieldName, entityClass, field.getType(), ofType, false);
|
||||
MybatisLabel.Builder<A, R> builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, entityClass, field.getType(), ofType, false);
|
||||
mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build());
|
||||
return this;
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.github.yulichang.wrapper.segments;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
|
||||
@ -38,6 +39,8 @@ public interface Select {
|
||||
|
||||
boolean isFunc();
|
||||
|
||||
SFunction<?,?>[] getArgs();
|
||||
|
||||
BaseFuncEnum getFunc();
|
||||
|
||||
boolean isLabel();
|
||||
|
@ -2,6 +2,7 @@ package com.github.yulichang.wrapper.segments;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import lombok.Getter;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
@ -82,6 +83,11 @@ public class SelectAlias implements Select {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SFunction<?, ?>[] getArgs() {
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public BaseFuncEnum getFunc() {
|
||||
|
@ -2,6 +2,7 @@ package com.github.yulichang.wrapper.segments;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import lombok.Getter;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
@ -23,6 +24,8 @@ public class SelectFunc implements Select {
|
||||
|
||||
private final String column;
|
||||
|
||||
private final SFunction<?, ?>[] args;
|
||||
|
||||
private final boolean hasAlias;
|
||||
|
||||
private final String alias;
|
||||
@ -37,6 +40,7 @@ public class SelectFunc implements Select {
|
||||
this.cache = cache;
|
||||
this.column = cache.getColumn();
|
||||
this.hasAlias = true;
|
||||
this.args = null;
|
||||
this.alias = alias;
|
||||
this.isFunc = true;
|
||||
this.func = func;
|
||||
@ -45,6 +49,18 @@ public class SelectFunc implements Select {
|
||||
public SelectFunc(String alias, String index, BaseFuncEnum func, String column) {
|
||||
this.index = index;
|
||||
this.column = column;
|
||||
this.args = null;
|
||||
this.cache = null;
|
||||
this.hasAlias = true;
|
||||
this.alias = alias;
|
||||
this.isFunc = true;
|
||||
this.func = func;
|
||||
}
|
||||
|
||||
public SelectFunc(String alias, String index, BaseFuncEnum func, SFunction<?, ?>[] args) {
|
||||
this.index = index;
|
||||
this.column = null;
|
||||
this.args = args;
|
||||
this.cache = null;
|
||||
this.hasAlias = true;
|
||||
this.alias = alias;
|
||||
@ -103,4 +119,54 @@ public class SelectFunc implements Select {
|
||||
public boolean isStr() {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 泛型不同不能使用可变参数
|
||||
* 我想10个参数应该够了吧...
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public static class Func {
|
||||
|
||||
public final <A> SFunction<?, ?>[] accept(SFunction<A, ?> a) {
|
||||
return new SFunction[]{a};
|
||||
}
|
||||
|
||||
public final <A, B> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b) {
|
||||
return new SFunction[]{a, b};
|
||||
}
|
||||
|
||||
public final <A, B, C> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c) {
|
||||
return new SFunction[]{a, b, c};
|
||||
}
|
||||
|
||||
public final <A, B, C, D> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d) {
|
||||
return new SFunction[]{a, b, c, d};
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e) {
|
||||
return new SFunction[]{a, b, c, d, e};
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f) {
|
||||
return new SFunction[]{a, b, c, d, e, f};
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F, G> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g) {
|
||||
return new SFunction[]{a, b, c, d, e, f, g};
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F, G, H> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h) {
|
||||
return new SFunction[]{a, b, c, d, e, f, g, h};
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F, G, H, I> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h, SFunction<I, ?> i) {
|
||||
return new SFunction[]{a, b, c, d, e, f, g, h, i};
|
||||
}
|
||||
|
||||
public final <A, B, C, D, E, F, G, H, I, J> SFunction<?, ?>[] accept(SFunction<A, ?> a, SFunction<B, ?> b, SFunction<C, ?> c, SFunction<D, ?> d, SFunction<E, ?> e, SFunction<F, ?> f, SFunction<G, ?> g, SFunction<H, ?> h, SFunction<I, ?> i, SFunction<J, ?> j) {
|
||||
return new SFunction[]{a, b, c, d, e, f, g, h, j};
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ package com.github.yulichang.wrapper.segments;
|
||||
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import lombok.Getter;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
@ -107,6 +108,11 @@ public class SelectLabel implements Select {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SFunction<?, ?>[] getArgs() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseFuncEnum getFunc() {
|
||||
return null;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.github.yulichang.wrapper.segments;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import lombok.Getter;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
@ -84,6 +85,11 @@ public class SelectNormal implements Select {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SFunction<?, ?>[] getArgs() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseFuncEnum getFunc() {
|
||||
return null;
|
||||
|
@ -1,6 +1,7 @@
|
||||
package com.github.yulichang.wrapper.segments;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||
import com.github.yulichang.wrapper.enums.BaseFuncEnum;
|
||||
import org.apache.ibatis.type.TypeHandler;
|
||||
|
||||
@ -82,6 +83,11 @@ public class SelectString implements Select {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SFunction<?, ?>[] getArgs() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public BaseFuncEnum getFunc() {
|
||||
return null;
|
||||
|
Loading…
x
Reference in New Issue
Block a user