How can I create a dynamic query for Spring Integration JDBC outbound-channel-adapter?

99封情书 提交于 2019-12-24 01:05:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!