2017-06-27 24 views
1

我有以下bean定義:JdbcPollingChannelAdapter更新 - 無豆

@Bean 
JdbcPollingChannelAdapter jdbcPollingChannelAdapter() { 
    // Get all the pending jobs 
    JdbcPollingChannelAdapter adapter = new JdbcPollingChannelAdapter(jdbcTemplate, "select id from poller_jobs where status = 'PENDING'"); 
    // Immediately mark them as running so the next jdbc poll doesn't re-process them 
    adapter.setUpdateSql("update poller_jobs set status='RUNNING' where id in (:id)"); 
    adapter.setMaxRowsPerPoll(100); 
    adapter.setRowMapper((r, i) -> r.getLong("id")); 
    return adapter; 
} 

而這種失敗,因爲該行映射器映射到只是一個long ID,因此適配器不知道如何獲取更新中需要的ID。任何人都知道如何做到這一點,而不需要select *並映射到一個完整的對象?這似乎比我真正需要的更多的開銷。

回答

1

這個工作對我來說:

<inbound-channel-adapter data-source="dataSource" 
          channel="target" 
          query="select id from item where status=2" 
          update="update item set status=10 where id in (:#root)" 
          update-per-row="true" 
          row-mapper="columnRowMapper"/> 

    <beans:bean id="columnRowMapper" class="org.springframework.jdbc.core.SingleColumnRowMapper"/> 

所以,第一個是(:#root)作爲PARAM佔位只是因爲默認setUpdateSqlParameterSourceFactory()ExpressionEvaluatingSqlParameterSourceFactory,其中評估上下文的根對象是SELECT的結果,或者因爲它是由update-per-row="true"站立,每排在ResultSet

if (payload != null && this.updateSql != null) { 
     if (this.updatePerRow) { 
      for (Object row : payload) { 
       executeUpdateQuery(row); 
      } 
     } 
     else { 
      executeUpdateQuery(payload); 
     } 
    } 

因此,您在您的配置需要的就是這個代碼兩行:

​​

SingleColumnRowMapper確實在ResultSet真正單列的伎倆,BTW。

+0

完美地工作。謝謝! –