spring batch custom jdbc itemreader

时间:2017-08-22 09:10:39

标签: java spring jdbc

我想创建一个自定义的jdbc项目阅读器,我找到了这个解决方案

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import javax.sql.DataSource;

@Configuration
public class DatabaseToXmlFileJobConfig {

private static final String QUERY_FIND_STUDENTS =
        "SELECT " +
            "email_address, " +
            "name, " +
            "purchased_package " +
        "FROM STUDENTS " +
        "ORDER BY email_address ASC";

@Bean
ItemReader<StudentDTO> databaseXmlItemReader(DataSource dataSource) {
    JdbcCursorItemReader<StudentDTO> databaseReader = new 
JdbcCursorItemReader<>();

    databaseReader.setDataSource(dataSource);
    databaseReader.setSql(QUERY_FIND_STUDENTS);
    databaseReader.setRowMapper(new BeanPropertyRowMapper<>
(StudentDTO.class));

    return databaseReader;
}
}

但我不知道如何使用它,创建我自己的ItemReader? 无论如何要在这两个班级之间建立联系吗? 或者无论如何要创建自定义JDBC阅读器?

public class DatabaseReader implements ItemReader<StudentDTO> {

@Override
public StudentDTO read() throws Exception, UnexpectedInputException, 
ParseException, NonTransientResourceException {

    return null;
}

}

1 个答案:

答案 0 :(得分:0)

在 Spring 批处理中,一个步骤有 3 个核心元素

  1. 读者
  2. 处理器(可选)
  3. 作家

Spring Batch 已经为大多数常见情况提供了一组 readers and writers,如果您没有特殊情况,可以直接使用它们

根据我在您的问题中所看到的,您可以按原样使用它。

工作

    @Bean
    public Job jobTest(){
        JobParametersBuilder parametersBuilder = new JobParametersBuilder();
        Instant timestamp =LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant();

        return jobBuilder.get("myJobName")
                .next(StudentsReport())
                .next(sendReportByEmail())
                .incrementer( jobParameters -> parametersBuilder.addJobParameters(jobParameters)
                                                                .addDate("timeStamp"
                                                                        , Date.from( timestamp)
                                                                 ).toJobParameters()
                )
                .build();
    }

步骤定义

    @Bean
    public Step studentsReport(){
        return stepBuilder.get("getStudentsThatSomething")
                .<User,User>chunk(100)
                .reader(studentsJdbcReader())
                .writer(studentsXMLWriter())
                .build();
    }

JDBC Reader Bean

@Autowired
DataSource dataSource;

private static final String QUERY_FIND_STUDENTS =
        "SELECT " +
            "email_address, " +
            "name, " +
            "purchased_package " +
        "FROM STUDENTS " +
        "ORDER BY email_address ASC";

@Bean
@StepScope
JdbcCursorItemReader<StudentDTO> studentsJdbcReader() {
    JdbcCursorItemReader<StudentDTO> databaseReader = new 
JdbcCursorItemReader<>();

    databaseReader.setDataSource(dataSource);
    databaseReader.setSql(QUERY_FIND_STUDENTS);
    databaseReader.setRowMapper(new BeanPropertyRowMapper<>
(StudentDTO.class));

    return databaseReader;
}

作家豆

    @Bean
    @StepScope
    public ItemWriter<StudentDTO> studentFlatWriter(){
        return new StudentXmlWriter(OUTPUT_FILE);
    }

要创建自定义 XML 编写器类,您可以查看 Alexey Saenko 在他的 Spring Batch Tutorial 中提供的示例,实际上本教程将为您了解 Spring Batch 打下良好的基础。