diff --git a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java index f82968d..5e77b52 100644 --- a/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java +++ b/src/main/java/com/github/yulichang/interceptor/MPJInterceptor.java @@ -9,7 +9,7 @@ import com.github.yulichang.method.MPJResultType; import com.github.yulichang.toolkit.Constant; import com.github.yulichang.toolkit.ReflectionKit; import com.github.yulichang.wrapper.MPJLambdaWrapper; -import com.github.yulichang.wrapper.SelectColumn; +import com.github.yulichang.toolkit.support.SelectColumn; import org.apache.ibatis.executor.Executor; import org.apache.ibatis.logging.Log; import org.apache.ibatis.logging.LogFactory; diff --git a/src/main/java/com/github/yulichang/method/MPJBaseMethod.java b/src/main/java/com/github/yulichang/method/MPJBaseMethod.java index 39dd1fb..dae42c6 100644 --- a/src/main/java/com/github/yulichang/method/MPJBaseMethod.java +++ b/src/main/java/com/github/yulichang/method/MPJBaseMethod.java @@ -1,8 +1,16 @@ package com.github.yulichang.method; +import com.baomidou.mybatisplus.annotation.FieldStrategy; +import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.metadata.TableInfo; 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.toolkit.Constant; + +import java.util.Objects; + +import static java.util.stream.Collectors.joining; /** * 连表sql条件 @@ -14,7 +22,10 @@ public interface MPJBaseMethod extends Constants { default String mpjSqlWhereEntityWrapper(boolean newLine, TableInfo table) { if (table.isWithLogicDelete()) { - String sqlScript = (NEWLINE + getLogicDeleteSql(table, true, true) + NEWLINE); + String sqlScript = getAllSqlWhere(table, true, true, WRAPPER_ENTITY_DOT); + sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true); + sqlScript += NEWLINE; + sqlScript += (NEWLINE + getLogicDeleteSql(table, true, true) + NEWLINE); String normalSqlScript = SqlScriptUtils.convertIf(String.format("AND ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_NONEMPTYOFNORMAL), true); normalSqlScript += NEWLINE; normalSqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_EMPTYOFNORMAL), true); @@ -23,7 +34,10 @@ public interface MPJBaseMethod extends Constants { sqlScript = SqlScriptUtils.convertWhere(sqlScript); return newLine ? NEWLINE + sqlScript : sqlScript; } else { - String sqlScript = SqlScriptUtils.convertIf(String.format("${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_NONEMPTYOFWHERE), true); + String sqlScript = getAllSqlWhere(table, false, true, WRAPPER_ENTITY_DOT); + sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER_ENTITY), true); + sqlScript += NEWLINE; + sqlScript += SqlScriptUtils.convertIf(String.format("${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_NONEMPTYOFWHERE), true); sqlScript = SqlScriptUtils.convertWhere(sqlScript) + NEWLINE; sqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_EMPTYOFWHERE), true); sqlScript = SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", WRAPPER), true); @@ -32,6 +46,55 @@ public interface MPJBaseMethod extends Constants { } + /** + * 拷贝 tableInfo 里面的 getAllSqlWhere方法 + */ + default String getAllSqlWhere(TableInfo tableInfo, boolean ignoreLogicDelFiled, boolean withId, final String prefix) { + final String newPrefix = prefix == null ? EMPTY : prefix; + String filedSqlScript = tableInfo.getFieldList().stream() + .filter(i -> { + if (ignoreLogicDelFiled) { + return !(tableInfo.isWithLogicDelete() && i.isLogicDelete()); + } + return true; + }) + .map(i -> getSqlWhere(i, newPrefix)).filter(Objects::nonNull).collect(joining(NEWLINE)); + if (!withId || StringUtils.isBlank(tableInfo.getKeyProperty())) { + return filedSqlScript; + } + String newKeyProperty = newPrefix + tableInfo.getKeyProperty(); + String keySqlScript = Constant.TABLE_ALIAS + DOT + tableInfo.getKeyColumn() + EQUALS + SqlScriptUtils.safeParam(newKeyProperty); + return SqlScriptUtils.convertIf(keySqlScript, String.format("%s != null", newKeyProperty), false) + + NEWLINE + filedSqlScript; + } + + 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()); + // 查询的时候只判非空 + return convertIf(tableFieldInfo, sqlScript, convertIfProperty(newPrefix, tableFieldInfo.getProperty()), tableFieldInfo.getWhereStrategy()); + } + + default String convertIf(TableFieldInfo tableFieldInfo, final String sqlScript, final String property, final FieldStrategy fieldStrategy) { + if (fieldStrategy == FieldStrategy.NEVER) { + return null; + } + if (tableFieldInfo.isPrimitive() || fieldStrategy == FieldStrategy.IGNORED) { + return sqlScript; + } + if (fieldStrategy == FieldStrategy.NOT_EMPTY && tableFieldInfo.isCharSequence()) { + return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null and %s != ''", property, property), + false); + } + return SqlScriptUtils.convertIf(sqlScript, String.format("%s != null", property), false); + } + + default String convertIfProperty(String prefix, String property) { + return StringUtils.isNotBlank(prefix) ? prefix.substring(0, prefix.length() - 1) + "['" + property + "']" : property; + } + + default String getLogicDeleteSql(TableInfo tableInfo, boolean startWithAnd, boolean isWhere) { if (tableInfo.isWithLogicDelete()) { String logicDeleteSql = formatLogicDeleteSql(tableInfo, isWhere); diff --git a/src/main/java/com/github/yulichang/wrapper/SelectColumn.java b/src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java similarity index 97% rename from src/main/java/com/github/yulichang/wrapper/SelectColumn.java rename to src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java index 74fb4dc..74498d4 100644 --- a/src/main/java/com/github/yulichang/wrapper/SelectColumn.java +++ b/src/main/java/com/github/yulichang/toolkit/support/SelectColumn.java @@ -1,4 +1,4 @@ -package com.github.yulichang.wrapper; +package com.github.yulichang.toolkit.support; import com.baomidou.mybatisplus.core.metadata.TableFieldInfo; import com.baomidou.mybatisplus.core.toolkit.StringPool; diff --git a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java index 690c1ba..923ee01 100644 --- a/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java +++ b/src/main/java/com/github/yulichang/wrapper/MPJLambdaWrapper.java @@ -12,6 +12,7 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils; import com.baomidou.mybatisplus.core.toolkit.support.SFunction; import com.github.yulichang.toolkit.*; import com.github.yulichang.toolkit.support.ColumnCache; +import com.github.yulichang.toolkit.support.SelectColumn; import com.github.yulichang.wrapper.enums.BaseFuncEnum; import com.github.yulichang.wrapper.interfaces.LambdaJoin; import com.github.yulichang.wrapper.interfaces.Query; diff --git a/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java b/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java index 623b66e..b15397f 100644 --- a/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java +++ b/src/main/java/com/github/yulichang/wrapper/interfaces/LambdaJoin.java @@ -115,6 +115,34 @@ public interface LambdaJoin extends MPJBaseJoin { return join(Constant.INNER_JOIN, condition, clazz, function); } + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, SFunction left, SFunction right) { + return fullJoin(true, clazz, left, right); + } + + /** + * ignore 参考 left join + */ + default Children fullJoin(Class clazz, OnFunction function) { + return fullJoin(true, clazz, function); + } + + /** + * ignore 参考 left join + */ + default Children fullJoin(boolean condition, Class clazz, SFunction left, SFunction right) { + return fullJoin(condition, clazz, on -> on.eq(left, right)); + } + + /** + * ignore 参考 left join + */ + default Children fullJoin(boolean condition, Class clazz, OnFunction function) { + return join(Constant.FULL_JOIN, condition, clazz, function); + } + /** * 查询基类 可以直接调用此方法实现以上所有功能 *