Я немного поискал, но не смог найти пример кода. Весенняя партия
чтение из REST api (что я сделал) и запись нескольких записей
за одно чтение одной таблицы БД с помощью JdbcBatchItemWriter
.
Ниже мой код BatchConfig
, но он записывает только одну запись. я думаю я
должен заставить мой процессор возвращать объект List of Registration и
JDBCItemWriter
должен записывать несколько записей
код
@Configuration
@EnableBatchProcessing
public class BatchConfiguration {
@Autowired
public JobBuilderFactory jobBuilderFactory;
@Autowired
public StepBuilderFactory stepBuilderFactory;
@Autowired
private Environment environment;
@Bean
RestTemplate restTemplate() {
return new RestTemplate();
}
//my reader
@Bean
ItemReader<EmployeeEmploymentDTO> restEmployeeReader(Environment
environment,
RestTemplate restTemplate) {
return new RESTEmployeeReader(
environment.getRequiredProperty("rest.api.to.listemployees.ugs.api.url"),
restTemplate
);
}
//my processor which is a separate class
@Bean
public RegistrationItemProcessor processor() {
return new RegistrationItemProcessor();
}
//my writer which now only inserts one record for a read but i want to
insert multiple varying number of records for a read
@Bean
public JdbcBatchItemWriter<Registration> writer(DataSource dataSource) {
return new JdbcBatchItemWriterBuilder<Registration>()
.itemSqlParameterSourceProvider(new
BeanPropertyItemSqlParameterSourceProvider<>())
.sql("INSERT INTO registration //.....*ommitted insert statement
.dataSource(dataSource)
.build();
}
@Bean
public Job importUserJob(JobCompletionNotificationListener listener,
Step step1) {
return jobBuilderFactory.get("importUserJob")
.incrementer(new RunIdIncrementer())
.listener(listener)
.flow(step1)
.end()
.build();
}
@Bean
public Step step1(JdbcBatchItemWriter<Registration> writer) {
return stepBuilderFactory.get("step1")
.<EmployeeEmploymentDTO, Registration> chunk(10)
.reader(restEmployeeReader(environment,restTemplate()))
.processor(processor())
.writer(writer)
.build();
}
}
Мой процессор вернул список List
Мой писатель, как показано ниже
public class MultiOutputItemWriter implements ItemWriter<List<Registration>> {
ItemWriter<Registration> itemWriter;
@Autowired
NamedParameterJdbcTemplate namedParamJdbcTemplate;
@Override
public void write(List<? extends List<Registration>> items) throws Exception {
for (List<Registration> registrations : items) {
final String SQL_INSERT_INTO_REGISTRATION = "INSERT INTO registration (employee_id, ....";
final List<MapSqlParameterSource> params = new ArrayList<>();
for (Registration registration : registrations) {
MapSqlParameterSource param = new MapSqlParameterSource();
param.addValue("employeeId", registration.getEmployeeId());
param.addValue("startDate", registration.getStartDate());
param.addValue("user", registration.getUser());
param.addValue("endTime", registration.getEndTime());
params.add(param);
}
namedParamJdbcTemplate.batchUpdate(SQL_INSERT_INTO_REGISTRATION,params.toArray(new MapSqlParameterSource[params.size()]));
}
}
}
Я видел stackoverflow.com/questions/7836689/… и stackoverflow.com/questions/7894445/…, но не смог найти код, который мог бы мне помочь