mirror of
https://gitee.com/mybatis-flex/mybatis-flex.git
synced 2025-12-07 00:58:24 +08:00
feat: add lambda query
This commit is contained in:
parent
3bb82a2813
commit
cd5b65f2cc
@ -0,0 +1,87 @@
|
||||
/**
|
||||
* 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.util.ClassUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
|
||||
import java.lang.reflect.Array;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
public class If {
|
||||
|
||||
|
||||
/**
|
||||
* 判断对象是否为空
|
||||
*/
|
||||
public static boolean isNull(Object object) {
|
||||
return object == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断对象是否非空
|
||||
*/
|
||||
public static boolean notNull(Object object) {
|
||||
return !isNull(object);
|
||||
}
|
||||
|
||||
/**
|
||||
* 查看某个对象是否为空,支持数组、集合、map 等
|
||||
* @param object
|
||||
*/
|
||||
public static boolean notEmpty(Object object){
|
||||
if (object == null){
|
||||
return false;
|
||||
}
|
||||
|
||||
if (object instanceof Collection){
|
||||
return !((Collection<?>) object).isEmpty();
|
||||
}
|
||||
|
||||
if (ClassUtil.isArray(object.getClass())){
|
||||
return Array.getLength(object) >0;
|
||||
}
|
||||
|
||||
if (object instanceof Map){
|
||||
return !((Map<?, ?>) object).isEmpty();
|
||||
}
|
||||
|
||||
if (object instanceof String){
|
||||
return StringUtil.isNotBlank((String) object);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查看某个对象是否为空数据 或者 null
|
||||
* @param object
|
||||
*/
|
||||
public static boolean isEmpty(Object object){
|
||||
return !notEmpty(object);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 查看某个 string 对象是否有文本内容
|
||||
* @param object
|
||||
*/
|
||||
public static boolean hasText(Object object){
|
||||
return object != null && StringUtil.isNotBlank((String) object);
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,397 @@
|
||||
/**
|
||||
* 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 java.util.Collection;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class LambdaConditionBuilder {
|
||||
|
||||
private QueryWrapper queryWrapper;
|
||||
private QueryColumn queryColumn;
|
||||
private SqlConnector connector;
|
||||
|
||||
public LambdaConditionBuilder(QueryWrapper queryWrapper, QueryColumn queryColumn, SqlConnector connector) {
|
||||
this.queryWrapper = queryWrapper;
|
||||
this.queryColumn = queryColumn;
|
||||
this.connector = connector;
|
||||
}
|
||||
|
||||
/**
|
||||
* equals
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public QueryWrapper eq(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.eq(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
public <T> QueryWrapper eq(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.eq(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* not equals !=
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public QueryWrapper ne(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.ne(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper ne(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.ne(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* like %%
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public QueryWrapper like(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.like(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper like(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.like(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper likeLeft(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.likeLeft(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper likeLeft(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.likeLeft(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper likeRight(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.likeRight(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper likeRight(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.likeRight(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于 greater than
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public QueryWrapper gt(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.gt(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper gt(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.gt(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于等于 greater or equal
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public QueryWrapper ge(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.ge(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper ge(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.ge(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于 less than
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public QueryWrapper lt(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.lt(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper lt(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.lt(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于等于 less or equal
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public QueryWrapper le(Object value) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.le(value), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper le(Object value, Predicate<T> when) {
|
||||
if (value != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.le(value, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* IS NULL
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public QueryWrapper isNull() {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.isNull(), connector);
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper isNull(Predicate<T> when) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.isNull(when), connector);
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* IS NOT NULL
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
public QueryWrapper isNotNull() {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.isNotNull(), connector);
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper isNotNull(Predicate<T> when) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.isNotNull(when), connector);
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* in arrays
|
||||
*
|
||||
* @param arrays
|
||||
* @return
|
||||
*/
|
||||
public QueryWrapper in(Object... arrays) {
|
||||
if (arrays != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.in(arrays), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper in(Object[] arrays, Predicate<T> when) {
|
||||
//忽略 QueryWrapper.in("name", null) 的情况
|
||||
if (arrays != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.in(arrays, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* in child select
|
||||
*
|
||||
* @param queryWrapper
|
||||
* @return
|
||||
*/
|
||||
public QueryWrapper in(QueryWrapper queryWrapper) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.in(queryWrapper), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper in(QueryWrapper queryWrapper, Predicate<T> when) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.in(queryWrapper, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* in Collection
|
||||
*
|
||||
* @param collection
|
||||
* @return
|
||||
*/
|
||||
public QueryWrapper in(Collection<?> collection) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.in(collection), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper in(Collection<?> collection, Predicate<T> when) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.in(collection, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* not int arrays
|
||||
*
|
||||
* @param arrays
|
||||
* @return
|
||||
*/
|
||||
public QueryWrapper notIn(Object... arrays) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.notIn(arrays), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper notIn(Object[] arrays, Predicate<T> when) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.notIn(arrays, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* not in Collection
|
||||
*
|
||||
* @param collection
|
||||
* @return
|
||||
*/
|
||||
public QueryWrapper notIn(Collection<?> collection) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.notIn(collection), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper notIn(Collection<?> collection, Predicate<T> when) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.notIn(collection, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
/**
|
||||
* not in child select
|
||||
*
|
||||
* @param queryWrapper
|
||||
*/
|
||||
public QueryWrapper notIn(QueryWrapper queryWrapper) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.notIn(queryWrapper), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper notIn(QueryWrapper queryWrapper, Predicate<T> when) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.notIn(queryWrapper, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* between
|
||||
*
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
public QueryWrapper between(Object start, Object end) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.between(start, end), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper between(Object start, Object end, Predicate<T> when) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.between(start, end, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* not between
|
||||
*
|
||||
* @param start
|
||||
* @param end
|
||||
*/
|
||||
public QueryWrapper notBetween(Object start, Object end) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.notBetween(start, end), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
public <T> QueryWrapper notBetween(Object start, Object end, Predicate<T> when) {
|
||||
if (queryWrapper != null) {
|
||||
queryWrapper.addWhereQueryCondition(queryColumn.notBetween(start, end, when), connector);
|
||||
}
|
||||
return queryWrapper;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@ -23,6 +23,7 @@ import com.mybatisflex.core.util.*;
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
/**
|
||||
* 查询列,描述的是一张表的字段
|
||||
@ -114,6 +115,14 @@ public class QueryColumn implements Serializable {
|
||||
}
|
||||
|
||||
|
||||
public <T> QueryCondition eq(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_EQUALS, value).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* not equals !=
|
||||
*
|
||||
@ -126,7 +135,19 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_NOT_EQUALS, value);
|
||||
}
|
||||
|
||||
public <T> QueryCondition ne(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_NOT_EQUALS, value).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* like %%
|
||||
*
|
||||
* @param value
|
||||
*/
|
||||
public QueryCondition like(Object value) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
@ -134,6 +155,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, "%" + value + "%");
|
||||
}
|
||||
|
||||
public <T> QueryCondition like(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, "%" + value + "%").when(fn);
|
||||
}
|
||||
|
||||
|
||||
public QueryCondition likeLeft(Object value) {
|
||||
if (value == null) {
|
||||
@ -142,6 +170,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, "%" + value);
|
||||
}
|
||||
|
||||
public <T> QueryCondition likeLeft(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, "%" + value).when(fn);
|
||||
}
|
||||
|
||||
|
||||
public QueryCondition likeRight(Object value) {
|
||||
if (value == null) {
|
||||
@ -150,6 +185,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, value + "%");
|
||||
}
|
||||
|
||||
public <T> QueryCondition likeRight(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LIKE, value + "%").when(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于 greater than
|
||||
*
|
||||
@ -162,6 +204,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_GT, value);
|
||||
}
|
||||
|
||||
public <T> QueryCondition gt(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_GT, value).when(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* 大于等于 greater or equal
|
||||
*
|
||||
@ -174,6 +223,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_GE, value);
|
||||
}
|
||||
|
||||
public <T> QueryCondition ge(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_GE, value).when(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于 less than
|
||||
*
|
||||
@ -186,6 +242,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LT, value);
|
||||
}
|
||||
|
||||
public <T> QueryCondition lt(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LT, value).when(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* 小于等于 less or equal
|
||||
*
|
||||
@ -198,6 +261,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LE, value);
|
||||
}
|
||||
|
||||
public <T> QueryCondition le(Object value, Predicate<T> fn) {
|
||||
if (value == null) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_LE, value).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* IS NULL
|
||||
@ -208,6 +278,10 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_IS_NULL, null);
|
||||
}
|
||||
|
||||
public <T> QueryCondition isNull(Predicate<T> fn) {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_IS_NULL, null).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* IS NOT NULL
|
||||
@ -218,6 +292,10 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_IS_NOT_NULL, null);
|
||||
}
|
||||
|
||||
public <T> QueryCondition isNotNull(Predicate<T> fn) {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_IS_NOT_NULL, null).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* in arrays
|
||||
@ -233,6 +311,14 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_IN, arrays);
|
||||
}
|
||||
|
||||
public <T> QueryCondition in(Object[] arrays, Predicate<T> fn) {
|
||||
//忽略 QueryWrapper.in("name", null) 的情况
|
||||
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_IN, arrays).when(fn);
|
||||
}
|
||||
|
||||
/**
|
||||
* in child select
|
||||
*
|
||||
@ -243,6 +329,10 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_IN, queryWrapper);
|
||||
}
|
||||
|
||||
public <T> QueryCondition in(QueryWrapper queryWrapper, Predicate<T> fn) {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_IN, queryWrapper).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* in Collection
|
||||
@ -257,6 +347,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
|
||||
public <T> QueryCondition in(Collection<?> collection, Predicate<T> fn) {
|
||||
if (collection != null && !collection.isEmpty()) {
|
||||
return in(collection.toArray(), fn);
|
||||
}
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* not int arrays
|
||||
*
|
||||
@ -271,6 +368,14 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_NOT_IN, arrays);
|
||||
}
|
||||
|
||||
public <T> QueryCondition notIn(Object[] arrays, Predicate<T> fn) {
|
||||
//忽略 QueryWrapper.notIn("name", null) 的情况
|
||||
if (arrays == null || arrays.length == 0 || (arrays.length == 1 && arrays[0] == null)) {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_NOT_IN, arrays).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* not in Collection
|
||||
@ -285,6 +390,13 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
|
||||
public <T> QueryCondition notIn(Collection<?> collection, Predicate<T> fn) {
|
||||
if (collection != null && !collection.isEmpty()) {
|
||||
return notIn(collection.toArray(), fn);
|
||||
}
|
||||
return QueryCondition.createEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* not in child select
|
||||
*
|
||||
@ -294,6 +406,10 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_NOT_IN, queryWrapper);
|
||||
}
|
||||
|
||||
public <T> QueryCondition notIn(QueryWrapper queryWrapper, Predicate<T> fn) {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_NOT_IN, queryWrapper).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* between
|
||||
@ -305,6 +421,11 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_BETWEEN, new Object[]{start, end});
|
||||
}
|
||||
|
||||
public <T> QueryCondition between(Object start, Object end, Predicate<T> fn) {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_BETWEEN, new Object[]{start, end}).when(fn);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* not between
|
||||
*
|
||||
@ -315,6 +436,10 @@ public class QueryColumn implements Serializable {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_NOT_BETWEEN, new Object[]{start, end});
|
||||
}
|
||||
|
||||
public <T> QueryCondition notBetween(Object start, Object end, Predicate<T> fn) {
|
||||
return QueryCondition.create(this, QueryCondition.LOGIC_NOT_BETWEEN, new Object[]{start, end}).when(fn);
|
||||
}
|
||||
|
||||
|
||||
////order by ////
|
||||
public QueryOrderBy asc() {
|
||||
|
||||
@ -15,6 +15,9 @@
|
||||
*/
|
||||
package com.mybatisflex.core.query;
|
||||
|
||||
import com.mybatisflex.core.util.LambdaGetter;
|
||||
import com.mybatisflex.core.util.LambdaUtil;
|
||||
|
||||
public class QueryMethods {
|
||||
|
||||
public static FunctionQueryColumn count() {
|
||||
@ -90,6 +93,10 @@ public class QueryMethods {
|
||||
return new QueryColumn(schema, table, column);
|
||||
}
|
||||
|
||||
public static <T> QueryColumn column(LambdaGetter<T> fn) {
|
||||
return LambdaUtil.getQueryColumn(fn);
|
||||
}
|
||||
|
||||
public static SelectQueryColumn column(QueryWrapper queryWrapper) {
|
||||
return new SelectQueryColumn(queryWrapper);
|
||||
}
|
||||
|
||||
@ -17,14 +17,13 @@ package com.mybatisflex.core.query;
|
||||
|
||||
import com.mybatisflex.core.FlexConsts;
|
||||
import com.mybatisflex.core.dialect.DialectFactory;
|
||||
import com.mybatisflex.core.exception.FlexExceptions;
|
||||
import com.mybatisflex.core.table.TableDef;
|
||||
import com.mybatisflex.core.util.ArrayUtil;
|
||||
import com.mybatisflex.core.util.CollectionUtil;
|
||||
import com.mybatisflex.core.util.SqlUtil;
|
||||
import com.mybatisflex.core.util.StringUtil;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import com.mybatisflex.core.util.*;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
|
||||
@ -51,6 +50,15 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(Class<?>... entityClasses) {
|
||||
for (Class<?> entityClass : entityClasses) {
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
|
||||
from(new QueryTable(tableInfo.getSchema(), tableInfo.getTableName()));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper from(String... tables) {
|
||||
for (String table : tables) {
|
||||
if (StringUtil.isBlank(table)) {
|
||||
@ -99,10 +107,8 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
if (CollectionUtil.isEmpty(queryTables)) {
|
||||
throw new IllegalArgumentException("query table must not be empty.");
|
||||
}
|
||||
if (queryTables.size() > 1) {
|
||||
throw FlexExceptions.wrap("QueryWrapper.as(...) only support 1 table");
|
||||
}
|
||||
queryTables.get(0).alias = alias;
|
||||
|
||||
queryTables.get(queryTables.size() - 1).alias = alias;
|
||||
return this;
|
||||
}
|
||||
|
||||
@ -131,6 +137,10 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T> LambdaConditionBuilder where(LambdaGetter<T> fn) {
|
||||
return new LambdaConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.AND);
|
||||
}
|
||||
|
||||
public QueryWrapper and(QueryCondition queryCondition) {
|
||||
return addWhereQueryCondition(queryCondition, SqlConnector.AND);
|
||||
}
|
||||
@ -145,6 +155,21 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public <T> LambdaConditionBuilder and(LambdaGetter<T> fn) {
|
||||
return new LambdaConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.AND);
|
||||
}
|
||||
|
||||
|
||||
public QueryWrapper and(Consumer<QueryWrapper> consumer) {
|
||||
QueryWrapper newWrapper = new QueryWrapper();
|
||||
consumer.accept(newWrapper);
|
||||
QueryCondition whereQueryCondition = newWrapper.whereQueryCondition;
|
||||
if (whereQueryCondition != null) {
|
||||
and(new Brackets(whereQueryCondition));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryWrapper or(QueryCondition queryCondition) {
|
||||
return addWhereQueryCondition(queryCondition, SqlConnector.OR);
|
||||
}
|
||||
@ -159,6 +184,21 @@ public class QueryWrapper extends BaseQueryWrapper<QueryWrapper> {
|
||||
return this;
|
||||
}
|
||||
|
||||
public QueryWrapper or(Consumer<QueryWrapper> consumer) {
|
||||
QueryWrapper newWrapper = new QueryWrapper();
|
||||
consumer.accept(newWrapper);
|
||||
QueryCondition whereQueryCondition = newWrapper.whereQueryCondition;
|
||||
if (whereQueryCondition != null) {
|
||||
or(new Brackets(whereQueryCondition));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public <T> LambdaConditionBuilder or(LambdaGetter<T> fn) {
|
||||
return new LambdaConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.OR);
|
||||
}
|
||||
|
||||
public Joiner<QueryWrapper> leftJoin(String table) {
|
||||
return joining(Join.TYPE_LEFT, new QueryTable(table), true);
|
||||
}
|
||||
|
||||
@ -1071,4 +1071,8 @@ public class TableInfo {
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public QueryColumn getQueryColumnByProperty(String property) {
|
||||
return new QueryColumn(schema, tableName, propertyColumnMapping.get(property));
|
||||
}
|
||||
}
|
||||
|
||||
@ -15,6 +15,10 @@
|
||||
*/
|
||||
package com.mybatisflex.core.util;
|
||||
|
||||
import com.mybatisflex.core.exception.FlexExceptions;
|
||||
import com.mybatisflex.core.query.QueryColumn;
|
||||
import com.mybatisflex.core.table.TableInfo;
|
||||
import com.mybatisflex.core.table.TableInfoFactory;
|
||||
import org.apache.ibatis.reflection.property.PropertyNamer;
|
||||
import org.apache.ibatis.util.MapUtil;
|
||||
|
||||
@ -26,9 +30,11 @@ import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
public class LambdaUtil {
|
||||
|
||||
private LambdaUtil() {}
|
||||
private LambdaUtil() {
|
||||
}
|
||||
|
||||
private static final Map<Class<?>, SerializedLambda> lambdaMap = new ConcurrentHashMap<>();
|
||||
private static final Map<String, Class<?>> classMap = new ConcurrentHashMap<>();
|
||||
|
||||
public static <T> String getFieldName(LambdaGetter<T> getter) {
|
||||
SerializedLambda lambda = getSerializedLambda(getter);
|
||||
@ -37,6 +43,22 @@ public class LambdaUtil {
|
||||
}
|
||||
|
||||
|
||||
public static <T> QueryColumn getQueryColumn(LambdaGetter<T> getter) {
|
||||
SerializedLambda lambda = getSerializedLambda(getter);
|
||||
String methodName = lambda.getImplMethodName();
|
||||
String implClass = lambda.getImplClass();
|
||||
Class<?> entityClass = MapUtil.computeIfAbsent(classMap, implClass, s -> {
|
||||
try {
|
||||
return Class.forName(s.replace("/","."));
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw FlexExceptions.wrap(e);
|
||||
}
|
||||
});
|
||||
TableInfo tableInfo = TableInfoFactory.ofEntityClass(entityClass);
|
||||
return tableInfo.getQueryColumnByProperty(PropertyNamer.methodToProperty(methodName));
|
||||
}
|
||||
|
||||
|
||||
private static SerializedLambda getSerializedLambda(Serializable getter) {
|
||||
return MapUtil.computeIfAbsent(lambdaMap, getter.getClass(), aClass -> {
|
||||
try {
|
||||
|
||||
@ -19,16 +19,13 @@ import com.mybatisflex.core.MybatisFlexBootstrap;
|
||||
import com.mybatisflex.core.audit.AuditManager;
|
||||
import com.mybatisflex.core.audit.ConsoleMessageCollector;
|
||||
import com.mybatisflex.core.audit.MessageCollector;
|
||||
import com.mybatisflex.core.query.If;
|
||||
import com.mybatisflex.core.query.QueryWrapper;
|
||||
import com.mybatisflex.core.row.Db;
|
||||
import org.apache.ibatis.cursor.Cursor;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder;
|
||||
import org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import static com.mybatisflex.test.table.AccountTableDef.ACCOUNT;
|
||||
import static com.mybatisflex.test.table.ArticleTableDef.ARTICLE;
|
||||
|
||||
@ -113,29 +110,50 @@ public class EntityTestStarter {
|
||||
// Object object = accountMapper.selectObjectByQuery(wrapper2);
|
||||
// System.out.println(object);
|
||||
//
|
||||
// QueryWrapper asWrapper = QueryWrapper.create()
|
||||
|
||||
QueryWrapper asWrapper1 = QueryWrapper.create()
|
||||
// .select(ARTICLE.ALL_COLUMNS)
|
||||
// .select(ACCOUNT.USER_NAME.as(ArticleDTO::getAuthorName)
|
||||
// , ACCOUNT.AGE.as(ArticleDTO::getAuthorAge)
|
||||
// , ACCOUNT.BIRTHDAY
|
||||
// )
|
||||
.from(ARTICLE)
|
||||
.leftJoin(ACCOUNT).as("a").on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
||||
.where(Account::getId).ge(100, If::notEmpty)
|
||||
.and(queryWrapper -> {
|
||||
queryWrapper
|
||||
.where(Account::getId).ge(100)
|
||||
.or(Account::getAge).gt(200)
|
||||
.and(Article::getAccountId).eq(200)
|
||||
.or(queryWrapper1 -> {
|
||||
queryWrapper1.where(Account::getId).like("a",If::notEmpty);
|
||||
})
|
||||
;
|
||||
});
|
||||
System.out.println(asWrapper1.toSQL());
|
||||
// .and(query->query.and);
|
||||
// .andEq(Account::getId,100);
|
||||
// .and(new Brackets(column))
|
||||
// .where(column(Account::getId).eq(1).when(If::notEmpty));
|
||||
|
||||
// .where(ARTICLE.ID.ge(0).or(ACCOUNT.ID.ge(0)));
|
||||
// .where(Account::getId).eq(1).and()
|
||||
// where(eq(Account::getId,1))
|
||||
|
||||
|
||||
//
|
||||
// List<ArticleDTO> articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
|
||||
// System.out.println(articleDTOS);
|
||||
// Page<ArticleDTO> paginate = accountMapper.paginateAs(Page.of(1, 1), asWrapper1, ArticleDTO.class);
|
||||
// System.out.println(paginate);
|
||||
//
|
||||
//
|
||||
//
|
||||
// QueryWrapper asWrapper = QueryWrapper.create()
|
||||
// .select(ARTICLE.ALL_COLUMNS,ACCOUNT.ALL_COLUMNS)
|
||||
// .from(ARTICLE)
|
||||
// .leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
||||
//// .where(ACCOUNT.ID.ge(0));
|
||||
// .where(ARTICLE.ID.ge(0).or(ACCOUNT.ID.ge(0)));
|
||||
////
|
||||
//// List<ArticleDTO> articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
|
||||
//// System.out.println(articleDTOS);
|
||||
// Page<ArticleDTO> paginate = accountMapper.paginateAs(Page.of(1, 1), asWrapper, ArticleDTO.class);
|
||||
// System.out.println(paginate);
|
||||
|
||||
|
||||
|
||||
QueryWrapper asWrapper = QueryWrapper.create()
|
||||
.select(ARTICLE.ALL_COLUMNS,ACCOUNT.ALL_COLUMNS)
|
||||
.from(ARTICLE)
|
||||
.leftJoin(ACCOUNT).on(ARTICLE.ACCOUNT_ID.eq(ACCOUNT.ID))
|
||||
.where(ARTICLE.ID.ge(0).or(ACCOUNT.ID.ge(0)));
|
||||
|
||||
// RowUtil.printPretty(Db.selectListByQuery(asWrapper));
|
||||
//
|
||||
@ -144,19 +162,19 @@ public class EntityTestStarter {
|
||||
// Page<ArticleDTO01> paginate = accountMapper.paginateAs(Page.of(1, 10), asWrapper, ArticleDTO01.class);
|
||||
// System.out.println(paginate);
|
||||
|
||||
Db.tx(new Supplier<Boolean>() {
|
||||
@Override
|
||||
public Boolean get() {
|
||||
Cursor<Account> accounts = accountMapper.selectCursorByQuery(asWrapper);
|
||||
System.out.println(accounts.isOpen());
|
||||
for (Account account : accounts) {
|
||||
System.out.println(accounts.isOpen());
|
||||
System.out.println(account);
|
||||
}
|
||||
System.out.println(accounts.isOpen());
|
||||
return true;
|
||||
}
|
||||
});
|
||||
// Db.tx(new Supplier<Boolean>() {
|
||||
// @Override
|
||||
// public Boolean get() {
|
||||
// Cursor<Account> accounts = accountMapper.selectCursorByQuery(asWrapper);
|
||||
// System.out.println(accounts.isOpen());
|
||||
// for (Account account : accounts) {
|
||||
// System.out.println(accounts.isOpen());
|
||||
// System.out.println(account);
|
||||
// }
|
||||
// System.out.println(accounts.isOpen());
|
||||
// return true;
|
||||
// }
|
||||
// });
|
||||
|
||||
// Cursor<Account> accounts = accountMapper.selectCursorByQuery(asWrapper);
|
||||
// System.out.println(accounts.isOpen());
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user