如何为 Spring Integration JDBC outbound-channel-adapter 创建动态查询? [英] How can I create a dynamic query for Spring Integration JDBC outbound-channel-adapter?
问题描述
我需要创建一个由 jdbc:outbound-channel-adapter 执行的动态更新语句,因为我需要使用具有可变数量条件的 case 表达式设置一个字段,如下所示:
I need to create a dynamic update statement to be be executed by a jdbc:outbound-channel-adapter, because I need to set one field using a case expression that has variable number of conditions as follows:
UPDATE tableA
SET fieldA = CASE
WHEN fieldB IN ('a','b') THEN 1
WHEN fieldB IN ('c','d') THEN 2
...
WHEN fieldB IN (...) THEN N
END
WHERE fieldC = :headers[MY_FIELDC]
我可以在 Spring 表达式中创建这个动态更新语句,如下所示:
I can create this dynamic update statement in a Spring expression as follows:
"'UPDATE tableA SET fieldA = ' + headers[MY_CASE_EXP] + ' WHERE fieldC = :headers[MY_FIELDC]'"
但查询属性似乎不支持Spring表达式.
But the query attribute does not seem to support Spring expressions.
如何生成供 jdbc:outbound-channel-adapter 使用的动态查询?
How can I generate a dynamic query for use by the jdbc:outbound-channel-adapter?
推荐答案
为了动态创建查询,需要使用ExpressionEvaluatingSqlParameterSourceFactory
,如下所示:
In order to create query dynamically, you need to use ExpressionEvaluatingSqlParameterSourceFactory
, something like below:
<jdbc:outbound-channel-adapter data-source="dataSource" channel="outboundJdbcChannelOne"
query="UPDATE tableA SET fieldA = :something WHERE fieldC = :somethingElse"
sql-parameter-source-factory="spelSource"/>
<bean id="spelSource" class="org.springframework.integration.jdbc.ExpressionEvaluatingSqlParameterSourceFactory">
<property name="parameterExpressions">
<map>
<entry key="something" value="headers['MY_CASE_EXP']"/>
<entry key="somethingElse" value="headers['MY_FIELDC']"/>
</map>
</property>
</bean>
请参阅本部分 在 spring 参考文档中了解更多详细信息.
Please see this section in spring reference documentation for more details.
这篇关于如何为 Spring Integration JDBC outbound-channel-adapter 创建动态查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!