可用版本:Dev (3.21) | 最新 (3.20) | 3.19 | 3.18 | 3.17 | 3.16 | 3.15 | 3.14 | 3.13 | 3.12 | 3.11

变量绑定

适用于 ✅ 开源版   ✅ 专业版   ✅ 企业版

每个 org.jooq.QueryPart 都必须实现 accept(Context<?>) 方法。这个 Context 有两个目的(还有许多其他目的)

  • 它提供了一些关于变量绑定过程中 "状态" 的信息。
  • 它为将值绑定到 context 的内部 java.sql.PreparedStatement 提供了一个通用的 API

此处的 org.jooq.BindContext API 概述

// This method provides access to the PreparedStatement to which bind values are bound
PreparedStatement statement();

// These methods provide convenience to delegate variable binding
BindContext bind(QueryPart part) throws DataAccessException;
BindContext bind(Collection<? extends QueryPart> parts) throws DataAccessException;
BindContext bind(QueryPart[] parts) throws DataAccessException;

// These methods perform the actual variable binding
BindContext bindValue(Object value, Class<?> type) throws DataAccessException;
BindContext bindValues(Object... values) throws DataAccessException;

一些其他方法继承自通用的 org.jooq.Context,该 Context 在 org.jooq.RenderContextorg.jooq.BindContext 之间共享。详细信息记录在上一章关于 SQL 渲染 的内容中

一个将值绑定到 SQL 的示例

通过查看 jOOQ 内部表示的 (简化的) CompareCondition 可以提供一个简单的例子。它用于任何 org.jooq.Condition 比较两个字段,例如这里的 AUTHOR.ID = BOOK.AUTHOR_ID 条件

-- [...]
WHERE AUTHOR.ID = ?
-- [...]

这是 jOOQ 如何在这种条件下绑定值的

@Override
public final void bind(BindContext context) throws DataAccessException {
    // The CompareCondition itself does not bind any variables.
    // But the two fields involved in the condition might do so...
    context.bind(field1).bind(field2);
}

请参阅手册关于 自定义 QueryParts纯 SQL QueryParts 的章节,了解如何编写自己的 query parts 以扩展 jOOQ。

反馈

您对此页面有任何反馈吗?我们很乐意听到您的意见!

The jOOQ Logo