1

我有一个 Pojo 合作伙伴:合作伙伴 ID 列表

地址 Pojo : AddressId, Address, City, Country, Pin

我想在 Spring Batch 中创建一个平面文件 - 文件将是:PartnerId;AddressId;Address;City;Country;Pin

我正在获取带有 ID 和地址列表的合作伙伴 Pojo

如何将 FlatFileItemWriter 与 PartnerPojo My FlatFileItemWriterConfiguration 配置一起使用:

<?xml version="1.0" encoding="UTF-8"?>
<bean id="itemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter">
   <property name="resource" value="file:outputFile.txt" />
   <property name="lineAggregator">
      <bean class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
         <property name="delimiter" value=";" />
         <property name="fieldExtractor">
            <bean class="org.springframework.batch.item.file.transform.BeanWrapperFieldExtractor">
               <property name="names" value="partnerId, addressId, address,city,country,pin " />
            </bean>
         </property>
      </bean>
   </property>
   <property name="headerCallback" ref="headerCallback" />
</bean>

我在 addressId 上收到错误

4

1 回答 1

1

您需要展平数据并将输出文件中预期的展平项目列表传递给编写器。例如:

class Partner {
   int id;
   List<Address> addresses;
}

class Address {
   int addressId;
   String address,city,country,pin;
}

// create this Pojo to encapsulate flat data (as in the expected csv)
class PartnerAddress {
   int partnerId, addressId;
   String address,city,country,pin;
}

项目处理器将准备数据:

class PartnerItemProcessor implements ItemProcessor<Partner, List<PartnerAddress>> {

    @Override
    public List<PartnerAddress> process(Partner partner) {
        List<PartnerAddress> partnerAddresses = new ArrayList<>();
        for (Address address : partner.getAddresses()) {
            PartnerAddress partnerAddress = new PartnerAddress();
            partnerAddress.setPartnerId(partner.getId());
            partnerAddress.setAddressId(address.getAddressId());
            partnerAddress.setAddress(address.getAddress());
            partnerAddress.setCity(address.getCity());
            partnerAddress.setCountry(address.getCountry());
            partnerAddress.setPin(address.getPin());
            partnerAddresses.add(partnerAddress);
        }
        return partnerAddresses;
    }
}

然后作者收到列表PartnerAddress并将它们写入平面文件。

希望这可以帮助。

于 2018-12-05T09:57:31.390 回答