Java 在spring批处理JdbcBatchItemWriter中添加多个更新查询

Java 在spring批处理JdbcBatchItemWriter中添加多个更新查询,java,spring-batch,Java,Spring Batch,我需要根据我在文件中收到的数据对两个表运行更新。源是一个单独的文件 我尝试使用多个查询搜索JdbcBatchItemWriter 目前,我尝试了下面的方法,一个作业,两个步骤——在这两个步骤中使用相同的阅读器、相同的处理器和相同的侦听器——但我认为这是重复的 <batch:job id="batchJob"> <batch:step id="step1" next="step2"> <batch:tasklet>

我需要根据我在文件中收到的数据对两个表运行更新。源是一个单独的文件

我尝试使用多个查询搜索JdbcBatchItemWriter

目前,我尝试了下面的方法,一个作业,两个步骤——在这两个步骤中使用相同的阅读器、相同的处理器和相同的侦听器——但我认为这是重复的

<batch:job id="batchJob">

    <batch:step id="step1" next="step2">
        <batch:tasklet>

            <batch:chunk reader="cvsFileItemReader" processor="myItemProcessor"
                writer="mysqlItemWriter1" commit-interval="1" skip-limit="10">
                <batch:listeners>
                    <batch:listener ref="stepListener">
                    </batch:listener>
                </batch:listeners>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>

    <batch:step id="step2">
        <batch:tasklet>
            <batch:chunk reader="cvsFileItemReader" processor="myItemProcessor"
                writer="mysqlItemWriter2" commit-interval="1" skip-limit="10">
                <batch:listeners>
                    <batch:listener ref="stepListener">
                    </batch:listener>
                </batch:listeners>
            </batch:chunk>
        </batch:tasklet>
    </batch:step>

</batch:job>

我的itemWriter1和2运行更新,我需要获取这两个查询的更新计数

这只是我发现的,但我认为这还不够理想

请让我知道我们是否可以在一个编写器中使用多个更新查询。还建议是否有任何最佳的方式


谢谢

您可以解决装饰问题
JdbcBatchItemWriter
:此装饰器负责在
ExecutionContext
中存储真正的编写器编写的项目数量,以便您可以稍后在步骤中返回

class CountingItemWriter implements ItemStreamWriter {
  private JdbcBatchItemWriter delegate;
  private int count = 0;
  private final String id;

  CountingItemWriter(String id) {
    this.id = "CountingItemWriter#"+id;
  }

  write(List<?> items) {
    delegate.write(items);
    count += items.size();
  }

  open(ExecutionContext ec) {
    count = getInt(this.id, 0);
  }

  update(ExecutionContext ec) {
    ec.putInt(this.id, count);
  }

  close() {
    // no-op
  }
}
class CountingItemWriter实现ItemStreamWriter{
私有JdbcBatchItemWriter委托;
私有整数计数=0;
私有最终字符串id;
CountingItemWriter(字符串id){
this.id=“CountingItemWriter#”+id;
}
写(列出项目){
委托。编写(项目);
count+=items.size();
}
打开(ExecutionContext ec){
count=getInt(this.id,0);
}
更新(ExecutionContext ec){
ec.putInt(此id,计数);
}
关闭(){
//无操作
}
}

如果您有多个写入程序可以为每个写入程序检索正确的计数器,请记住提供不同的
id

使用由两个不同的
JdbcBatchItemWriter
组成的
CompositeItemWriter
,每个查询一个execute@LucaBassoRicci,谢谢您的回复,我尝试过这种方法,但是我们如何获得每个writer的更新计数谢谢你的回复,我需要的计数是我的sql查询在每个writer或每个表中更新的行数,正如我前面提到的,我认为你给我的计数将给出items计数,我尝试使用步骤执行上下文中的writeCount,但是它给了我整个步骤的计数,而不是每个项目的计数-updateIf
delegate.write()
complete successfully您确定您编写了
items.size()
;如果write抛出异常,SB回滚所有并使用
commit interval=1
重新启动最后一个块。很抱歉,刚才的评论,是的,这对我很有帮助