问题
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]
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]'"
But the query attribute does not seem to support Spring expressions.
How can I generate a dynamic query for use by the jdbc:outbound-channel-adapter?
回答1:
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>
Please see this section in spring reference documentation for more details.
来源:https://stackoverflow.com/questions/17005450/how-can-i-create-a-dynamic-query-for-spring-integration-jdbc-outbound-channel-ad