Query.of(entity)构建时传入fields

This commit is contained in:
Looly 2023-07-20 11:51:37 +08:00
parent 1e7e9dfee2
commit 6b45cfb7e2

View File

@ -12,6 +12,7 @@
package org.dromara.hutool.db.sql; 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.collection.ListUtil;
import org.dromara.hutool.core.array.ArrayUtil; import org.dromara.hutool.core.array.ArrayUtil;
import org.dromara.hutool.db.DbRuntimeException; import org.dromara.hutool.db.DbRuntimeException;
@ -19,6 +20,7 @@ import org.dromara.hutool.db.Entity;
import org.dromara.hutool.db.Page; import org.dromara.hutool.db.Page;
import java.util.Collection; import java.util.Collection;
import java.util.Set;
/** /**
* 查询对象用于传递查询所需的字段值<br> * 查询对象用于传递查询所需的字段值<br>
@ -26,30 +28,45 @@ import java.util.Collection;
* 如果想自定义返回结果则可在查询对象中自定义要查询的字段名分页{@link Page}信息来自定义结果 * 如果想自定义返回结果则可在查询对象中自定义要查询的字段名分页{@link Page}信息来自定义结果
* *
* @author Looly * @author Looly
*
*/ */
public class Query implements Cloneable { public class Query implements Cloneable {
/** 查询的字段名列表 */ /**
* 查询的字段名列表
*/
Collection<String> fields; Collection<String> fields;
/** 查询的表名 */ /**
* 查询的表名
*/
String[] tableNames; String[] tableNames;
/** 查询的条件语句 */ /**
* 查询的条件语句
*/
Condition[] where; Condition[] where;
/** 分页对象 */ /**
* 分页对象
*/
Page page; Page page;
/** /**
* {@link Entity}构建Query * {@link Entity}构建Query
*
* @param where 条件查询{@link Entity}包含条件Map和表名 * @param where 条件查询{@link Entity}包含条件Map和表名
* @return Query * @return Query
* @since 5.5.3 * @since 5.5.3
*/ */
public static Query of(final Entity where){ public static Query of(final Entity where) {
return new Query(SqlUtil.buildConditions(where), where.getTableName()); final Query query = new Query(SqlUtil.buildConditions(where), where.getTableName());
final Set<String> fieldNames = where.getFieldNames();
if (CollUtil.isNotEmpty(fieldNames)) {
query.setFields(fieldNames);
}
return query;
} }
// --------------------------------------------------------------- Constructor start // --------------------------------------------------------------- Constructor start
/** /**
* 构造 * 构造
* *
@ -98,6 +115,7 @@ public class Query implements Cloneable {
// --------------------------------------------------------------- Constructor end // --------------------------------------------------------------- Constructor end
// --------------------------------------------------------------- Getters and Setters start // --------------------------------------------------------------- Getters and Setters start
/** /**
* 获得查询的字段名列表 * 获得查询的字段名列表
* *