Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用JdbcBatchItemWriter从RESTAPI进行Spring批处理读取并将一次读取的多条记录写入单个DB表的代码_Java - Fatal编程技术网

Java 使用JdbcBatchItemWriter从RESTAPI进行Spring批处理读取并将一次读取的多条记录写入单个DB表的代码

Java 使用JdbcBatchItemWriter从RESTAPI进行Spring批处理读取并将一次读取的多条记录写入单个DB表的代码,java,Java,我做了一些搜索,但找不到示例代码。春批 读取RESTAPI(我已经做过)并编写多条记录 使用JdbcBatchItemWriter对单个DB表进行一次读取。 下面是我的BatchConfig代码,但它只写一条记录。我想我 必须让我的处理器返回注册对象和 JDBCItemWriter必须写入多条记录 代码 @Configuration @EnableBatchProcessing public class BatchConfiguration { @Autowired publi

我做了一些搜索,但找不到示例代码。春批 读取RESTAPI(我已经做过)并编写多条记录 使用
JdbcBatchItemWriter
对单个DB表进行一次读取。 下面是我的
BatchConfig
代码,但它只写一条记录。我想我 必须让我的处理器返回注册对象和
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();
    }

 } 
@配置
@启用批处理
公共类批处理配置{
@自动连线
公共建筑商建筑商工厂;
@自动连线
公共StepBuilderFactory StepBuilderFactory;
@自动连线
私人环境;
@豆子
RestTemplate RestTemplate(){
返回新的RestTemplate();
}
//我的读者
@豆子
ItemReader重新雇用eReader(环境
环境
RestTemplate(RestTemplate){
返回新的重新雇用者负责人(
environment.getRequiredProperty(“rest.api.to.listemployees.ugs.api.url”),
restTemplate
);
}
//我的处理器是一个独立的类
@豆子
公共注册项目处理器(){
返回新的RegistrationItemProcessor();
}
//我的作者现在只插入一条记录进行阅读,但我想
为读取插入多个不同数量的记录
@豆子
公共JdbcBatchItemWriter编写器(数据源数据源){
返回新的JdbcBatchItemWriterBuilder()
.itemSqlParameterSourceProvider(新)
BeanPropertyItemSqlParameterSourceProvider())
.sql(“INSERT INTO registration//…*”ommitted INSERT语句
.dataSource(数据源)
.build();
}
@豆子
公共作业导入器作业(JobCompletionNotificationListener侦听器,
步骤1){
返回jobBuilderFactory.get(“importUserJob”)
.incrementer(新的RunIdIncrementer())
.listener(侦听器)
.流程(步骤1)
(完)
.build();
}
@豆子
公共步骤步骤1(JdbcBatchItemWriter编写器){
返回stepBuilderFactory.get(“step1”)
.chunk(10)
.reader(restEmployeeReader(环境,restTemplate()))
.processor(处理器())
.作者(作者)
.build();
}
} 

我的处理器返回了一个列表列表

我的作者如下

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()]));
          }
    }
}
公共类MultiOutputItemWriter实现ItemWriter{
项目编写器项目编写器;
@自动连线
NamedParameterJdbcTemplate namedParamJdbcTemplate;
@凌驾
public void write(列表),我看到了,但找不到可以帮助我的代码