diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java index 514e4e3..b2c79a3 100644 --- a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinAutoConfiguration.java @@ -58,6 +58,7 @@ public class MybatisPlusJoinAutoConfiguration { this.properties = properties; ConfigProperties.subTableLogic = properties.getSubTableLogic(); ConfigProperties.msCache = properties.isMsCache(); + ConfigProperties.tableAlias = properties.getTableAlias(); } /** diff --git a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java index ac0dc62..f3d842b 100644 --- a/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java +++ b/mybatis-plus-join-boot-starter/src/main/java/com/github/yulichang/autoconfigure/MybatisPlusJoinProperties.java @@ -20,6 +20,11 @@ public class MybatisPlusJoinProperties { */ private Boolean banner = true; + /** + * 表别名 + */ + private String tableAlias = "t"; + /** * 连表查询副表是否启用逻辑删除(前提是MP配置了逻辑删除) */ diff --git a/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json index 62d2698..04c9c5b 100644 --- a/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/mybatis-plus-join-boot-starter/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -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": "表别名." } ] } \ No newline at end of file diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java index 1230570..eda7d8c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/config/ConfigProperties.java @@ -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"; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java index 9d3ed84..ae810e2 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java @@ -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 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); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java index 2867e58..3ac093c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/method/MPJBaseMethod.java @@ -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()); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java index 38baa6e..9214664 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/MPJLambdaQueryWrapper.java @@ -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 extends AbstractLambdaWrapper> - implements Query, T, SFunction>, MPJJoin, T> { + implements Query, T, SFunction>, StringJoin, T> { /** * 查询字段 @@ -46,7 +46,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper extends AbstractLambdaWrapper selectIgnore(SFunction... columns) { if (ArrayUtils.isNotEmpty(columns)) { for (SFunction 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 extends AbstractLambdaWrapper column, boolean onlyColumn) { - return Constant.TABLE_ALIAS + StringPool.DOT + super.columnToString(column, onlyColumn); + return alias + StringPool.DOT + super.columnToString(column, onlyColumn); } public MPJLambdaQueryWrapper select(String... columns) { @@ -162,7 +162,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper", 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 extends AbstractLambdaWrapper selectAll(Class clazz) { - return selectAll(clazz, Constant.TABLE_ALIAS); + return selectAll(clazz, alias); } /** @@ -219,7 +219,7 @@ public class MPJLambdaQueryWrapper extends AbstractLambdaWrapper extends AbstractWrapper> - implements Query, T, String>, MPJJoin, T> { + implements Query, T, String>, StringJoin, T> { /** * 查询字段 @@ -44,7 +44,7 @@ public class MPJQueryWrapper extends AbstractWrapper extends AbstractWrapper 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; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/MPJJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/StringJoin.java similarity index 95% rename from mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/MPJJoin.java rename to mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/StringJoin.java index 35f11c4..378db91 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/MPJJoin.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/query/interfaces/StringJoin.java @@ -7,7 +7,7 @@ import com.github.yulichang.toolkit.Constant; * @author yulichang */ @SuppressWarnings("unused") -public interface MPJJoin extends MPJBaseJoin { +public interface StringJoin extends MPJBaseJoin { /** * left join diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java index 2eaffab..0488cc0 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/Constant.java @@ -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; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java index d3b7abf..644c22a 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/toolkit/LogicInfoUtils.java @@ -21,25 +21,26 @@ public class LogicInfoUtils implements Constants { private static final Map, Map> 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 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; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java index f4c374f..9d6c125 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractLambdaWrapper.java @@ -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> extends MPJAbstractWrapper { + /** + * 主表别名 + */ + 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 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 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 clazz, String index, String alias) { + DEFAULT_TABLE = new Table(clazz, index, false, alias); + } private final List 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 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); + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java index 90126f7..3c06ccd 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJAbstractWrapper.java @@ -572,19 +572,6 @@ public abstract class MPJAbstractWrapper(16); - expression = new MergeSegments(); - lastSql = SharedString.emptyString(); - sqlComment = SharedString.emptyString(); - sqlFirst = SharedString.emptyString(); - node = ROOT_NODE; - } - @Override public void clear() { entity = null; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java index 7bb7324..4fce469 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -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 extends MPJAbstractLambdaWrapper> @@ -44,10 +39,6 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper> resultMapMybatisLabel = new ArrayList<>(); /** - * 是否构建是否存在一对多 + * 是否有表别名 */ - @Getter - private boolean resultMap = false; + private boolean hasAlias; /** * 查询字段 sql */ @@ -71,10 +61,6 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper clazz, String alias) { + this.alias = alias; + setEntityClass(clazz); + super.initNeed(); + } + /** * 不建议直接 new 该实例,使用 MPJWrappers.lambdaQuery() @@ -185,8 +188,34 @@ public class MPJLambdaWrapper extends MPJAbstractLambdaWrapper[] 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 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 extends MPJAbstractLambdaWrapper 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 extends MPJAbstractLambdaWrapper extends MPJAbstractLambdaWrapper 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 extends MPJAbstractLambdaWrapper MPJLambdaWrapper join(String keyWord, Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + public MPJLambdaWrapper join(String keyWord, Class clazz, String tableAlias, BiConsumer, MPJLambdaWrapper> 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 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 join(String keyWord, boolean condition, String joinSql) { + if (condition) { + MPJLambdaWrapper wrapper = new MPJLambdaWrapper<>(); + wrapper.from.setStringValue(joinSql); + wrapper.keyWord = StringPool.SPACE + keyWord + StringPool.SPACE; + onWrappers.add(wrapper); + } + return typedThis; + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java index 554b452..e4178aa 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/Query.java @@ -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 extends Serializable { } + default Children selectFunc(String sql, Function[]> column, String alias) { + getSelectColum().add(new SelectFunc(alias, getIndex(), () -> sql, column.apply(new SelectFunc.Func()))); + return getChildren(); + } + + default Children selectFunc(String sql, Function[]> column, SFunction alias) { + getSelectColum().add(new SelectFunc(LambdaUtils.getName(alias), getIndex(), () -> sql, column.apply(new SelectFunc.Func()))); + return getChildren(); + } + /* 默认聚合函数扩展 */ /** diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java index 0734491..d933773 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryJoin.java @@ -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 extends MPJBaseJoin { +public interface QueryJoin extends MPJBaseJoin, StringJoin { /** * left join @@ -26,23 +26,13 @@ public interface QueryJoin extends MPJBaseJoin { return join(Constant.LEFT_JOIN, clazz, left, right); } - /** - * left join - * - * @param left 条件 - * @param right 条件 - */ - default Children leftJoin(SFunction left, SFunction right) { - return join(Constant.LEFT_JOIN, left, right); - } - /** * left join 多条件 *

* 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) * * @param clazz 关联实体类 - * @param function 条件 + * @param function 条件` */ default Children leftJoin(Class clazz, WrapperFunction> function) { return join(Constant.LEFT_JOIN, clazz, function); @@ -59,16 +49,6 @@ public interface QueryJoin extends MPJBaseJoin { return join(Constant.LEFT_JOIN, clazz, left, right, ext); } - /** - * left join - * - * @param left 条件 - * @param right 条件 - */ - default Children leftJoin(SFunction left, SFunction right, WrapperFunction> ext) { - return join(Constant.LEFT_JOIN, left, right, ext); - } - /** * left join 多条件 *

@@ -82,17 +62,56 @@ public interface QueryJoin extends MPJBaseJoin { } /** - * ignore 参考 left join + * left join + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 */ - default Children rightJoin(Class clazz, SFunction left, SFunction right) { - return join(Constant.RIGHT_JOIN, clazz, left, right); + default Children leftJoin(Class clazz, String alias, SFunction left, SFunction right) { + return join(Constant.LEFT_JOIN, clazz,alias, left, right); + } + + /** + * left join 多条件 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param function 条件 + */ + default Children leftJoin(Class clazz, String alias, WrapperFunction> function) { + return join(Constant.LEFT_JOIN, clazz,alias, function); + } + + /** + * left join + * + * @param clazz 关联的实体类 + * @param left 条件 + * @param right 条件 + */ + default Children leftJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.LEFT_JOIN, clazz,alias, left, right, ext); + } + + /** + * left join 多条件 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param consumer 条件 + */ + default Children leftJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.LEFT_JOIN, clazz,alias, consumer); } /** * ignore 参考 left join */ - default Children rightJoin(SFunction left, SFunction right) { - return join(Constant.RIGHT_JOIN, left, right); + default Children rightJoin(Class clazz, SFunction left, SFunction right) { + return join(Constant.RIGHT_JOIN, clazz, left, right); } /** @@ -112,15 +131,36 @@ public interface QueryJoin extends MPJBaseJoin { /** * ignore 参考 left join */ - default Children rightJoin(SFunction left, SFunction right, WrapperFunction> ext) { - return join(Constant.RIGHT_JOIN, left, right, ext); + default Children rightJoin(Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.RIGHT_JOIN, clazz, consumer); } /** * ignore 参考 left join */ - default Children rightJoin(Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { - return join(Constant.RIGHT_JOIN, clazz, consumer); + default Children rightJoin(Class clazz, String alias, SFunction left, SFunction right) { + return join(Constant.RIGHT_JOIN, clazz,alias, left, right); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, String alias, WrapperFunction> function) { + return join(Constant.RIGHT_JOIN, clazz,alias, function); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.RIGHT_JOIN, clazz,alias, left, right, ext); + } + + /** + * ignore 参考 left join + */ + default Children rightJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.RIGHT_JOIN, clazz,alias, consumer); } @@ -131,13 +171,6 @@ public interface QueryJoin extends MPJBaseJoin { return join(Constant.INNER_JOIN, clazz, on -> on.eq(left, right)); } - /** - * ignore 参考 left join - */ - default Children innerJoin(SFunction left, SFunction right) { - return join(Constant.INNER_JOIN, LambdaUtils.getEntityClass(left), on -> on.eq(left, right)); - } - /** * ignore 参考 left join */ @@ -152,13 +185,6 @@ public interface QueryJoin extends MPJBaseJoin { return join(Constant.INNER_JOIN, clazz, left, right, ext); } - /** - * ignore 参考 left join - */ - default Children innerJoin(SFunction left, SFunction right, WrapperFunction> ext) { - return join(Constant.INNER_JOIN, left, right, ext); - } - /** * ignore 参考 left join */ @@ -170,15 +196,36 @@ public interface QueryJoin extends MPJBaseJoin { /** * ignore 参考 left join */ - default Children fullJoin(Class clazz, SFunction left, SFunction right) { - return join(Constant.FULL_JOIN, clazz, left, right); + default Children innerJoin(Class clazz, String alias, SFunction left, SFunction right) { + return join(Constant.INNER_JOIN, clazz,alias, on -> on.eq(left, right)); } /** * ignore 参考 left join */ - default Children fullJoin(SFunction left, SFunction right) { - return join(Constant.FULL_JOIN, left, right); + default Children innerJoin(Class clazz, String alias, WrapperFunction> function) { + return join(Constant.INNER_JOIN, clazz,alias, function); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.INNER_JOIN, clazz,alias, left, right, ext); + } + + /** + * ignore 参考 left join + */ + default Children innerJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.INNER_JOIN, clazz,alias, consumer); + } + + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, SFunction left, SFunction right) { + return join(Constant.FULL_JOIN, clazz, left, right); } /** @@ -198,15 +245,36 @@ public interface QueryJoin extends MPJBaseJoin { /** * ignore 参考 left join */ - default Children fullJoin(SFunction left, SFunction right, WrapperFunction> ext) { - return join(Constant.FULL_JOIN, left, right, ext); + default Children fullJoin(Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.FULL_JOIN, clazz, consumer); } /** * ignore 参考 left join */ - default Children fullJoin(Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { - return join(Constant.FULL_JOIN, clazz, consumer); + default Children fullJoin(Class clazz, String alias, SFunction left, SFunction right) { + return join(Constant.FULL_JOIN, clazz,alias, left, right); + } + + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, String alias, WrapperFunction> function) { + return join(Constant.FULL_JOIN, clazz,alias, function); + } + + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(Constant.FULL_JOIN, clazz,alias, left, right, ext); + } + + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer) { + return join(Constant.FULL_JOIN, clazz,alias, consumer); } /** @@ -224,16 +292,6 @@ public interface QueryJoin extends MPJBaseJoin { return join(keyWord, clazz, on -> on.eq(left, right)); } - /** - * 自定义连表关键词 - * - * @param left 条件 - * @param right 条件 - */ - default Children join(String keyWord, SFunction left, SFunction right) { - return join(keyWord, LambdaUtils.getEntityClass(left), on -> on.eq(left, right)); - } - /** * 自定义连表关键词 *

@@ -262,16 +320,54 @@ public interface QueryJoin extends MPJBaseJoin { /** * 自定义连表关键词 + * 调用此方法 keyword 前后需要带空格 比如 " LEFT JOIN " " RIGHT JOIN " + *

+ * 查询基类 可以直接调用此方法实现以上所有功能 * + * @param keyWord 连表关键字 + * @param clazz 连表实体类 + * @param left 关联条件 + * @param right 扩展 用于关联表的 select 和 where + */ + default Children join(String keyWord, Class clazz, String alias, SFunction left, SFunction right) { + return join(keyWord, clazz,alias, on -> on.eq(left, right)); + } + + /** + * 自定义连表关键词 + *

+ * 例 leftJoin(UserDO.class, on -> on.eq(UserDO::getId,UserAddressDO::getUserId).le().gt()...) + * + * @param clazz 关联实体类 + * @param function 条件 + */ + default Children join(String keyWord, Class clazz, String alias, WrapperFunction> function) { + return join(keyWord, clazz,alias, (on, e) -> function.apply(on)); + } + + /** + * 自定义连表关键词 + * + * @param clazz 关联的实体类 * @param left 条件 * @param right 条件 */ - default Children join(String keyWord, SFunction left, SFunction right, WrapperFunction> ext) { - return join(keyWord, LambdaUtils.getEntityClass(left), left, right, ext); + default Children join(String keyWord, Class clazz, String alias, SFunction left, SFunction right, WrapperFunction> ext) { + return join(keyWord, clazz,alias, (on, e) -> { + on.eq(left, right); + ext.apply(e); + }); } /** * 内部使用, 不建议直接调用 */ - Children join(String keyWord, Class clazz, BiConsumer, MPJLambdaWrapper> consumer); + default Children join(String keyWord, Class clazz, BiConsumer, MPJLambdaWrapper> consumer) { + return join(keyWord, clazz, null, consumer); + } + + /** + * 内部使用, 不建议直接调用 + */ + Children join(String keyWord, Class clazz, String alias, BiConsumer, MPJLambdaWrapper> consumer); } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryLabel.java index 3b86af0..b9b5651 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/interfaces/QueryLabel.java @@ -46,20 +46,19 @@ public interface QueryLabel { return selectCollection(null, child, dtoField); } - default > Children selectCollection(Integer index, Class child, SFunction dtoField) { + default > Children selectCollection(String prefix, Class child, SFunction dtoField) { String dtoFieldName = LambdaUtils.getName(dtoField); Class dtoClass = LambdaUtils.getEntityClass(dtoField); Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass); Field field = fieldMap.get(dtoFieldName); Class genericType = MPJReflectionKit.getGenericType(field); MybatisLabel.Builder builder; - String s = Objects.isNull(index) ? null : index.toString(); if (genericType == null || genericType.isAssignableFrom(child)) { //找不到集合泛型 List List List , 直接查询数据库实体 - builder = new MybatisLabel.Builder<>(s, dtoFieldName, child, field.getType()); + builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType()); } else { Class ofType = (Class) 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 { return selectCollection(null, child, dtoField, collection); } - default > Children selectCollection(Integer index, + default > Children selectCollection(String prefix, Class child, SFunction dtoField, MFunc> collection) { @@ -109,8 +108,7 @@ public interface QueryLabel { //获取集合泛型 Class genericType = MPJReflectionKit.getGenericType(field); Class ofType = (Class) genericType; - MybatisLabel.Builder builder = new MybatisLabel.Builder<>(Objects.isNull(index) ? null : index.toString(), - dtoFieldName, child, field.getType(), ofType, false); + MybatisLabel.Builder builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, child, field.getType(), ofType, false); MybatisLabel.Builder czBuilder = collection.apply(builder); addLabel(czBuilder.build()); return getChildren(); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java index d94939e..a4f1c83 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/resultmap/MybatisLabel.java @@ -121,20 +121,19 @@ public class MybatisLabel { /** * 嵌套 */ - public > Builder collection(Integer index, Class entityClass, SFunction func) { + public > Builder collection(String prefix, Class entityClass, SFunction func) { String dtoFieldName = LambdaUtils.getName(func); Class dtoClass = LambdaUtils.getEntityClass(func); Map fieldMap = MPJReflectionKit.getFieldMap(dtoClass); Field field = fieldMap.get(dtoFieldName); Class genericType = MPJReflectionKit.getGenericType(field); MybatisLabel.Builder builder; - String s = Objects.isNull(index) ? null : index.toString(); if (genericType == null || genericType.isAssignableFrom(entityClass)) { //找不到集合泛型 List List List , 直接查询数据库实体 - builder = new Builder<>(s, dtoFieldName, entityClass, field.getType()); + builder = new Builder<>(prefix, dtoFieldName, entityClass, field.getType()); } else { Class ofType = (Class) 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 { /** * 嵌套 */ - public > Builder collection(Integer index, + public > Builder collection(String prefix, Class entityClass, SFunction func, MFunc> mFunc) { @@ -157,8 +156,7 @@ public class MybatisLabel { //获取集合泛型 Class genericType = MPJReflectionKit.getGenericType(field); Class ofType = (Class) genericType; - MybatisLabel.Builder builder = new MybatisLabel.Builder<>(Objects.isNull(index) ? null : index.toString(), - dtoFieldName, entityClass, field.getType(), ofType, false); + MybatisLabel.Builder builder = new MybatisLabel.Builder<>(prefix, dtoFieldName, entityClass, field.getType(), ofType, false); mybatisLabel.mybatisLabels.add(mFunc.apply(builder).build()); return this; } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java index 973a507..51408f5 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/Select.java @@ -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(); diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java index 185956f..92b982c 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectAlias.java @@ -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() { diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java index 9db55ee..f6e2bde 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectFunc.java @@ -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 SFunction[] accept(SFunction a) { + return new SFunction[]{a}; + } + + public final SFunction[] accept(SFunction a, SFunction b) { + return new SFunction[]{a, b}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c) { + return new SFunction[]{a, b, c}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d) { + return new SFunction[]{a, b, c, d}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e) { + return new SFunction[]{a, b, c, d, e}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f) { + return new SFunction[]{a, b, c, d, e, f}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f, SFunction g) { + return new SFunction[]{a, b, c, d, e, f, g}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f, SFunction g, SFunction h) { + return new SFunction[]{a, b, c, d, e, f, g, h}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f, SFunction g, SFunction h, SFunction i) { + return new SFunction[]{a, b, c, d, e, f, g, h, i}; + } + + public final SFunction[] accept(SFunction a, SFunction b, SFunction c, SFunction d, SFunction e, SFunction f, SFunction g, SFunction h, SFunction i, SFunction j) { + return new SFunction[]{a, b, c, d, e, f, g, h, j}; + } + + } } diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java index 9d85889..ab1b084 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectLabel.java @@ -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; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java index 5c0e1a6..ac1644e 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectNormal.java @@ -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; diff --git a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java index e76f102..99855ea 100644 --- a/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java +++ b/mybatis-plus-join-core/src/main/java/com/github/yulichang/wrapper/segments/SelectString.java @@ -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;