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

This commit is contained in:
Looly
2023-07-20 11:50:23 +08:00
parent e91d5d8e44
commit f424fc09c4
4 changed files with 41 additions and 16 deletions

View File

@@ -1,5 +1,6 @@
package cn.hutool.db.sql;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.db.DbRuntimeException;
@@ -7,12 +8,13 @@ import cn.hutool.db.Entity;
import cn.hutool.db.Page;
import java.util.Collection;
import java.util.Set;
/**
* 查询对象,用于传递查询所需的字段值<br>
* 查询对象根据表名(可以多个),多个条件 {@link Condition} 构建查询对象完成查询。<br>
* 如果想自定义返回结果,则可在查询对象中自定义要查询的字段名,分页{@link Page}信息来自定义结果。
*
*
* @author Looly
*
*/
@@ -34,13 +36,19 @@ public class Query {
* @since 5.5.3
*/
public static Query of(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
/**
* 构造
*
*
* @param tableNames 表名
*/
public Query(String... tableNames) {
@@ -50,7 +58,7 @@ public class Query {
/**
* 构造
*
*
* @param where 条件语句
* @param tableNames 表名
*/
@@ -60,7 +68,7 @@ public class Query {
/**
* 构造
*
*
* @param where 条件语句
* @param page 分页
* @param tableNames 表名
@@ -71,7 +79,7 @@ public class Query {
/**
* 构造
*
*
* @param fields 字段
* @param tableNames 表名
* @param where 条件
@@ -88,7 +96,7 @@ public class Query {
// --------------------------------------------------------------- Getters and Setters start
/**
* 获得查询的字段名列表
*
*
* @return 查询的字段名列表
*/
public Collection<String> getFields() {
@@ -97,7 +105,7 @@ public class Query {
/**
* 设置查询的字段名列表
*
*
* @param fields 查询的字段名列表
* @return this
*/
@@ -108,7 +116,7 @@ public class Query {
/**
* 设置查询的字段名列表
*
*
* @param fields 查询的字段名列表
* @return this
*/
@@ -119,7 +127,7 @@ public class Query {
/**
* 获得表名数组
*
*
* @return 表名数组
*/
public String[] getTableNames() {
@@ -128,7 +136,7 @@ public class Query {
/**
* 设置表名
*
*
* @param tableNames 表名
* @return this
*/
@@ -139,7 +147,7 @@ public class Query {
/**
* 获得条件语句
*
*
* @return 条件语句
*/
public Condition[] getWhere() {
@@ -148,7 +156,7 @@ public class Query {
/**
* 设置条件语句
*
*
* @param where 条件语句
* @return this
*/
@@ -159,7 +167,7 @@ public class Query {
/**
* 获得分页对象,无分页返回{@code null}
*
*
* @return 分页对象 or {@code null}
*/
public Page getPage() {
@@ -168,7 +176,7 @@ public class Query {
/**
* 设置分页对象
*
*
* @param page 分页对象
* @return this
*/
@@ -180,7 +188,7 @@ public class Query {
/**
* 获得第一个表名
*
*
* @return 表名
* @throws DbRuntimeException 没有表
*/