mirror of
https://gitee.com/best_handsome/mybatis-plus-join
synced 2025-07-11 00:02:22 +08:00
恢复对setEntity条件查询的支持
This commit is contained in:
parent
56e1d28d49
commit
758df4c136
@ -9,7 +9,7 @@ import com.github.yulichang.method.MPJResultType;
|
|||||||
import com.github.yulichang.toolkit.Constant;
|
import com.github.yulichang.toolkit.Constant;
|
||||||
import com.github.yulichang.toolkit.ReflectionKit;
|
import com.github.yulichang.toolkit.ReflectionKit;
|
||||||
import com.github.yulichang.wrapper.MPJLambdaWrapper;
|
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.executor.Executor;
|
||||||
import org.apache.ibatis.logging.Log;
|
import org.apache.ibatis.logging.Log;
|
||||||
import org.apache.ibatis.logging.LogFactory;
|
import org.apache.ibatis.logging.LogFactory;
|
||||||
|
@ -1,8 +1,16 @@
|
|||||||
package com.github.yulichang.method;
|
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.metadata.TableInfo;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
import com.baomidou.mybatisplus.core.toolkit.Constants;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.sql.SqlScriptUtils;
|
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条件
|
* 连表sql条件
|
||||||
@ -14,7 +22,10 @@ public interface MPJBaseMethod extends Constants {
|
|||||||
|
|
||||||
default String mpjSqlWhereEntityWrapper(boolean newLine, TableInfo table) {
|
default String mpjSqlWhereEntityWrapper(boolean newLine, TableInfo table) {
|
||||||
if (table.isWithLogicDelete()) {
|
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);
|
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 += NEWLINE;
|
||||||
normalSqlScript += SqlScriptUtils.convertIf(String.format(" ${%s}", WRAPPER_SQLSEGMENT), String.format("%s != null and %s != '' and %s", WRAPPER_SQLSEGMENT, WRAPPER_SQLSEGMENT, WRAPPER_EMPTYOFNORMAL), true);
|
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);
|
sqlScript = SqlScriptUtils.convertWhere(sqlScript);
|
||||||
return newLine ? NEWLINE + sqlScript : sqlScript;
|
return newLine ? NEWLINE + sqlScript : sqlScript;
|
||||||
} else {
|
} 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.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(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);
|
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) {
|
default String getLogicDeleteSql(TableInfo tableInfo, boolean startWithAnd, boolean isWhere) {
|
||||||
if (tableInfo.isWithLogicDelete()) {
|
if (tableInfo.isWithLogicDelete()) {
|
||||||
String logicDeleteSql = formatLogicDeleteSql(tableInfo, isWhere);
|
String logicDeleteSql = formatLogicDeleteSql(tableInfo, isWhere);
|
||||||
|
@ -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.metadata.TableFieldInfo;
|
||||||
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
@ -12,6 +12,7 @@ import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
|||||||
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
|
||||||
import com.github.yulichang.toolkit.*;
|
import com.github.yulichang.toolkit.*;
|
||||||
import com.github.yulichang.toolkit.support.ColumnCache;
|
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.enums.BaseFuncEnum;
|
||||||
import com.github.yulichang.wrapper.interfaces.LambdaJoin;
|
import com.github.yulichang.wrapper.interfaces.LambdaJoin;
|
||||||
import com.github.yulichang.wrapper.interfaces.Query;
|
import com.github.yulichang.wrapper.interfaces.Query;
|
||||||
|
@ -115,6 +115,34 @@ public interface LambdaJoin<Children, Entity> extends MPJBaseJoin<Entity> {
|
|||||||
return join(Constant.INNER_JOIN, condition, clazz, function);
|
return join(Constant.INNER_JOIN, condition, clazz, function);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore 参考 left join
|
||||||
|
*/
|
||||||
|
default <T, X> Children fullJoin(Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||||
|
return fullJoin(true, clazz, left, right);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore 参考 left join
|
||||||
|
*/
|
||||||
|
default <T> Children fullJoin(Class<T> clazz, OnFunction function) {
|
||||||
|
return fullJoin(true, clazz, function);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore 参考 left join
|
||||||
|
*/
|
||||||
|
default <T, X> Children fullJoin(boolean condition, Class<T> clazz, SFunction<T, ?> left, SFunction<X, ?> right) {
|
||||||
|
return fullJoin(condition, clazz, on -> on.eq(left, right));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* ignore 参考 left join
|
||||||
|
*/
|
||||||
|
default <T> Children fullJoin(boolean condition, Class<T> clazz, OnFunction function) {
|
||||||
|
return join(Constant.FULL_JOIN, condition, clazz, function);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 查询基类 可以直接调用此方法实现以上所有功能
|
* 查询基类 可以直接调用此方法实现以上所有功能
|
||||||
*
|
*
|
||||||
|
Loading…
x
Reference in New Issue
Block a user