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
eb58a2987f
commit
7efad696da
@ -20,7 +20,7 @@ public abstract class MPJAbstractMethod extends AbstractMethod implements MPJBas
|
||||
*/
|
||||
@Override
|
||||
protected String sqlWhereEntityWrapper(boolean newLine, TableInfo table) {
|
||||
return whereEntityWrapper(newLine);
|
||||
return mpjSqlWhereEntityWrapper(newLine, table);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -1,5 +1,6 @@
|
||||
package com.github.yulichang.method;
|
||||
|
||||
import com.baomidou.mybatisplus.core.metadata.TableInfo;
|
||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
|
||||
@ -11,16 +12,64 @@ import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
||||
*/
|
||||
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;
|
||||
default String mpjSqlWhereEntityWrapper(boolean newLine, TableInfo table) {
|
||||
if (table.isWithLogicDelete()) {
|
||||
String 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);
|
||||
sqlScript += normalSqlScript;
|
||||
sqlScript = SqlScriptUtils.convertChoose(String.format("%s != null", WRAPPER), sqlScript,
|
||||
table.getLogicDeleteSql(false, true));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
default String getLogicDeleteSql(TableInfo tableInfo, boolean startWithAnd, boolean isWhere) {
|
||||
if (tableInfo.isWithLogicDelete()) {
|
||||
String logicDeleteSql = formatLogicDeleteSql(tableInfo, isWhere);
|
||||
if (startWithAnd) {
|
||||
logicDeleteSql = " AND " + logicDeleteSql;
|
||||
}
|
||||
return logicDeleteSql;
|
||||
}
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
|
||||
default String formatLogicDeleteSql(TableInfo tableInfo, boolean isWhere) {
|
||||
final String value = isWhere ? tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue() :
|
||||
tableInfo.getLogicDeleteFieldInfo().getLogicDeleteValue();
|
||||
if (isWhere) {
|
||||
if (NULL.equalsIgnoreCase(value)) {
|
||||
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL";
|
||||
} else {
|
||||
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(
|
||||
tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
|
||||
}
|
||||
}
|
||||
final String targetStr = "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS;
|
||||
if (NULL.equalsIgnoreCase(value)) {
|
||||
return targetStr + NULL;
|
||||
} else {
|
||||
return targetStr + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -20,57 +20,5 @@ public interface TableAlias extends Constants, MPJBaseMethod {
|
||||
+ SPACE + SqlScriptUtils.convertIf("${ew.from}",
|
||||
String.format("%s != null and %s and %s != null and %s != ''", Constant.PARAM_TYPE, Constant.PARAM_TYPE,
|
||||
"ew.from", "ew.from"), false);
|
||||
|
||||
}
|
||||
|
||||
default String mpjSqlWhereEntityWrapper(boolean newLine, TableInfo table) {
|
||||
if (table.isWithLogicDelete()) {
|
||||
String 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);
|
||||
sqlScript += normalSqlScript;
|
||||
sqlScript = SqlScriptUtils.convertChoose(String.format("%s != null", WRAPPER), sqlScript,
|
||||
table.getLogicDeleteSql(false, true));
|
||||
sqlScript = SqlScriptUtils.convertWhere(sqlScript);
|
||||
return newLine ? NEWLINE + sqlScript : sqlScript;
|
||||
} else {
|
||||
return whereEntityWrapper(newLine);
|
||||
}
|
||||
}
|
||||
|
||||
default String getLogicDeleteSql(TableInfo tableInfo, boolean startWithAnd, boolean isWhere) {
|
||||
if (tableInfo.isWithLogicDelete()) {
|
||||
String logicDeleteSql = formatLogicDeleteSql(tableInfo, isWhere);
|
||||
if (startWithAnd) {
|
||||
logicDeleteSql = " AND " + logicDeleteSql;
|
||||
}
|
||||
return logicDeleteSql;
|
||||
}
|
||||
return EMPTY;
|
||||
}
|
||||
|
||||
|
||||
default String formatLogicDeleteSql(TableInfo tableInfo, boolean isWhere) {
|
||||
final String value = isWhere ? tableInfo.getLogicDeleteFieldInfo().getLogicNotDeleteValue() :
|
||||
tableInfo.getLogicDeleteFieldInfo().getLogicDeleteValue();
|
||||
if (isWhere) {
|
||||
if (NULL.equalsIgnoreCase(value)) {
|
||||
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + " IS NULL";
|
||||
} else {
|
||||
return "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS + String.format(
|
||||
tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
|
||||
}
|
||||
}
|
||||
final String targetStr = "${ew.alias}." + tableInfo.getLogicDeleteFieldInfo().getColumn() + EQUALS;
|
||||
if (NULL.equalsIgnoreCase(value)) {
|
||||
return targetStr + NULL;
|
||||
} else {
|
||||
return targetStr + String.format(tableInfo.getLogicDeleteFieldInfo().isCharSequence() ? "'%s'" : "%s", value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user