Spring Batch - 仅读者的最后一项更新

时间:2016-09-28 14:29:05

标签: spring spring-batch

我必须从文件(FlatFile)中读取并更新列,如果文件中存在的ID与列中的id匹配。正在正确读取文件,但此处只更新了最后一个id值。请找到代码段

Job-Config.xml

<bean id="abcitemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
        <property name="resource" value="file:datafile/outputs/ibdData.txt" />
    <property name="lineMapper">
        <bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">          
          <property name="lineTokenizer">
                <bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
                <property name="names" value="ID,NAM,TYPE" />
                <property name="delimiter" value="|"/>
            </bean>
          </property>
          <property name="fieldSetMapper">  
          <bean class="com.pershing.intraware.springbatch.mapper.abcFieldsetMapper" />           
          </property>
          </bean>
      </property>
    </bean> 

    <bean id="abcitemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter" scope="step">
    <property name="dataSource" ref="dataSource" /> 
    <property name="sql"><value>UPDATE TEST_abc SET BIZ_ARNG_CD = CASE WHEN ID IN (SELECT ID FROM TEST_abc WHERE ID= ? and MONTH=(to_char(sysdate, 'MM')) AND  YR =(to_char(sysdate, 'YY'))) THEN 'Y' ELSE 'N' END</value></property>
    <!-- It will take care matching between object property and sql name parameter -->
    <property name="itemPreparedStatementSetter" ref="testPrepStatementSetter" /> 
</bean>
</beans>

Setter.java

public class IDItemPreparedStatementSetter implements ItemPreparedStatementSetter<Test>  {
  @Override
  public void setValues(Test item, PreparedStatement ps) throws SQLException {
    // TODO Auto-generated method stub
    ps.setString(1, item.getID());
  }     
}

1 个答案:

答案 0 :(得分:1)

您的查询每次触发时都会更新数据库的每一行。你需要限制它。目前;必须将BIZ_ARNG_CD设置为&#39; Y&#39;对于ID等于传递给作者的最后一条记录的ID的记录。

您可以通过两种方式解决此问题 -

  1. 将数据库列默认为&#39; N&#39;并且不要将它设置为N&#39; N&#39; N&#39; N&#39; N&#39;在更新声明中
  2. 在更新脚本中添加where子句(BIZ_ARNG_CD!=&#39; Y&#39;)