如何在csv中编写多头标头,以便第二个标头值应来自数据库
//预期的输出
personId,firstName,lastName,email,age fullname,total // this is the second header which should come the database kaa,karthi,sa,123@,34
//以下是用于弹簧批处理写入csv的代码段
@Bean(destroyMethod="") public JdbcCursorItemReader<Person> reader(){ JdbcCursorItemReader<Person> cursorItemReader = new JdbcCursorItemReader<>(); cursorItemReader.setDataSource(dataSource); cursorItemReader.setSql("SELECT person_id,first_name,last_name,email,age FROM springbatchdb.person"); cursorItemReader.setRowMapper(new PersonRowMapper()); return cursorItemReader; } @Bean(destroyMethod="") public PersonItenProcessor processor(){ return new PersonItenProcessor(); } @Bean(destroyMethod="") public FlatFileItemWriter<Person> writer(){ FlatFileItemWriter<Person> writer = new FlatFileItemWriter<Person>(); String exportFileHeader = "personId,firstName,lastName,email,age"; StringHeaderWriter headerWriter = new StringHeaderWriter(exportFileHeader); writer.setHeaderCallback(headerWriter); writer.setResource(new ClassPathResource("person.csv")); DelimitedLineAggregator<Person> lineAggregator = new DelimitedLineAggregator<Person>(); lineAggregator.setDelimiter(","); BeanWrapperFieldExtractor<Person> fieldExtractor = new BeanWrapperFieldExtractor<Person>(); fieldExtractor.setNames(new String[]{"personId","firstName","lastName","email","age"}); lineAggregator.setFieldExtractor(fieldExtractor); writer.setLineAggregator(lineAggregator); return writer; }
您可以在中执行查询,FlatFileHeaderCallback然后将结果附加到标题的第一行。
FlatFileHeaderCallback