出站通道适配器

出站通道适配器与入站通道适配器相反:它的作用是处理消息并使用它来执行 SQL 查询。默认情况下,消息负载和消息头可作为查询的输入参数,如以下示例所示:

<int-jdbc:outbound-channel-adapter
    query="insert into items (id, status, name) values (:headers[id], 0, :payload[something])"
    data-source="dataSource"
    channel="input"/>

在前面的示例中,到达标记为 input 的通道的消息的负载是一个映射,其中包含一个键为 something 的值,因此 [] 运算符从该映射中解引用该值。消息头也作为映射访问。

前面查询中的参数是传入消息上的 bean 属性表达式(不是 SpEL 表达式)。此行为是 SqlParameterSource 的一部分,SqlParameterSource 是出站适配器创建的默认源。您可以注入不同的 SqlParameterSourceFactory 以获得不同的行为。

出站适配器需要引用 DataSourceJdbcTemplate。您还可以注入 SqlParameterSourceFactory 来控制每个传入消息与查询的绑定。为了更顺畅地使用 SqlParameterSourceFactory(特别是默认的 BeanPropertySqlParameterSourceFactory 及其 MapSqlParameterSource),从 6.5 版本开始,JdbcMessageHandler 公开了一个 usePayloadAsParameterSource 标志,以指示是否应将整个消息作为参数源输入传递。如果输入通道是直接通道,则出站适配器在与消息发送方相同的线程中运行其查询,因此也在相同的事务中运行(如果存在)。

使用 SpEL 表达式传递参数

大多数 JDBC 通道适配器的一个常见要求是将参数作为 SQL 查询或存储过程或函数的一部分进行传递。如前所述,这些参数默认是 bean 属性表达式,而不是 SpEL 表达式。但是,如果需要将 SpEL 表达式作为参数传递,则必须显式注入 SqlParameterSourceFactory

以下示例使用 ExpressionEvaluatingSqlParameterSourceFactory 来实现该要求:

<jdbc:outbound-channel-adapter data-source="dataSource" channel="input"
    query="insert into MESSAGES (MESSAGE_ID,PAYLOAD,CREATED_DATE) values (:id, :payload, :createdDate)"
    sql-parameter-source-factory="spelSource"/>

<bean id="spelSource"
      class="o.s.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
    <property name="parameterExpressions">
        <map>
            <entry key="id"          value="headers['id'].toString()"/>
            <entry key="createdDate" value="new java.util.Date()"/>
            <entry key="payload"     value="payload"/>
        </map>
    </property>
</bean>

有关详细信息,请参阅 定义参数源

使用 PreparedStatement 回调

有时,SqlParameterSourceFactory 的灵活性和松散耦合无法满足目标 PreparedStatement 的需求,或者我们需要进行一些低级 JDBC 工作。Spring JDBC 模块提供了配置执行环境(例如 ConnectionCallbackPreparedStatementCreator)和操作参数值(例如 SqlParameterSource)的 API。它甚至可以访问用于低级操作的 API,例如 StatementCallback

从 Spring Integration 4.2 开始,MessagePreparedStatementSetter 允许在 requestMessage 上下文中手动指定 PreparedStatement 上的参数。此类的作用与标准 Spring JDBC API 中的 PreparedStatementSetter 完全相同。实际上,当 JdbcMessageHandlerJdbcTemplate 上调用 execute 时,它是直接从内联 PreparedStatementSetter 实现中调用的。

此函数式接口选项与 sqlParameterSourceFactory 互斥,可以作为从 requestMessage 填充 PreparedStatement 参数的更强大替代方案。例如,当我们以流式方式将 File 数据存储到数据库 BLOB 列时,它非常有用。以下示例展示了如何实现:

@Bean
@ServiceActivator(inputChannel = "storeFileChannel")
public MessageHandler jdbcMessageHandler(DataSource dataSource) {
    JdbcMessageHandler jdbcMessageHandler = new JdbcMessageHandler(dataSource,
            "INSERT INTO imagedb (image_name, content, description) VALUES (?, ?, ?)");
    jdbcMessageHandler.setPreparedStatementSetter((ps, m) -> {
        ps.setString(1, m.getHeaders().get(FileHeaders.FILENAME));
        try (FileInputStream inputStream = new FileInputStream((File) m.getPayload()); ) {
            ps.setBlob(2, inputStream);
        }
        catch (Exception e) {
            throw new MessageHandlingException(m, e);
        }
        ps.setClob(3, new StringReader(m.getHeaders().get("description", String.class)));
    });
    return jdbcMessageHandler;
}

从 XML 配置的角度来看,<int-jdbc:outbound-channel-adapter> 组件上提供了 prepared-statement-setter 属性。它允许您指定 MessagePreparedStatementSetter bean 引用。

批量更新

从 5.1 版本开始,如果请求消息的负载是 Iterable 实例,JdbcMessageHandler 会执行 JdbcOperations.batchUpdate()Iterable 的每个元素都将被包装成一个 Message,并带有请求消息的头,如果该元素本身不是 Message 的话。在基于常规 SqlParameterSourceFactory 的配置中,这些消息用于构建 SqlParameterSource[] 作为上述 JdbcOperations.batchUpdate() 函数中使用的参数。当应用 MessagePreparedStatementSetter 配置时,将使用 BatchPreparedStatementSetter 变体来迭代每个项的这些消息,并针对它们调用提供的 MessagePreparedStatementSetter。当选择 keysGenerated 模式时,不支持批量更新。