From 6b45cfb7e2269633e31fa7b7fda88a18430181ef Mon Sep 17 00:00:00 2001 From: Looly Date: Thu, 20 Jul 2023 11:51:37 +0800 Subject: [PATCH] =?UTF-8?q?Query.of(entity)=E6=9E=84=E5=BB=BA=E6=97=B6?= =?UTF-8?q?=E4=BC=A0=E5=85=A5fields?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/dromara/hutool/db/sql/Query.java | 46 +++++++++++++------ 1 file changed, 32 insertions(+), 14 deletions(-) diff --git a/hutool-db/src/main/java/org/dromara/hutool/db/sql/Query.java b/hutool-db/src/main/java/org/dromara/hutool/db/sql/Query.java index 8b7325f35..7fca3d602 100644 --- a/hutool-db/src/main/java/org/dromara/hutool/db/sql/Query.java +++ b/hutool-db/src/main/java/org/dromara/hutool/db/sql/Query.java @@ -12,6 +12,7 @@ package org.dromara.hutool.db.sql; +import org.dromara.hutool.core.collection.CollUtil; import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.array.ArrayUtil; import org.dromara.hutool.db.DbRuntimeException; @@ -19,6 +20,7 @@ import org.dromara.hutool.db.Entity; import org.dromara.hutool.db.Page; import java.util.Collection; +import java.util.Set; /** * 查询对象,用于传递查询所需的字段值
@@ -26,30 +28,45 @@ import java.util.Collection; * 如果想自定义返回结果,则可在查询对象中自定义要查询的字段名,分页{@link Page}信息来自定义结果。 * * @author Looly - * */ public class Query implements Cloneable { - /** 查询的字段名列表 */ + /** + * 查询的字段名列表 + */ Collection fields; - /** 查询的表名 */ + /** + * 查询的表名 + */ String[] tableNames; - /** 查询的条件语句 */ + /** + * 查询的条件语句 + */ Condition[] where; - /** 分页对象 */ + /** + * 分页对象 + */ Page page; /** * 从{@link Entity}构建Query + * * @param where 条件查询{@link Entity},包含条件Map和表名 * @return Query * @since 5.5.3 */ - public static Query of(final Entity where){ - return new Query(SqlUtil.buildConditions(where), where.getTableName()); + public static Query of(final Entity where) { + final Query query = new Query(SqlUtil.buildConditions(where), where.getTableName()); + final Set fieldNames = where.getFieldNames(); + if (CollUtil.isNotEmpty(fieldNames)) { + query.setFields(fieldNames); + } + + return query; } // --------------------------------------------------------------- Constructor start + /** * 构造 * @@ -63,7 +80,7 @@ public class Query implements Cloneable { /** * 构造 * - * @param where 条件语句 + * @param where 条件语句 * @param tableNames 表名 */ public Query(final Condition[] where, final String... tableNames) { @@ -73,8 +90,8 @@ public class Query implements Cloneable { /** * 构造 * - * @param where 条件语句 - * @param page 分页 + * @param where 条件语句 + * @param page 分页 * @param tableNames 表名 */ public Query(final Condition[] where, final Page page, final String... tableNames) { @@ -84,10 +101,10 @@ public class Query implements Cloneable { /** * 构造 * - * @param fields 字段 + * @param fields 字段 * @param tableNames 表名 - * @param where 条件 - * @param page 分页 + * @param where 条件 + * @param page 分页 */ public Query(final Collection fields, final String[] tableNames, final Condition[] where, final Page page) { this.fields = fields; @@ -98,6 +115,7 @@ public class Query implements Cloneable { // --------------------------------------------------------------- Constructor end // --------------------------------------------------------------- Getters and Setters start + /** * 获得查询的字段名列表 * @@ -206,7 +224,7 @@ public class Query implements Cloneable { @Override public Query clone() { try { - return (Query) super.clone(); + return (Query) super.clone(); } catch (final CloneNotSupportedException e) { throw new AssertionError(); }