diff --git a/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java b/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java index 933de31..c6ca48b 100644 --- a/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java +++ b/src/main/java/com/github/yulichang/method/MPJAbstractMethod.java @@ -13,22 +13,14 @@ import java.util.List; * @author yulichang * @see AbstractMethod */ -public abstract class MPJAbstractMethod extends AbstractMethod { +public abstract class MPJAbstractMethod extends AbstractMethod implements MPJBaseMethod { /** * 连表操作不考虑entity查询和逻辑删除 */ @Override protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) { - String 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); - return newLine ? NEWLINE + sqlScript : sqlScript; + return whereEntityWrapper(newLine); } @Override diff --git a/src/main/java/com/github/yulichang/method/MPJBaseMethod.java b/src/main/java/com/github/yulichang/method/MPJBaseMethod.java new file mode 100644 index 0000000..c1b1b06 --- /dev/null +++ b/src/main/java/com/github/yulichang/method/MPJBaseMethod.java @@ -0,0 +1,26 @@ +package com.github.yulichang.method; + +import com.baomidou.mybatisplus.core.toolkit.Constants; +import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; + +/** + * 连表sql条件 + * + * @author yulichang + * @since 1.2.0 + */ +public interface MPJBaseMethod extends Constants { + + default String whereEntityWrapper(boolean newLine) { + String 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); + return newLine ? NEWLINE + sqlScript : sqlScript; + } + +} diff --git a/src/main/java/com/github/yulichang/method/mp/TableAlias.java b/src/main/java/com/github/yulichang/method/mp/TableAlias.java index 4f2925b..557880c 100644 --- a/src/main/java/com/github/yulichang/method/mp/TableAlias.java +++ b/src/main/java/com/github/yulichang/method/mp/TableAlias.java @@ -3,6 +3,7 @@ package com.github.yulichang.method.mp; import com.baomidou.mybatisplus.core.metadata.TableInfo; import com.baomidou.mybatisplus.core.toolkit.Constants; import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils; +import com.github.yulichang.method.MPJBaseMethod; import com.github.yulichang.toolkit.Constant; /** @@ -11,7 +12,7 @@ import com.github.yulichang.toolkit.Constant; * @author yulichang * @since 1.2.0 */ -public interface TableAlias extends Constants { +public interface TableAlias extends Constants, MPJBaseMethod { default String getTableName(TableInfo tableInfo) { return tableInfo.getTableName() + SPACE + SqlScriptUtils.convertIf("${ew.alias}", @@ -38,15 +39,7 @@ public interface TableAlias 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); - 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); - return newLine ? NEWLINE + sqlScript : sqlScript; + return whereEntityWrapper(newLine); } }