1

我有豆类如下 -

public class Account {
    private String strAccNumber = "";

    private List<Account> accountList = new ArrayList<Account>();

     // getter setter
....
...
@Override
    public String toString() {
        // code for PassThroughLineAggregator
        String data="";
                    for (int j=0; j<accountList.size(); j++) {
            Account bean = accountList.get(j);
            data = data + bean.getStrAccNumber();
            if (j<(accountList.size()-1)) {
                data = data + "\n";
                                }       
        }
        return data;
    }
}

写入数据我想在我的配置文件中设置 accountList。我正在使用下面的代码来设置 bean 属性。

  <bean id="flatFileReader" class="org.springframework.batch.item.file.FlatFileItemReader"
    scope="step">
    <property name="resource" value="classpath:springBatch.csv" />
    <property name="strict" value="false"></property>
    <property name="linesToSkip" value="1" />
    <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="ACC#" />
                </bean>
            </property>
            <property name="fieldSetMapper">
                <bean
                    class="com.abc.reader.AccountDetailsFieldSetMapper" />
            </property>
        </bean>
    </property>
</bean>
 <bean id="flatFileProcessor"
    class="com.abc.processor.AccountItemProcessor"
    scope="step"></bean>
<bean id="flatFileWriter"
        class="com.abc.FlatFileItemWriterListener"
        scope="step">
        <property name="resource" value="classpath:springBatch1.csv" />

        <property name="lineAggregator">
            <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
                <property name="fieldExtractor">
                    <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
                        <property name="names" value="strAccNumber" />
                    </bean>
                </property>
            </bean>
        </property> 

    </bean>

AccountItemProcessor -

 public class AccountItemProcessor implements ItemProcessor<Account, List<Account>>{

@Override
public List<Account> process(Account accObj) throws Exception {
  // logic..
}

在处理步骤中处理单个记录后,我想编写多个项目(项目列表),如何使用 arraylist 一次编写多个项目。就我而言,我想从帐户列表中写入数据。

4

1 回答 1

0

你会想要实现你自己的LineAggregator. 这就是提供String要写入文件的内容。在您的情况下,您将编写一个返回String真正多行的 a 。

您可以在此处的文档中阅读有关该LineAggregator接口的更多信息:http: //docs.spring.io/spring-batch/apidocs/org/springframework/batch/item/file/transform/LineAggregator.html

于 2017-04-24T14:44:03.837 回答