mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
Merge remote-tracking branch 'origin/main'
This commit is contained in:
commit
00bb32161d
@ -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` 方法,
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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 {
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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;
|
||||
|
||||
@ -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<QueryCondition> {
|
||||
|
||||
//当前条件的上一个条件
|
||||
protected QueryCondition prev;
|
||||
|
||||
//当前条件的下一个条件
|
||||
protected QueryCondition next;
|
||||
|
||||
|
||||
@ -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;
|
||||
|
||||
|
||||
@ -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;
|
||||
@ -37,13 +38,26 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
* 根据实体类对象,构建查询条件
|
||||
*
|
||||
* @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, SqlOperators operators) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(ClassUtil.getUsefulClass(entity.getClass()));
|
||||
return tableInfo.buildQueryWrapper(entity, operators);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public <Q extends QueryWrapper> WithBuilder<Q> with(String name) {
|
||||
if (with == null) {
|
||||
@ -207,7 +221,7 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
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);
|
||||
}
|
||||
|
||||
@ -256,14 +270,14 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@ -305,20 +319,20 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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) {
|
||||
operators = Collections.emptyMap();
|
||||
operators = SqlOperators.empty();
|
||||
}
|
||||
if (mapConditions != null) {
|
||||
QueryCondition condition = null;
|
||||
@ -657,16 +671,16 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
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 +798,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取 queryWrapper 的参数
|
||||
* 在构建 sql 的时候,需要保证 where 在 having 的前面
|
||||
@ -818,7 +831,6 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 获取 queryWrapper 的参数
|
||||
* 在构建 sql 的时候,需要保证 where 在 having 的前面
|
||||
|
||||
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package com.mybatisflex.core.query;
|
||||
|
||||
import com.mybatisflex.core.constant.SqlOperator;
|
||||
import com.mybatisflex.core.constant.SqlConnector;
|
||||
import com.mybatisflex.core.table.TableDef;
|
||||
import com.mybatisflex.core.util.LambdaGetter;
|
||||
import com.mybatisflex.core.util.LambdaUtil;
|
||||
@ -150,7 +150,7 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
}
|
||||
|
||||
@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);
|
||||
return (R) this;
|
||||
}
|
||||
@ -201,13 +201,13 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
}
|
||||
|
||||
@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);
|
||||
return (R) this;
|
||||
}
|
||||
|
||||
@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);
|
||||
return (R) this;
|
||||
}
|
||||
@ -248,13 +248,13 @@ public class QueryWrapperAdapter<R extends QueryWrapperAdapter<R>> extends Query
|
||||
}
|
||||
|
||||
@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);
|
||||
return (R) this;
|
||||
}
|
||||
|
||||
@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);
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -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, SqlOperators operators) {
|
||||
QueryColumn[] queryColumns = new QueryColumn[defaultQueryColumns.length];
|
||||
for (int i = 0; i < defaultQueryColumns.length; i++) {
|
||||
queryColumns[i] = columnQueryMapping.get(defaultQueryColumns[i]);
|
||||
@ -941,7 +942,15 @@ 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);
|
||||
if (operator == SqlOperator.LIKE || operator == SqlOperator.NOT_LIKE) {
|
||||
value = "%" + value + "%";
|
||||
}
|
||||
queryWrapper.and(QueryCondition.create(queryColumn, operator, value));
|
||||
} else {
|
||||
queryWrapper.and(queryColumn.eq(value));
|
||||
}
|
||||
}
|
||||
});
|
||||
return queryWrapper;
|
||||
|
||||
@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@ -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;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user