diff --git a/docs/zh/base/querywrapper.md b/docs/zh/base/querywrapper.md index 3f892429..8118fef8 100644 --- a/docs/zh/base/querywrapper.md +++ b/docs/zh/base/querywrapper.md @@ -938,6 +938,55 @@ WHERE `a`.`id` >= 100 AND ``` +## Entity 转化为 QueryWrapper + +QueryWrapper 提供了 `create()` 方法帮助用户把 entity 转化为 QueryWrapper。 + +简单示例: + +```java +Account account = new Account(); +account.setAge(18); +account.setUserName("michael"); + +QueryWrapper qw = QueryWrapper.create(account); +System.out.println(qw.toSQL()); +``` + +输出的 SQL 内容如下: + +```sql +SELECT `id`, `user_name`, `birthday`, `sex`, `age`, `is_normal` +FROM `tb_account` +WHERE `user_name` = 'michael' `age` = 18 +``` + +自定义 Entity 字段的 SQL 操作符示例: + +```java +Account account = new Account(); +account.setAge(18); +account.setUserName("michael"); + +SqlOperators operators = SqlOperators.of() + .set(Account::getUserName, SqlOperator.LIKE) + .set(Account::getAge, SqlOperator.GE); + +QueryWrapper qw = QueryWrapper.create(account, operators); +System.out.println(qw.toSQL()); +``` + +输出的 SQL 内容如下: + +```sql +SELECT `id`, `user_name`, `birthday`, `sex`, `age`, `is_normal` +FROM `tb_account` +WHERE `user_name` LIKE '%michael%' AND `age` >= 18 +``` + + + + ## QueryWrapper 序列化 在 `QueryWrapper` 中,由于其定义了 `循环引用` 的一些数据结构,同时,其很多属性都是 `private` 或者 `protected` 修饰且没有 `getter` `setter` 方法, diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java index e773d01f..07c689fa 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapper.java @@ -52,7 +52,7 @@ public class QueryWrapper extends BaseQueryWrapper { * @param operators 每个属性对应的操作符 * @return 查询对象 QueryWrapper */ - public static QueryWrapper create(Object entity, Map operators) { + public static QueryWrapper create(Object entity, SqlOperators operators) { TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass())); return tableInfo.buildQueryWrapper(entity, operators); } @@ -221,7 +221,7 @@ public class QueryWrapper extends BaseQueryWrapper { return and(whereConditions); } - public QueryWrapper where(Map whereConditions, Map operators) { + public QueryWrapper where(Map whereConditions, SqlOperators operators) { return and(whereConditions, operators); } @@ -270,14 +270,14 @@ public class QueryWrapper extends BaseQueryWrapper { public QueryWrapper and(Map whereConditions) { - return and(whereConditions, Collections.emptyMap()); + return and(whereConditions, SqlOperators.empty()); } - public QueryWrapper and(Map whereConditions, Map operators) { + public QueryWrapper and(Map whereConditions, SqlOperators operators) { return connectMap(whereConditions, operators, SqlConnector.AND, SqlConnector.AND); } - public QueryWrapper and(Map whereConditions, Map operators, SqlConnector innerConnector) { + public QueryWrapper and(Map whereConditions, SqlOperators operators, SqlConnector innerConnector) { return connectMap(whereConditions, operators, SqlConnector.AND, innerConnector); } @@ -319,20 +319,20 @@ public class QueryWrapper extends BaseQueryWrapper { public QueryWrapper or(Map whereConditions) { - return or(whereConditions, Collections.emptyMap()); + return or(whereConditions, SqlOperators.empty()); } - public QueryWrapper or(Map whereConditions, Map operators) { + public QueryWrapper or(Map whereConditions, SqlOperators operators) { return connectMap(whereConditions, operators, SqlConnector.OR, SqlConnector.AND); } - public QueryWrapper or(Map whereConditions, Map operators, SqlConnector innerConnector) { + public QueryWrapper or(Map whereConditions, SqlOperators operators, SqlConnector innerConnector) { return connectMap(whereConditions, operators, SqlConnector.OR, SqlConnector.AND); } - protected QueryWrapper connectMap(Map mapConditions, Map operators, SqlConnector outerConnector, SqlConnector innerConnector) { + protected QueryWrapper connectMap(Map mapConditions, SqlOperators operators, SqlConnector outerConnector, SqlConnector innerConnector) { if (operators == null) { - operators = Collections.emptyMap(); + operators = SqlOperators.empty(); } if (mapConditions != null) { QueryCondition condition = null; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java index 6f7991df..9ca212f9 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryWrapperAdapter.java @@ -16,7 +16,6 @@ package com.mybatisflex.core.query; import com.mybatisflex.core.constant.SqlConnector; -import com.mybatisflex.core.constant.SqlOperator; import com.mybatisflex.core.table.TableDef; import com.mybatisflex.core.util.LambdaGetter; import com.mybatisflex.core.util.LambdaUtil; @@ -151,7 +150,7 @@ public class QueryWrapperAdapter> extends Query } @Override - public R where(Map whereConditions, Map operators) { + public R where(Map whereConditions, SqlOperators operators) { super.where(whereConditions, operators); return (R) this; } @@ -202,13 +201,13 @@ public class QueryWrapperAdapter> extends Query } @Override - public R and(Map whereConditions, Map operators) { + public R and(Map whereConditions, SqlOperators operators) { super.and(whereConditions, operators); return (R) this; } @Override - public R and(Map whereConditions, Map operators, SqlConnector innerConnector) { + public R and(Map whereConditions, SqlOperators operators, SqlConnector innerConnector) { super.and(whereConditions, operators, innerConnector); return (R) this; } @@ -249,13 +248,13 @@ public class QueryWrapperAdapter> extends Query } @Override - public R or(Map whereConditions, Map operators) { + public R or(Map whereConditions, SqlOperators operators) { super.or(whereConditions, operators); return (R) this; } @Override - public R or(Map whereConditions, Map operators, SqlConnector innerConnector) { + public R or(Map whereConditions, SqlOperators operators, SqlConnector innerConnector) { super.or(whereConditions, operators, innerConnector); return (R) this; } diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/SqlOperators.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/SqlOperators.java new file mode 100644 index 00000000..fbb8fe00 --- /dev/null +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/SqlOperators.java @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com). + *

+ * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.mybatisflex.core.query; + +import com.mybatisflex.core.constant.SqlOperator; +import com.mybatisflex.core.util.LambdaGetter; +import com.mybatisflex.core.util.LambdaUtil; + +import java.util.HashMap; + +/** + * @author michael + */ +public class SqlOperators extends HashMap { + + private final static SqlOperators EMPTY = new SqlOperators(); + + static SqlOperators empty() { + return EMPTY; + } + + public static SqlOperators of() { + return new SqlOperators(); + } + + public static SqlOperators of( + LambdaGetter getter, SqlOperator operator + ) { + SqlOperators map = new SqlOperators(1); + map.put(LambdaUtil.getFieldName(getter), operator); + return map; + } + + public static SqlOperators of( + String fieldName, SqlOperator operator + ) { + SqlOperators map = new SqlOperators(1); + map.put(fieldName, operator); + return map; + } + + public SqlOperators() { + } + + public SqlOperators(int initialCapacity) { + super(initialCapacity); + } + + public SqlOperators set(LambdaGetter getter, SqlOperator operator) { + this.put(LambdaUtil.getFieldName(getter), operator); + return this; + } + + public SqlOperators set(String fieldName, SqlOperator operator) { + this.put(fieldName, operator); + return this; + } +} diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java index ab7e76ed..0e4616c6 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/table/TableInfo.java @@ -944,6 +944,9 @@ public class TableInfo { QueryColumn queryColumn = buildQueryColumn(column); if (operators != null && operators.containsKey(property)) { SqlOperator operator = operators.get(property); + if (operator == SqlOperator.LIKE || operator == SqlOperator.NOT_LIKE) { + value = "%" + value + "%"; + } queryWrapper.and(QueryCondition.create(queryColumn, operator, value)); } else { queryWrapper.and(queryColumn.eq(value)); diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java index 564d30bf..ff8bbb35 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/AccountSqlTester.java @@ -16,6 +16,7 @@ package com.mybatisflex.coretest; +import com.mybatisflex.core.constant.SqlOperator; import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.dialect.KeywordWrap; import com.mybatisflex.core.dialect.LimitOffsetProcessor; @@ -26,6 +27,7 @@ import com.mybatisflex.core.table.DynamicTableProcessor; import com.mybatisflex.core.table.TableInfo; import com.mybatisflex.core.table.TableInfoFactory; import com.mybatisflex.core.table.TableManager; +import com.mybatisflex.core.query.SqlOperators; import org.junit.Test; import java.util.Arrays; @@ -201,7 +203,6 @@ public class AccountSqlTester { } - @Test public void testWhereSql3() { QueryWrapper queryWrapper = QueryWrapper.create() @@ -575,4 +576,30 @@ public class AccountSqlTester { // System.out.println(">>>>> informix: " + informixSql); } + + @Test + public void testEntityToQueryWrapper1() { + Account account = new Account(); + account.setAge(18); + account.setUserName("michael"); + + QueryWrapper qw = QueryWrapper.create(account); + System.out.println(qw.toSQL()); + } + + + @Test + public void testEntityToQueryWrapper2() { + Account account = new Account(); + account.setAge(18); + account.setUserName("michael"); + + SqlOperators operators = SqlOperators.of() + .set(Account::getUserName, SqlOperator.LIKE) + .set(Account::getAge, SqlOperator.GE); + + QueryWrapper qw = QueryWrapper.create(account, operators); + + System.out.println(qw.toSQL()); + } }