我可以根据收到的消息为 int-jdbc:outbound-channel-adapter 定义目标 table 吗?

Can I define the target table for int-jdbc:outbound-channel-adapter based on the received message?

我正在使用 Spring 集成 JDBC 支持根据特定条件(存储在消息 headers 作为 "table"):

<int:channel id="cmTablesJdbcChannel"></int:channel>
<int-jdbc:outbound-channel-adapter channel="cmTablesJdbcChannel"
    id="cmTableJdbcOutputAdaptor" data-source="datasource"
    query="insert into TABLE_NAME values (int_id, parent_int_id, name) values (:headers[int_id],:headers[parent_int_id],:headers[name])">
</int-jdbc:outbound-channel-adapter>

我尝试用几个表达式替换 TABLE_NAME 但 none 有效:

${headers['table']}
#{headers['table']}
:[headers['table]}

我试图避免使用 20 个不同的出站通道适配器并重复使用一个,但动态设置要使用的 table 的名称。你知道这是否可能吗?

有类似的问题,但与要使用的参数有关:How can I create a dynamic query for Spring Integration JDBC outbound-channel-adapter?

不,它现在不起作用,TABLE_NAME不能作为参数。

随时提出 JIRA 问题以考虑 query-expression 之类的东西在运行时针对请求消息构建 INSERT/UPDATE SQL。

与此同时,您应该使用一些自定义 POJO 中的 NamedParameterJdbcTemplate,以便从 <outbound-channel-adapter> 中使用,或者像一个复杂的表达式:

<service-activator input-channel="cmTablesJdbcChannel" output-channel="nullChannel" 
    expression="@jdbcTemplate.update('insert into ' + headers.table + ' (int_id, parent_int_id, name) values (:int_id,:parent_int_id,:name)', headers)"/>

注意,不要使用带有参数值的直接 SQL 表达式构建。 parametrized: 的变体是任何 RDBMS 的首选方式。它将在服务器端编译(索引、查询计划等)并重新用于所有其他即将执行的操作。