From 7a13d28a67d6ef5d93ccc38f9caba4657df3608e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 26 Aug 2023 15:43:54 +0800 Subject: [PATCH 1/4] feat: add QueryWrapper.create(entity) --- .../mybatisflex/core/query/QueryWrapper.java | 25 +++++++++++++------ .../com/mybatisflex/core/table/TableInfo.java | 10 ++++++-- 2 files changed, 26 insertions(+), 9 deletions(-) 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 1718a64e..3ca3d6a2 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 @@ -37,13 +37,26 @@ public class QueryWrapper extends BaseQueryWrapper { * 根据实体类对象,构建查询条件 * * @param entity 实体类对象 - * @return 查询对象 + * @return 查询对象 QueryWrapper */ public static QueryWrapper create(Object entity) { TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass())); - return tableInfo.buildQueryWrapper(entity); + return tableInfo.buildQueryWrapper(entity, null); } + /** + * 根据实体类构建查询条件 + * + * @param entity 实体类对象 + * @param operators 每个属性对应的操作符 + * @return 查询对象 QueryWrapper + */ + public static QueryWrapper create(Object entity, Map operators) { + TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass())); + return tableInfo.buildQueryWrapper(entity, operators); + } + + @SuppressWarnings("unchecked") public WithBuilder with(String name) { if (with == null) { @@ -657,16 +670,16 @@ public class QueryWrapper extends BaseQueryWrapper { public QueryWrapper limit(Number rows) { if (rows != null) { setLimitRows(rows.longValue()); - }else { + } else { setLimitRows(null); } return this; } public QueryWrapper offset(Number offset) { - if (offset!= null) { + if (offset != null) { setLimitOffset(offset.longValue()); - }else { + } else { setLimitOffset(null); } return this; @@ -784,7 +797,6 @@ public class QueryWrapper extends BaseQueryWrapper { } - /** * 获取 queryWrapper 的参数 * 在构建 sql 的时候,需要保证 where 在 having 的前面 @@ -818,7 +830,6 @@ public class QueryWrapper extends BaseQueryWrapper { } - /** * 获取 queryWrapper 的参数 * 在构建 sql 的时候,需要保证 where 在 having 的前面 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 0c14fd53..ab7e76ed 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 @@ -19,6 +19,7 @@ import com.mybatisflex.annotation.*; import com.mybatisflex.core.FlexConsts; import com.mybatisflex.core.FlexGlobalConfig; import com.mybatisflex.core.constant.SqlConsts; +import com.mybatisflex.core.constant.SqlOperator; import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.exception.locale.LocalizedFormats; @@ -922,7 +923,7 @@ public class TableInfo { } - public QueryWrapper buildQueryWrapper(Object entity) { + public QueryWrapper buildQueryWrapper(Object entity, Map operators) { QueryColumn[] queryColumns = new QueryColumn[defaultQueryColumns.length]; for (int i = 0; i < defaultQueryColumns.length; i++) { queryColumns[i] = columnQueryMapping.get(defaultQueryColumns[i]); @@ -941,7 +942,12 @@ public class TableInfo { Object value = metaObject.getValue(property); if (value != null && !"".equals(value)) { QueryColumn queryColumn = buildQueryColumn(column); - queryWrapper.and(queryColumn.eq(value)); + if (operators != null && operators.containsKey(property)) { + SqlOperator operator = operators.get(property); + queryWrapper.and(QueryCondition.create(queryColumn, operator, value)); + } else { + queryWrapper.and(queryColumn.eq(value)); + } } }); return queryWrapper; From 6290b75e7d51e032e20694d6c8f61c0cfbed7e7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 26 Aug 2023 15:49:43 +0800 Subject: [PATCH 2/4] refactor: change SqlConnector package --- .../com/mybatisflex/core/activerecord/query/QueryModel.java | 1 + .../com/mybatisflex/core/activerecord/query/WhereBuilder.java | 1 + .../com/mybatisflex/core/{query => constant}/SqlConnector.java | 3 +-- .../main/java/com/mybatisflex/core/query/BaseQueryWrapper.java | 1 + .../src/main/java/com/mybatisflex/core/query/Brackets.java | 1 + .../src/main/java/com/mybatisflex/core/query/CPI.java | 1 + .../main/java/com/mybatisflex/core/query/QueryCondition.java | 2 ++ .../java/com/mybatisflex/core/query/QueryConditionBuilder.java | 1 + .../src/main/java/com/mybatisflex/core/query/QueryWrapper.java | 1 + .../java/com/mybatisflex/core/query/QueryWrapperAdapter.java | 1 + .../java/com/mybatisflex/coretest/DynamicConditionTest.java | 1 + 11 files changed, 12 insertions(+), 2 deletions(-) rename mybatis-flex-core/src/main/java/com/mybatisflex/core/{query => constant}/SqlConnector.java (96%) diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/QueryModel.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/QueryModel.java index ad7484aa..ad5344f4 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/QueryModel.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/QueryModel.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.activerecord.query; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.constant.SqlConsts; import com.mybatisflex.core.query.*; import com.mybatisflex.core.table.TableDef; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/WhereBuilder.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/WhereBuilder.java index d504d305..1d4b174f 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/WhereBuilder.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/activerecord/query/WhereBuilder.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.activerecord.query; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.query.*; import com.mybatisflex.core.util.LambdaGetter; import com.mybatisflex.core.util.LambdaUtil; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/SqlConnector.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConnector.java similarity index 96% rename from mybatis-flex-core/src/main/java/com/mybatisflex/core/query/SqlConnector.java rename to mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConnector.java index 93f909bb..1ba1fabf 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/SqlConnector.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/constant/SqlConnector.java @@ -13,12 +13,11 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -package com.mybatisflex.core.query; +package com.mybatisflex.core.constant; /** * @author michael */ - public enum SqlConnector { diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java index 62b18f86..46a0b03b 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/BaseQueryWrapper.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.query; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.exception.FlexExceptions; import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.ObjectUtil; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Brackets.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Brackets.java index 6f102929..e99873e3 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Brackets.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/Brackets.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.query; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.util.ObjectUtil; import com.mybatisflex.core.util.StringUtil; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java index 93c8cb8f..f58dab3a 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/CPI.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.query; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.dialect.IDialect; import com.mybatisflex.core.util.CollectionUtil; import com.mybatisflex.core.util.StringUtil; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java index a273bc51..1f4862b1 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryCondition.java @@ -16,6 +16,7 @@ package com.mybatisflex.core.query; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.constant.SqlConsts; import com.mybatisflex.core.constant.SqlOperator; import com.mybatisflex.core.dialect.IDialect; @@ -38,6 +39,7 @@ public class QueryCondition implements CloneSupport { //当前条件的上一个条件 protected QueryCondition prev; + //当前条件的下一个条件 protected QueryCondition next; diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryConditionBuilder.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryConditionBuilder.java index d00e6b2e..60f039ce 100644 --- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryConditionBuilder.java +++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryConditionBuilder.java @@ -15,6 +15,7 @@ */ package com.mybatisflex.core.query; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.util.LambdaGetter; import com.mybatisflex.core.util.LambdaUtil; 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 3ca3d6a2..e773d01f 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 @@ -16,6 +16,7 @@ package com.mybatisflex.core.query; import com.mybatisflex.core.FlexConsts; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.constant.SqlConsts; import com.mybatisflex.core.constant.SqlOperator; import com.mybatisflex.core.dialect.DialectFactory; 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 d1576cbc..6f7991df 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 @@ -15,6 +15,7 @@ */ 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; diff --git a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java index 4abfd151..b2b33c95 100644 --- a/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java +++ b/mybatis-flex-core/src/test/java/com/mybatisflex/coretest/DynamicConditionTest.java @@ -16,6 +16,7 @@ package com.mybatisflex.coretest; +import com.mybatisflex.core.constant.SqlConnector; import com.mybatisflex.core.query.*; import com.mybatisflex.core.util.StringUtil; import org.junit.Test; From bcc0907c01b5940ec54f01808e564d3939b4f8c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 26 Aug 2023 16:53:33 +0800 Subject: [PATCH 3/4] feat: add SqlOperators --- docs/zh/base/querywrapper.md | 49 +++++++++++++ .../mybatisflex/core/query/QueryWrapper.java | 20 +++--- .../core/query/QueryWrapperAdapter.java | 11 ++- .../mybatisflex/core/query/SqlOperators.java | 71 +++++++++++++++++++ .../com/mybatisflex/core/table/TableInfo.java | 3 + .../coretest/AccountSqlTester.java | 29 +++++++- 6 files changed, 166 insertions(+), 17 deletions(-) create mode 100644 mybatis-flex-core/src/main/java/com/mybatisflex/core/query/SqlOperators.java 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()); + } } From 184b655907a7b17841180ac4f948c208e0ef43eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=80=E6=BA=90=E6=B5=B7=E5=93=A5?= Date: Sat, 26 Aug 2023 17:00:09 +0800 Subject: [PATCH 4/4] feat: add SqlOperators --- .../src/main/java/com/mybatisflex/core/table/TableInfo.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 0e4616c6..f7604614 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 @@ -923,7 +923,7 @@ public class TableInfo { } - public QueryWrapper buildQueryWrapper(Object entity, Map operators) { + public QueryWrapper buildQueryWrapper(Object entity, SqlOperators operators) { QueryColumn[] queryColumns = new QueryColumn[defaultQueryColumns.length]; for (int i = 0; i < defaultQueryColumns.length; i++) { queryColumns[i] = columnQueryMapping.get(defaultQueryColumns[i]);