diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/If.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/If.java
new file mode 100644
index 00000000..86b35882
--- /dev/null
+++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/If.java
@@ -0,0 +1,87 @@
+/**
+ * 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.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);
+ }
+
+}
diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/LambdaConditionBuilder.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/LambdaConditionBuilder.java
new file mode 100644
index 00000000..45dee1aa
--- /dev/null
+++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/LambdaConditionBuilder.java
@@ -0,0 +1,397 @@
+/**
+ * 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 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 QueryWrapper eq(Object value, Predicate 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 QueryWrapper ne(Object value, Predicate 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 QueryWrapper like(Object value, Predicate 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 QueryWrapper likeLeft(Object value, Predicate 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 QueryWrapper likeRight(Object value, Predicate 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 QueryWrapper gt(Object value, Predicate 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 QueryWrapper ge(Object value, Predicate 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 QueryWrapper lt(Object value, Predicate 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 QueryWrapper le(Object value, Predicate 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 QueryWrapper isNull(Predicate when) {
+ queryWrapper.addWhereQueryCondition(queryColumn.isNull(when), connector);
+ return queryWrapper;
+ }
+
+
+ /**
+ * IS NOT NULL
+ *
+ * @return
+ */
+ public QueryWrapper isNotNull() {
+ queryWrapper.addWhereQueryCondition(queryColumn.isNotNull(), connector);
+ return queryWrapper;
+ }
+
+ public QueryWrapper isNotNull(Predicate 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 QueryWrapper in(Object[] arrays, Predicate 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 QueryWrapper in(QueryWrapper queryWrapper, Predicate 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 QueryWrapper in(Collection> collection, Predicate 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 QueryWrapper notIn(Object[] arrays, Predicate 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 QueryWrapper notIn(Collection> collection, Predicate 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 QueryWrapper notIn(QueryWrapper queryWrapper, Predicate 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 QueryWrapper between(Object start, Object end, Predicate 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 QueryWrapper notBetween(Object start, Object end, Predicate when) {
+ if (queryWrapper != null) {
+ queryWrapper.addWhereQueryCondition(queryColumn.notBetween(start, end, when), connector);
+ }
+ return queryWrapper;
+ }
+
+
+}
diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java
index ec528cdf..5823061f 100644
--- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java
+++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryColumn.java
@@ -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 QueryCondition eq(Object value, Predicate 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 QueryCondition ne(Object value, Predicate 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 QueryCondition like(Object value, Predicate 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 QueryCondition likeLeft(Object value, Predicate 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 QueryCondition likeRight(Object value, Predicate 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 QueryCondition gt(Object value, Predicate 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 QueryCondition ge(Object value, Predicate 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 QueryCondition lt(Object value, Predicate 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 QueryCondition le(Object value, Predicate 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 QueryCondition isNull(Predicate 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 QueryCondition isNotNull(Predicate 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 QueryCondition in(Object[] arrays, Predicate 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 QueryCondition in(QueryWrapper queryWrapper, Predicate 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 QueryCondition in(Collection> collection, Predicate 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 QueryCondition notIn(Object[] arrays, Predicate 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 QueryCondition notIn(Collection> collection, Predicate 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 QueryCondition notIn(QueryWrapper queryWrapper, Predicate 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 QueryCondition between(Object start, Object end, Predicate 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 QueryCondition notBetween(Object start, Object end, Predicate fn) {
+ return QueryCondition.create(this, QueryCondition.LOGIC_NOT_BETWEEN, new Object[]{start, end}).when(fn);
+ }
+
////order by ////
public QueryOrderBy asc() {
diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java
index 9e2e1bc7..d11fe066 100644
--- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java
+++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/query/QueryMethods.java
@@ -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 QueryColumn column(LambdaGetter fn) {
+ return LambdaUtil.getQueryColumn(fn);
+ }
+
public static SelectQueryColumn column(QueryWrapper queryWrapper) {
return new SelectQueryColumn(queryWrapper);
}
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 a4eababd..bb38b77d 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
@@ -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 {
@@ -51,6 +50,15 @@ public class QueryWrapper extends BaseQueryWrapper {
}
+ 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 {
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 {
return this;
}
+ public LambdaConditionBuilder where(LambdaGetter 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 {
return this;
}
+ public LambdaConditionBuilder and(LambdaGetter fn) {
+ return new LambdaConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.AND);
+ }
+
+
+ public QueryWrapper and(Consumer 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 {
return this;
}
+ public QueryWrapper or(Consumer consumer) {
+ QueryWrapper newWrapper = new QueryWrapper();
+ consumer.accept(newWrapper);
+ QueryCondition whereQueryCondition = newWrapper.whereQueryCondition;
+ if (whereQueryCondition != null) {
+ or(new Brackets(whereQueryCondition));
+ }
+ return this;
+ }
+
+
+ public LambdaConditionBuilder or(LambdaGetter fn) {
+ return new LambdaConditionBuilder(this, LambdaUtil.getQueryColumn(fn), SqlConnector.OR);
+ }
+
public Joiner leftJoin(String table) {
return joining(Join.TYPE_LEFT, new QueryTable(table), true);
}
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 13730c7a..a21547ad 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
@@ -1071,4 +1071,8 @@ public class TableInfo {
}
return value;
}
+
+ public QueryColumn getQueryColumnByProperty(String property) {
+ return new QueryColumn(schema, tableName, propertyColumnMapping.get(property));
+ }
}
diff --git a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/LambdaUtil.java b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/LambdaUtil.java
index 5a9c1cc1..1d9ce1af 100644
--- a/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/LambdaUtil.java
+++ b/mybatis-flex-core/src/main/java/com/mybatisflex/core/util/LambdaUtil.java
@@ -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, SerializedLambda> lambdaMap = new ConcurrentHashMap<>();
+ private static final Map> classMap = new ConcurrentHashMap<>();
public static String getFieldName(LambdaGetter getter) {
SerializedLambda lambda = getSerializedLambda(getter);
@@ -37,6 +43,22 @@ public class LambdaUtil {
}
+ public static QueryColumn getQueryColumn(LambdaGetter 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 {
diff --git a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java
index 350b0b58..5df715de 100644
--- a/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java
+++ b/mybatis-flex-test/mybatis-flex-native-test/src/main/java/com/mybatisflex/test/EntityTestStarter.java
@@ -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 articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
+// System.out.println(articleDTOS);
+// Page 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 articleDTOS = accountMapper.selectListByQueryAs(asWrapper, ArticleDTO.class);
-//// System.out.println(articleDTOS);
-// Page 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 paginate = accountMapper.paginateAs(Page.of(1, 10), asWrapper, ArticleDTO01.class);
// System.out.println(paginate);
- Db.tx(new Supplier() {
- @Override
- public Boolean get() {
- Cursor 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() {
+// @Override
+// public Boolean get() {
+// Cursor 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 accounts = accountMapper.selectCursorByQuery(asWrapper);
// System.out.println(accounts.isOpen());