我正在使用spring批处理来处理CSV文件。
fullname,dateOfBirth, gender, nationality
Joe Smith,09/11/1966, M, America
William Norman,22/22/1988,M, Cuba
fullname,dateOfBirth, gender, nationality,currency,regions
Joe Smith,09/11/1966, M, America,Dollar, North America
William Norman,22/22/1988,M, Cuba,Peso, South America
enter code here .
private FlatFileItemReader<Person> reader(String filename) {
FlatFileItemReader<Person> reader = new FlatFileItemReader<>();
reader.setLinesToSkip(1);
reader.setStrict(false);
log.debug("Job parameter {}", filename);
reader.setResource(new FileSystemResource(filename));
reader.setRecordSeparatorPolicy(new DefaultRecordSeparatorPolicy());
reader.setLineMapper(new PersonLineMapper());
return reader;
}
enter code here .
public PersonLineMapper() {
super();
DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
lineTokenizer.setNames( "fullname","dateOfBirth", "gender", "natinlaity");
setLineTokenizer(lineTokenizer);
BeanWrapperFieldSetMapper<Person> beanWrapperFieldSetMapper = new BeanWrapperFieldSetMapper<>();
beanWrapperFieldSetMapper.setTargetType(Person.class);
beanWrapperFieldSetMapper.setCustomEditors(Collections.singletonMap("java.time.LocalDate", new LocalDatePropertyEditor()));
setFieldSetMapper(beanWrapperFieldSetMapper);
}
一切正常。
我的问题是有没有办法动态映射csv到bean。 就像我的csv引入了新的标题,如何处理它们