|
@@ -18,11 +18,10 @@ package com.baomidou.mybatisplus.core.conditions.query;
|
|
|
import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
|
|
|
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
|
|
|
import com.baomidou.mybatisplus.core.metadata.TableFieldInfo;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.*;
|
|
|
-import com.baomidou.mybatisplus.core.toolkit.sql.SqlUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.ArrayUtils;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.StringPool;
|
|
|
+import com.baomidou.mybatisplus.core.toolkit.TableInfoHelper;
|
|
|
|
|
|
-import java.util.Arrays;
|
|
|
-import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.concurrent.atomic.AtomicInteger;
|
|
|
import java.util.function.Predicate;
|
|
@@ -38,69 +37,45 @@ import java.util.function.Predicate;
|
|
|
@SuppressWarnings("serial")
|
|
|
public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>> {
|
|
|
|
|
|
- /**
|
|
|
- * 过滤的字段
|
|
|
- */
|
|
|
- private String predicateSelect;
|
|
|
-
|
|
|
/**
|
|
|
* 查询字段
|
|
|
*/
|
|
|
- private String[] sqlSelect;
|
|
|
-
|
|
|
- /**
|
|
|
- * 排除字段
|
|
|
- */
|
|
|
- @Deprecated
|
|
|
- private String[] excludeColumns = new String[]{};
|
|
|
-
|
|
|
+ private String sqlSelect;
|
|
|
|
|
|
public QueryWrapper() {
|
|
|
this(null);
|
|
|
}
|
|
|
|
|
|
public QueryWrapper(T entity) {
|
|
|
- this(entity, null);
|
|
|
+ this.entity = entity;
|
|
|
+ this.initEntityClass();
|
|
|
+ this.initNeed();
|
|
|
}
|
|
|
|
|
|
- public QueryWrapper(T entity, String... column) {
|
|
|
- this.sqlSelect = column;
|
|
|
+ public QueryWrapper(T entity, String... columns) {
|
|
|
this.entity = entity;
|
|
|
+ this.select(columns);
|
|
|
this.initEntityClass();
|
|
|
this.initNeed();
|
|
|
}
|
|
|
|
|
|
- private QueryWrapper(T entity, String sqlSelect[], AtomicInteger paramNameSeq,
|
|
|
+ /**
|
|
|
+ * 非对外公开的构造方法,只用于生产嵌套 sql
|
|
|
+ *
|
|
|
+ * @param entityClass 本不应该需要的
|
|
|
+ */
|
|
|
+ private QueryWrapper(T entity, Class<T> entityClass, AtomicInteger paramNameSeq,
|
|
|
Map<String, Object> paramNameValuePairs, MergeSegments mergeSegments) {
|
|
|
this.entity = entity;
|
|
|
- this.sqlSelect = sqlSelect;
|
|
|
+ this.entityClass = entityClass;
|
|
|
this.paramNameSeq = paramNameSeq;
|
|
|
this.paramNameValuePairs = paramNameValuePairs;
|
|
|
this.expression = mergeSegments;
|
|
|
- this.initEntityClass();
|
|
|
}
|
|
|
|
|
|
- @Override
|
|
|
- public String getSqlSelect() {
|
|
|
- if (StringUtils.isNotEmpty(predicateSelect)) {
|
|
|
- return predicateSelect;
|
|
|
- }
|
|
|
- //TODO 这里看要不要兼容下原来的sqlSelect,进行切割
|
|
|
- if (ArrayUtils.isNotEmpty(sqlSelect)) {
|
|
|
- List<String> excludeColumnList = Arrays.asList(excludeColumns);
|
|
|
- sqlSelect = Arrays.stream(sqlSelect).filter(i -> !excludeColumnList.contains(i)).toArray(String[]::new);
|
|
|
- } else {
|
|
|
- if (entityClass != null) {
|
|
|
- sqlSelect = TableInfoHelper.getTableColumns(entityClass, excludeColumns);
|
|
|
- }
|
|
|
- }
|
|
|
- return ArrayUtils.isNotEmpty(sqlSelect) ?
|
|
|
- SqlUtils.stripSqlInjection(String.join(StringPool.COMMA, sqlSelect)) : null;
|
|
|
- }
|
|
|
-
|
|
|
- public QueryWrapper<T> select(String... sqlSelect) {
|
|
|
- if (ArrayUtils.isNotEmpty(sqlSelect)) {
|
|
|
- this.sqlSelect = sqlSelect;
|
|
|
+ public QueryWrapper<T> select(String... columns) {
|
|
|
+ if (ArrayUtils.isNotEmpty(columns)) {
|
|
|
+ this.sqlSelect = String.join(StringPool.COMMA, columns);
|
|
|
}
|
|
|
return typedThis;
|
|
|
}
|
|
@@ -112,7 +87,6 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
|
|
|
/**
|
|
|
* <p>
|
|
|
* 过滤查询的字段信息(主键除外!)
|
|
|
- * 目前该方法优先级最高,一旦使用其他的设置select的方法将失效!!!
|
|
|
* </p>
|
|
|
* <p>
|
|
|
* 例1: 只要 java 字段名以 "test" 开头的 -> select(i -> i.getProperty().startsWith("test"))
|
|
@@ -127,49 +101,22 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
|
|
|
*/
|
|
|
public QueryWrapper<T> select(Class<T> entityClass, Predicate<TableFieldInfo> predicate) {
|
|
|
this.entityClass = entityClass;
|
|
|
- this.checkEntityClass();
|
|
|
- this.predicateSelect = TableInfoHelper.getTableInfo(entityClass).chooseSelect(predicate);
|
|
|
- return typedThis;
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 排除字段
|
|
|
- *
|
|
|
- * @param entityClass 实体类
|
|
|
- * @param excludeColumns 排除字段列表
|
|
|
- * @deprecated begin 3.0.3,建议使用 {@link #select(Predicate)},请尽快使用新方法,预计 3.0.5 开始移除此方法
|
|
|
- */
|
|
|
- @Deprecated
|
|
|
- public QueryWrapper<T> excludeColumns(Class<T> entityClass, String... excludeColumns) {
|
|
|
- Assert.notEmpty(excludeColumns, "excludeColumns must not empty");
|
|
|
- this.excludeColumns = excludeColumns;
|
|
|
- this.entityClass = entityClass;
|
|
|
- this.checkEntityClass();
|
|
|
+ this.sqlSelect = TableInfoHelper.getTableInfo(getCheckEntityClass()).chooseSelect(predicate);
|
|
|
return typedThis;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * <p>
|
|
|
- * 排除字段,该方法请在 setEntity 之后使用,否则无法获知表实体类型
|
|
|
- * </p>
|
|
|
- *
|
|
|
- * @param excludeColumns 排除字段列表
|
|
|
- * @deprecated begin 3.0.3,建议使用 {@link #select(Predicate)},请尽快使用新方法,预计 3.0.5 开始移除此方法
|
|
|
- */
|
|
|
- @Deprecated
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public QueryWrapper<T> excludeColumns(String... excludeColumns) {
|
|
|
- this.checkEntityClass();
|
|
|
- return excludeColumns(entityClass, excludeColumns);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* <p>
|
|
|
* 返回一个支持 lambda 函数写法的 wrapper
|
|
|
* </p>
|
|
|
*/
|
|
|
public LambdaQueryWrapper<T> lambda() {
|
|
|
- return new LambdaQueryWrapper<>(entity, paramNameSeq, paramNameValuePairs, expression);
|
|
|
+ return new LambdaQueryWrapper<>(entity, entityClass, sqlSelect, paramNameSeq, paramNameValuePairs, expression);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public String getSqlSelect() {
|
|
|
+ return sqlSelect;
|
|
|
}
|
|
|
|
|
|
@Override
|
|
@@ -177,8 +124,14 @@ public class QueryWrapper<T> extends AbstractWrapper<T, String, QueryWrapper<T>>
|
|
|
return column;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * <p>
|
|
|
+ * 用于生成嵌套 sql
|
|
|
+ * 故 sqlSelect 不向下传递
|
|
|
+ * </p>
|
|
|
+ */
|
|
|
@Override
|
|
|
protected QueryWrapper<T> instance(AtomicInteger paramNameSeq, Map<String, Object> paramNameValuePairs) {
|
|
|
- return new QueryWrapper<>(entity, sqlSelect, paramNameSeq, paramNameValuePairs, new MergeSegments());
|
|
|
+ return new QueryWrapper<>(entity, entityClass, paramNameSeq, paramNameValuePairs, new MergeSegments());
|
|
|
}
|
|
|
}
|