I trying to write a spring batch
using SpringBoot
. At first the program will read data from database, then write it to .csv
file format.
This is the code I have tried
Reader
@Bean
public ItemReader<A> Reader() throws Exception {
List list = new ArrayList<>();
JdbcCursorItemReader<A> reader = new JdbcCursorItemReader<A>();
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
String sql = "SELECT ID AS id FROM TABLE_A ";
list = jdbcTemplate.query(sql, new Mapper()); // store all retrieved data to list
reader.setSql(sql);
reader.setDataSource(dataSource);
reader.setRowMapper(new Mapper());
if (list != null) {
for (A c : (List<A>) list) {
c.setId("123"); // overwrite the id
c.setState("Active");
}
}
return reader;
}
Mapper
public class Mapper implements RowMapper<A> {
@Override
public A mapRow(ResultSet rs, int rowNum) throws SQLException {
A c = new A();
c.setId(rs.getString("id"));
}
Writer
@Bean
public ItemWriter<A> Writer() {
FlatFileItemWriter<A> csvFileWriter = new FlatFileItemWriter<>();
String exportFileHeader = "ID" + delimiter + "State";
StringHeaderWriter headerWriter = new StringHeaderWriter(exportFileHeader);
csvFileWriter.setHeaderCallback(headerWriter);
headerWriter.checkRepository();
String exportFilePath = "/home/xxx/Desktop/outputs/" + fileName + time() + ".csv";
csvFileWriter.setResource(new FileSystemResource(exportFilePath));
LineAggregator<A> lineAggregator = createStudentLineAggregator();
csvFileWriter.setLineAggregator(lineAggregator);
return csvFileWriter;
}
When I check the generated file, I can see the value id from database, but not 123. Value for the State is also empty . How can I overwrite the mapping value ?