Files
fantaibao-qlexpress4/docs/execute-source.adoc
small-red-hat 2dfed7464e
Some checks failed
Reduce Adoc / reduce (push) Failing after 57s
Java Unit Test with Maven / test (push) Failing after 1m42s
first
2025-12-29 13:59:13 +08:00

50 lines
2.0 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

:toc:
= 表达式执行
本节说明在 QLExpress4 中执行表达式的多种方式。
== 使用 Map 作为上下文
- `execute(String, Map<String,Object>, QLOptions)`
- 说明:根据方法注释,使用 `Map` 作为上下文,`Map` 的 key 即为脚本中的变量名value 为对应变量值。
- 典型场景:从外部业务数据构造一个 `Map` 传入,直接在脚本里以变量名访问。
[source,java,indent=0]
----
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=firstQl]
----
== 使用对象字段作为上下文
- `execute(String, Object, QLOptions)`
- 说明:根据方法注释,使用一个普通 Java 对象作为上下文,脚本中的变量名对应该对象的字段名(或可访问的 getter
- 典型场景:已有一个承载上下文数据的 DTO/POJO直接将对象传入以便脚本读取其字段。
// 说明:此处引用对象上下文执行的测试代码。
[source,java,indent=0]
----
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=executeWithObject]
----
== 使用带别名的对象集合
- `executeWithAliasObjects(String, QLOptions, Object...)`
- 说明:根据方法注释,传入标记了 `@QLAlias` 的对象,注解值将作为上下文中的变量名;未标记的对象会被忽略。
- 典型场景:以中文(或领域语言)别名暴露对象属性/方法,供规则编写者直观地编写脚本。
[source,java,indent=0]
----
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=qlAlias]
----
== 使用自定义 `ExpressContext`
- `execute(String, ExpressContext, QLOptions)`
- 说明:该重载允许传入实现了 `ExpressContext` 的自定义上下文(例如动态变量上下文)。其它 `execute` 重载最终也会委托到此方法。
- 典型场景:需要按需按名计算变量值、隔离读写、或接入外部存储等高级能力时,自定义上下文最灵活。
[source,java,indent=0]
----
include::../src/test/java/com/alibaba/qlexpress4/Express4RunnerTest.java[tag=dynamicVar]
----