2016-02-20 32 views
0

在Spring批處理中,我的CompositeItemWriter使用多個ItemWriters將記錄插入到多個表中,但是目前這些ItemWriters中的每一個都按批處理方式插入,如下所示Spring批處理CompositeItemWriter在遍歷每個委託Writer時結束時執行批處理爲了提高性能

public class Item{ 
    public List<WarningObject> warnings 
    public List<ErrorObject> errors 
    public Long Id; 
    public String data1; 
    public String data2; 
    public String data3; 
    // getters and setters 

} 

我的作家配置

<bean id="compositeItemWriter" class="org.springframework.batch.item.support.CompositeItemWriter"> 
<property name="delegates"> 
<list> 
    <ref bean="warningItemWriter"/> 
    <ref bean="errorItemWriter"/> 
    <ref bean="CustomJdbcItemWriter"/> 
</list> 
</property> 

public class WarningItemWriter implements ItemWriter<Item>{ 

    @Autowire 
    String sql; 
    @AutoWire 
    JdbcTemple jdbcTemplate; 

    @Autowire 
    ItemPreparedStatementSetter itemPreparedStatementSetter; 

    @Override 
    public void write(final List<? extends Item> items) throws Exception { 
     for(Item item: items){ 

      jdbcTemplate.execute(sql,new PreparedStatementCallBack<int []>{ 
      @Override 
        public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { 
         for (WarningObject warning: item.getWarnings) { 
          itemPreparedStatementSetter.setValues(warning, ps); 
          ps.addBatch(); 
         } 
         return ps.executeBatch(); 

      }); 
     } 
    } 

} 

所以這工作正常,但這並沒有真正成爲一個警告的批量插入

目前它插入批次中我的一個項目的所有警告。但我最好想添加每個項目的警告,以準備陳述和所有項目的所有警告一​​旦添加我想打電話ps.executeBatch()

有人可以幫助我用這種方法嗎?

回答

0

外環剛剛轉會匿名類內部:

jdbcTemplate.execute(sql, new PreparedStatementCallBack<int []> { 
    @Override 
    public int[] doInPreparedStatement(PreparedStatement ps) throws SQLException, DataAccessException { 
     for (Item item: items) { 
      for (WarningObject warning: item.getWarnings) { 
       itemPreparedStatementSetter.setValues(warning, ps); 
       ps.addBatch(); 
      } 
     } 
     return ps.executeBatch(); 
    } 
}); 
+0

謝謝@Artefacto, –