mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: add SqlOperators
This commit is contained in:
parent
6290b75e7d
commit
bcc0907c01
@ -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 序列化
|
||||||
|
|
||||||
在 `QueryWrapper` 中,由于其定义了 `循环引用` 的一些数据结构,同时,其很多属性都是 `private` 或者 `protected` 修饰且没有 `getter` `setter` 方法,
|
在 `QueryWrapper` 中,由于其定义了 `循环引用` 的一些数据结构,同时,其很多属性都是 `private` 或者 `protected` 修饰且没有 `getter` `setter` 方法,
|
||||||
|
|||||||
@ -52,7 +52,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
* @param operators 每个属性对应的操作符
|
* @param operators 每个属性对应的操作符
|
||||||
* @return 查询对象 QueryWrapper
|
* @return 查询对象 QueryWrapper
|
||||||
*/
|
*/
|
||||||
public static QueryWrapper create(Object entity, Map<String, SqlOperator> operators) {
|
public static QueryWrapper create(Object entity, SqlOperators operators) {
|
||||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass()));
|
TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass()));
|
||||||
return tableInfo.buildQueryWrapper(entity, operators);
|
return tableInfo.buildQueryWrapper(entity, operators);
|
||||||
}
|
}
|
||||||
@ -221,7 +221,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
return and(whereConditions);
|
return and(whereConditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryWrapper where(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
public QueryWrapper where(Map<String, Object> whereConditions, SqlOperators operators) {
|
||||||
return and(whereConditions, operators);
|
return and(whereConditions, operators);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -270,14 +270,14 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
|
|
||||||
|
|
||||||
public QueryWrapper and(Map<String, Object> whereConditions) {
|
public QueryWrapper and(Map<String, Object> whereConditions) {
|
||||||
return and(whereConditions, Collections.emptyMap());
|
return and(whereConditions, SqlOperators.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryWrapper and(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
public QueryWrapper and(Map<String, Object> whereConditions, SqlOperators operators) {
|
||||||
return connectMap(whereConditions, operators, SqlConnector.AND, SqlConnector.AND);
|
return connectMap(whereConditions, operators, SqlConnector.AND, SqlConnector.AND);
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryWrapper and(Map<String, Object> whereConditions, Map<String, SqlOperator> operators, SqlConnector innerConnector) {
|
public QueryWrapper and(Map<String, Object> whereConditions, SqlOperators operators, SqlConnector innerConnector) {
|
||||||
return connectMap(whereConditions, operators, SqlConnector.AND, innerConnector);
|
return connectMap(whereConditions, operators, SqlConnector.AND, innerConnector);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -319,20 +319,20 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
|||||||
|
|
||||||
|
|
||||||
public QueryWrapper or(Map<String, Object> whereConditions) {
|
public QueryWrapper or(Map<String, Object> whereConditions) {
|
||||||
return or(whereConditions, Collections.emptyMap());
|
return or(whereConditions, SqlOperators.empty());
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryWrapper or(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
public QueryWrapper or(Map<String, Object> whereConditions, SqlOperators operators) {
|
||||||
return connectMap(whereConditions, operators, SqlConnector.OR, SqlConnector.AND);
|
return connectMap(whereConditions, operators, SqlConnector.OR, SqlConnector.AND);
|
||||||
}
|
}
|
||||||
|
|
||||||
public QueryWrapper or(Map<String, Object> whereConditions, Map<String, SqlOperator> operators, SqlConnector innerConnector) {
|
public QueryWrapper or(Map<String, Object> whereConditions, SqlOperators operators, SqlConnector innerConnector) {
|
||||||
return connectMap(whereConditions, operators, SqlConnector.OR, SqlConnector.AND);
|
return connectMap(whereConditions, operators, SqlConnector.OR, SqlConnector.AND);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected QueryWrapper connectMap(Map<String, Object> mapConditions, Map<String, SqlOperator> operators, SqlConnector outerConnector, SqlConnector innerConnector) {
|
protected QueryWrapper connectMap(Map<String, Object> mapConditions, SqlOperators operators, SqlConnector outerConnector, SqlConnector innerConnector) {
|
||||||
if (operators == null) {
|
if (operators == null) {
|
||||||
operators = Collections.emptyMap();
|
operators = SqlOperators.empty();
|
||||||
}
|
}
|
||||||
if (mapConditions != null) {
|
if (mapConditions != null) {
|
||||||
QueryCondition condition = null;
|
QueryCondition condition = null;
|
||||||
|
|||||||
@ -16,7 +16,6 @@
|
|||||||
package com.mybatisflex.core.query;
|
package com.mybatisflex.core.query;
|
||||||
|
|
||||||
import com.mybatisflex.core.constant.SqlConnector;
|
import com.mybatisflex.core.constant.SqlConnector;
|
||||||
import com.mybatisflex.core.constant.SqlOperator;
|
|
||||||
import com.mybatisflex.core.table.TableDef;
|
import com.mybatisflex.core.table.TableDef;
|
||||||
import com.mybatisflex.core.util.LambdaGetter;
|
import com.mybatisflex.core.util.LambdaGetter;
|
||||||
import com.mybatisflex.core.util.LambdaUtil;
|
import com.mybatisflex.core.util.LambdaUtil;
|
||||||
@ -151,7 +150,7 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public R where(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
public R where(Map<String, Object> whereConditions, SqlOperators operators) {
|
||||||
super.where(whereConditions, operators);
|
super.where(whereConditions, operators);
|
||||||
return (R) this;
|
return (R) this;
|
||||||
}
|
}
|
||||||
@ -202,13 +201,13 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public R and(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
public R and(Map<String, Object> whereConditions, SqlOperators operators) {
|
||||||
super.and(whereConditions, operators);
|
super.and(whereConditions, operators);
|
||||||
return (R) this;
|
return (R) this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public R and(Map<String, Object> whereConditions, Map<String, SqlOperator> operators, SqlConnector innerConnector) {
|
public R and(Map<String, Object> whereConditions, SqlOperators operators, SqlConnector innerConnector) {
|
||||||
super.and(whereConditions, operators, innerConnector);
|
super.and(whereConditions, operators, innerConnector);
|
||||||
return (R) this;
|
return (R) this;
|
||||||
}
|
}
|
||||||
@ -249,13 +248,13 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public R or(Map<String, Object> whereConditions, Map<String, SqlOperator> operators) {
|
public R or(Map<String, Object> whereConditions, SqlOperators operators) {
|
||||||
super.or(whereConditions, operators);
|
super.or(whereConditions, operators);
|
||||||
return (R) this;
|
return (R) this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public R or(Map<String, Object> whereConditions, Map<String, SqlOperator> operators, SqlConnector innerConnector) {
|
public R or(Map<String, Object> whereConditions, SqlOperators operators, SqlConnector innerConnector) {
|
||||||
super.or(whereConditions, operators, innerConnector);
|
super.or(whereConditions, operators, innerConnector);
|
||||||
return (R) this;
|
return (R) this;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2022-2023, Mybatis-Flex (fuhai999@gmail.com).
|
||||||
|
* <p>
|
||||||
|
* 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
|
||||||
|
* <p>
|
||||||
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
* <p>
|
||||||
|
* 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<String, SqlOperator> {
|
||||||
|
|
||||||
|
private final static SqlOperators EMPTY = new SqlOperators();
|
||||||
|
|
||||||
|
static SqlOperators empty() {
|
||||||
|
return EMPTY;
|
||||||
|
}
|
||||||
|
|
||||||
|
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(int initialCapacity) {
|
||||||
|
super(initialCapacity);
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> SqlOperators set(LambdaGetter<T> getter, SqlOperator operator) {
|
||||||
|
this.put(LambdaUtil.getFieldName(getter), operator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
public <T> SqlOperators set(String fieldName, SqlOperator operator) {
|
||||||
|
this.put(fieldName, operator);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -944,6 +944,9 @@ public class TableInfo {
|
|||||||
QueryColumn queryColumn = buildQueryColumn(column);
|
QueryColumn queryColumn = buildQueryColumn(column);
|
||||||
if (operators != null && operators.containsKey(property)) {
|
if (operators != null && operators.containsKey(property)) {
|
||||||
SqlOperator operator = operators.get(property);
|
SqlOperator operator = operators.get(property);
|
||||||
|
if (operator == SqlOperator.LIKE || operator == SqlOperator.NOT_LIKE) {
|
||||||
|
value = "%" + value + "%";
|
||||||
|
}
|
||||||
queryWrapper.and(QueryCondition.create(queryColumn, operator, value));
|
queryWrapper.and(QueryCondition.create(queryColumn, operator, value));
|
||||||
} else {
|
} else {
|
||||||
queryWrapper.and(queryColumn.eq(value));
|
queryWrapper.and(queryColumn.eq(value));
|
||||||
|
|||||||
@ -16,6 +16,7 @@
|
|||||||
|
|
||||||
package com.mybatisflex.coretest;
|
package com.mybatisflex.coretest;
|
||||||
|
|
||||||
|
import com.mybatisflex.core.constant.SqlOperator;
|
||||||
import com.mybatisflex.core.dialect.IDialect;
|
import com.mybatisflex.core.dialect.IDialect;
|
||||||
import com.mybatisflex.core.dialect.KeywordWrap;
|
import com.mybatisflex.core.dialect.KeywordWrap;
|
||||||
import com.mybatisflex.core.dialect.LimitOffsetProcessor;
|
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.TableInfo;
|
||||||
import com.mybatisflex.core.table.TableInfoFactory;
|
import com.mybatisflex.core.table.TableInfoFactory;
|
||||||
import com.mybatisflex.core.table.TableManager;
|
import com.mybatisflex.core.table.TableManager;
|
||||||
|
import com.mybatisflex.core.query.SqlOperators;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
@ -201,7 +203,6 @@ public class AccountSqlTester {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testWhereSql3() {
|
public void testWhereSql3() {
|
||||||
QueryWrapper queryWrapper = QueryWrapper.create()
|
QueryWrapper queryWrapper = QueryWrapper.create()
|
||||||
@ -575,4 +576,30 @@ public class AccountSqlTester {
|
|||||||
// System.out.println(">>>>> informix: " + informixSql);
|
// 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());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user