【问题标题】:Spring Integration :How to send array as a single payload to a jdbc : outbound gatewaySpring Integration:如何将数组作为单个有效负载发送到 jdbc:出站网关
【发布时间】:2014-04-06 18:14:38
【问题描述】:

如何将数组作为单个有效负载发送到 jdbc:Spring Integration 的出站网关? 我正在构造一个字符串数组并将其发送到接口方法。然而,SQL 接受一个参数 ":payload",但它会因 UncategorizedSQLException 而失败。

出站网关的Sql查询如下

<int-jdbc:outbound-gateway data-source="dataSource"   request-channel="requestChannel" 
                           query="select XMLMSG from Table where SEQ_ID in (:payload)"                                
                           reply-channel="replyChannel" > 

</int-jdbc:outbound-gateway>

serviceinterface.findBySequenceIds(sequenceIdStringArray);

【问题讨论】:

    标签: spring-integration


    【解决方案1】:

    实际上对于substituteNamedParameters,对于IN 子句,参数必须是Collection 而不是arrayNamedParameterJdbcTemplate.substituteNamedParameters的源码:

    if (value instanceof Collection) {
        Iterator<?> entryIter = ((Collection<?>) value).iterator();
        int k = 0;
            while (entryIter.hasNext()) {
        if (k > 0) {
            actualSql.append(", ");
            }
    
    ......
          actualSql.append("?");
    

    所以,只需从您的字符串构造List,而不是array

    Arrays.asList("foo", "bar", "baz");
    

    而你真正的查询将是:

    select XMLMSG from Table where SEQ_ID in (?, ?, ?)
    

    JdbcTemplate 将为您完成的其他工作。

    更新

    注意,默认情况下&lt;int-jdbc:outbound-gateway/&gt; 只选择一条记录。要摆脱此限制,请指定 max-rows-per-poll="0"

    【讨论】:

    • Spring Integration 使用配置文件,无法动态构造 SQL 查询来计算命名参数的数量。
    • 不,你的配置没问题。您的问题是关于payload 的类型。我说real 查询,意思是发送到数据库的内容。我知道如何做 Spring Integration。您只需将数组 payload 更改为 List
    • 谢谢,让我试试这个方法。我会及时通知你。
    • 我尝试了这种方法,但是回复通道/行映射器只获取了一条记录(xml有效负载)。请告知。
    • 1.你的SELECT 真的返回几条记录吗? 2.如何为查询参数构建List有效载荷? 3. 你没试过调试,至少你的代码?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-05-10
    • 1970-01-01
    • 2018-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多