diff --git a/CHANGELOG.md b/CHANGELOG.md index f428a9478..0a55a707a 100755 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ * 【core 】 身份证工具类isValidCard18、isValidCard15入参null直接返回null(pr#1034@Gitee) * 【http 】 使用multiparty方式支持body参数(issue#3158@Github) * 【core 】 ZipReader增加setMaxSizeDiff方法,自定义或关闭ZipBomb(issue#3018@Github) +* 【db 】 Query.of(entity)构建时传入fields(issue#I7M5JU@Gitee) ### 🐞Bug修复 * 【core 】 修复MapUtil工具使用filter方法构造传入参数结果问题(issue#3162@Github) diff --git a/hutool-db/src/main/java/cn/hutool/db/sql/Query.java b/hutool-db/src/main/java/cn/hutool/db/sql/Query.java index 3ad95f9c9..205ca4ec3 100644 --- a/hutool-db/src/main/java/cn/hutool/db/sql/Query.java +++ b/hutool-db/src/main/java/cn/hutool/db/sql/Query.java @@ -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; /** * 查询对象,用于传递查询所需的字段值
* 查询对象根据表名(可以多个),多个条件 {@link Condition} 构建查询对象完成查询。
* 如果想自定义返回结果,则可在查询对象中自定义要查询的字段名,分页{@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 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 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 没有表 */ diff --git a/hutool-extra/src/main/java/cn/hutool/extra/expression/engine/jfireel/JfireELEngine.java b/hutool-extra/src/main/java/cn/hutool/extra/expression/engine/jfireel/JfireELEngine.java index 908e60f6b..9c6cd3f79 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/expression/engine/jfireel/JfireELEngine.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/expression/engine/jfireel/JfireELEngine.java @@ -14,6 +14,10 @@ import java.util.Map; */ public class JfireELEngine implements ExpressionEngine { + static { + checkEngineExist(Expression.class); + } + /** * 构造 */ @@ -24,4 +28,8 @@ public class JfireELEngine implements ExpressionEngine { public Object eval(String expression, Map context) { return Expression.parse(expression).calculate(context); } + + private static void checkEngineExist(Class clazz){ + // do nothing + } } diff --git a/hutool-extra/src/main/java/cn/hutool/extra/expression/engine/rhino/RhinoEngine.java b/hutool-extra/src/main/java/cn/hutool/extra/expression/engine/rhino/RhinoEngine.java index 0e3610228..9bc183605 100644 --- a/hutool-extra/src/main/java/cn/hutool/extra/expression/engine/rhino/RhinoEngine.java +++ b/hutool-extra/src/main/java/cn/hutool/extra/expression/engine/rhino/RhinoEngine.java @@ -17,6 +17,10 @@ import java.util.Map; */ public class RhinoEngine implements ExpressionEngine { + static { + checkEngineExist(Context.class); + } + @Override public Object eval(String expression, Map context) { final Context ctx = Context.enter(); @@ -31,4 +35,8 @@ public class RhinoEngine implements ExpressionEngine { Context.exit(); return result; } + + private static void checkEngineExist(Class clazz){ + // do nothing + } }