feat: (破坏性更新)统一使用数据库字段名作为 字符串 参数的输入,关闭 https://gitee.com/mybatis-flex/mybatis-flex/issues/I9Q08A

This commit is contained in:
Suomm 2024-10-03 16:12:07 +08:00
parent 3a2bfcb746
commit 6cfcd2f599
3 changed files with 133 additions and 36 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,9 +22,20 @@ import com.mybatisflex.core.constant.SqlOperator;
import com.mybatisflex.core.dialect.DialectFactory; import com.mybatisflex.core.dialect.DialectFactory;
import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfo;
import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.table.TableInfoFactory;
import com.mybatisflex.core.util.*; import com.mybatisflex.core.util.ArrayUtil;
import com.mybatisflex.core.util.ClassUtil;
import com.mybatisflex.core.util.CollectionUtil;
import com.mybatisflex.core.util.LambdaGetter;
import com.mybatisflex.core.util.LambdaUtil;
import com.mybatisflex.core.util.SqlUtil;
import com.mybatisflex.core.util.StringUtil;
import java.util.*; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.BooleanSupplier; import java.util.function.BooleanSupplier;
import java.util.function.Consumer; import java.util.function.Consumer;
import java.util.function.Predicate; import java.util.function.Predicate;
@ -371,6 +382,11 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
operators = SqlOperators.empty(); operators = SqlOperators.empty();
} }
if (mapConditions != null) { if (mapConditions != null) {
QueryTable table = null;
// 默认就是第一个表可能为 null
if (CollectionUtil.isNotEmpty(queryTables)) {
table = queryTables.get(0);
}
QueryCondition condition = null; QueryCondition condition = null;
for (Map.Entry<String, Object> entry : mapConditions.entrySet()) { for (Map.Entry<String, Object> entry : mapConditions.entrySet()) {
SqlOperator operator = operators.get(entry.getKey()); SqlOperator operator = operators.get(entry.getKey());
@ -387,7 +403,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
} else if (operator == SqlOperator.LIKE_RIGHT || operator == SqlOperator.NOT_LIKE_RIGHT) { } else if (operator == SqlOperator.LIKE_RIGHT || operator == SqlOperator.NOT_LIKE_RIGHT) {
value = "%" + value; value = "%" + value;
} }
QueryCondition cond = QueryCondition.create(new QueryColumn(entry.getKey()), operator, value); QueryCondition cond = QueryCondition.create(new QueryColumn(table, entry.getKey()), operator, value);
if (condition == null) { if (condition == null) {
condition = cond; condition = cond;
} else { } else {

View File

@ -1,5 +1,5 @@
/* /*
* Copyright (c) 2022-2025, Mybatis-Flex (fuhai999@gmail.com). * Copyright (c) 2022-2024, Mybatis-Flex (fuhai999@gmail.com).
* <p> * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -22,62 +22,143 @@ import com.mybatisflex.core.util.LambdaUtil;
import java.util.HashMap; import java.util.HashMap;
/** /**
* <p>SQL 操作符集合用于为多个字段分别设置操作符
*
* <p>该类继承自 {@link HashMap}其中键是<strong>数据库字段的名称</strong>
* 值是对应的 {@link SqlOperator} 枚举实例
*
* @author michael * @author michael
* @author 王帅
* @see SqlOperator
*/ */
public class SqlOperators extends HashMap<String, SqlOperator> { public class SqlOperators extends HashMap<String, SqlOperator> {
/**
* 一个空的实例用于表示没有操作符的情况
*/
private static final SqlOperators EMPTY = new SqlOperators() { private static final SqlOperators EMPTY = new SqlOperators() {
@Override @Override
public SqlOperator put(String key, SqlOperator value) { public SqlOperator put(String key, SqlOperator value) {
throw new IllegalArgumentException("Can not set SqlOperator for \"empty\" SqlOperators"); throw new IllegalArgumentException("不能为 \"empty\" SqlOperators 设置 SqlOperator");
} }
}; };
public static SqlOperators empty() { /**
return EMPTY; * 默认构造函数
} * 创建一个空的 {@link SqlOperators} 实例
*/
public static SqlOperators of() {
return new SqlOperators();
}
public static <T> SqlOperators of(LambdaGetter<T> getter, SqlOperator operator) {
SqlOperators map = new SqlOperators(1);
map.put(LambdaUtil.getFieldName(getter), operator);
return map;
}
public static <T> SqlOperators of(String fieldName, SqlOperator operator) {
SqlOperators map = new SqlOperators(1);
map.put(fieldName, operator);
return map;
}
public SqlOperators() { public SqlOperators() {
} }
/**
* <p>带初始容量的构造函数
*
* <p>创建一个具有指定初始容量的 {@link SqlOperators} 实例
*
* @param initialCapacity 初始容量
*/
public SqlOperators(int initialCapacity) { public SqlOperators(int initialCapacity) {
super(initialCapacity); super(initialCapacity);
} }
/**
* <p>复制构造函数
*
* <p>创建一个包含指定 {@link SqlOperators} 实例所有元素的新的 {@link SqlOperators} 实例
*
* @param sqlOperators 要复制的 {@link SqlOperators} 实例
*/
public SqlOperators(SqlOperators sqlOperators) { public SqlOperators(SqlOperators sqlOperators) {
this.putAll(sqlOperators); this.putAll(sqlOperators);
} }
/**
* <p>获取一个空的 {@link SqlOperators} 实例
*
* <p><strong>注意空实例不允许向其中添加任何操作符</strong>
*
* @return 一个空的不可操作的 {@link SqlOperators} 实例
*/
public static SqlOperators empty() {
return EMPTY;
}
public <T> SqlOperators set(LambdaGetter<T> getter, SqlOperator operator) { /**
this.put(LambdaUtil.getFieldName(getter), operator); * 创建一个新的 {@link SqlOperators} 实例
return this; *
} * @return 一个新的可操作的 {@link SqlOperators} 实例
*/
public SqlOperators set(String fieldName, SqlOperator operator) { public static SqlOperators of() {
this.put(fieldName, operator); return new SqlOperators();
}
/**
* 使用给定数据库的字段名称和操作符创建一个新的 {@link SqlOperators} 实例
*
* @param columnName 数据库的字段名称
* @param operator 对应的字段操作符
* @return 包含指定字段和操作符的 {@link SqlOperators} 实例
*/
public static SqlOperators of(String columnName, SqlOperator operator) {
return new SqlOperators(1).set(columnName, operator);
}
/**
* 使用给定的查询列{@link QueryColumn}和操作符创建一个新的 {@link SqlOperators} 实例
*
* @param column 查询列
* @param operator 对应的字段操作符
* @return 包含指定字段和操作符的 {@link SqlOperators} 实例
*/
public static SqlOperators of(QueryColumn column, SqlOperator operator) {
return new SqlOperators(1).set(column, operator);
}
/**
* 使用给定的 Lambda 表达式和操作符创建一个新的 {@link SqlOperators} 实例
*
* @param getter Lambda 表达式
* @param operator 对应的字段操作符
* @param <T> 实体类的类型
* @return 包含指定字段和操作符的 {@link SqlOperators} 实例
*/
public static <T> SqlOperators of(LambdaGetter<T> getter, SqlOperator operator) {
return new SqlOperators(1).set(getter, operator);
}
/**
* 设置数据库的字段名称以及对应的操作符
*
* @param columnName 字段名称
* @param operator 字段操作符
* @return 当前 {@link SqlOperators} 实例以便进行链式调用
*/
public SqlOperators set(String columnName, SqlOperator operator) {
this.put(columnName, operator);
return this; return this;
} }
/**
* 设置查询列{@link QueryColumn}对应数据库字段的操作符
*
* @param column 查询列
* @param operator 操作符
* @return 当前 {@link SqlOperators} 实例以便进行链式调用
*/
public SqlOperators set(QueryColumn column, SqlOperator operator) { public SqlOperators set(QueryColumn column, SqlOperator operator) {
this.put(column.getName(), operator); return set(column.getName(), operator);
return this; }
/**
* 设置 Lambda 表达式对应数据库字段的操作符
*
* @param getter Lambda 表达式
* @param operator 对应的操作符
* @param <T> 实体类的类型
* @return 当前 {@link SqlOperators} 实例以便进行链式调用
*/
public <T> SqlOperators set(LambdaGetter<T> getter, SqlOperator operator) {
return set(LambdaUtil.getQueryColumn(getter), operator);
} }
} }

View File

@ -1012,7 +1012,7 @@ public class TableInfo {
.findFirst() .findFirst()
.orElse(QueryMethods.column(getTableNameWithSchema(), column)); .orElse(QueryMethods.column(getTableNameWithSchema(), column));
if (operators != null) { if (operators != null) {
SqlOperator operator = operators.get(property); SqlOperator operator = operators.get(column);
if (operator == null) { if (operator == null) {
operator = SqlOperator.EQUALS; operator = SqlOperator.EQUALS;
} else if (operator == SqlOperator.IGNORE) { } else if (operator == SqlOperator.IGNORE) {