数据写入——写入到数据库中

spring batch提供了JdbcBatchItemWriter类,用来把数据写入到数据库中。
示例:本例把下面的User.txt文件中的数据保存到Mysql数据库中。

ID,NAME,AGE
1,lzj,28
2,tom,20
3,terry,30
4,lerry,18
5,bob,25
6,linda,27
7,marry,39
8,long,22
9,kin,33
10,jiken,40

在配置该作业程序前,首先导入依赖的包:

    <parent>
        <groupId>org.springframework.bootgroupId>
        <artifactId>spring-boot-starter-parentartifactId>
        <version>1.5.10.RELEASEversion>
        <relativePath /> 
    parent>

    <properties>
        <project.build.sourceEncoding>UTF-8project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8project.reporting.outputEncoding>
        <java.version>1.8java.version>
    properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-batchartifactId>
        dependency>
        <dependency>
            <groupId>org.springframeworkgroupId>
            <artifactId>spring-oxmartifactId>
        dependency>
        <dependency>
            <groupId>com.h2databasegroupId>
            <artifactId>h2artifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>mysqlgroupId>
            <artifactId>mysql-connector-javaartifactId>
            <scope>runtimescope>
        dependency>
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.springframework.batchgroupId>
            <artifactId>spring-batch-testartifactId>
            <scope>testscope>
        dependency>
        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
        dependency>
    dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.bootgroupId>
                <artifactId>spring-boot-maven-pluginartifactId>
            plugin>
        plugins>
    build>

然后在application.properties中配置mysql的数据源如下:

spring.datasource.url=jdbc:mysql://localhost:3306/lzj_database
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=lzjlzj

下面配置该作业程序,首先从User.txt中读取数据,然后把数据写入数据库的users表中:

@Configuration
public class BatchConfig {

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private DataSource dataSource;

    /*1、创建一个Job作业*/
    @Bean
    public Job fileReaderJob(){
        return jobBuilderFactory.get("fileReaderJob")
        .start(chunkStep())
        .build();
    }

    //2、创建一个step*/
    @Bean
    public Step chunkStep(){
        return stepBuilderFactory.get("chunkStep")
                .chunk(4)                                           //每4次提交一次
                .reader(fileItemReader())                           //读取文件,并把文件中每行数据映射到工程中的User bean中
                .writer(jdbcItemWrite())
                .allowStartIfComplete(true)
                .build();
    }

    //3、配置要读取文件的特性*/
    @Bean
    public ItemReader fileItemReader(){
        FlatFileItemReader reader= new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("/data/User.txt")); //指定读取文件的位置
        reader.setLinesToSkip(1);                                    //文件的第一行不用读取,跳过第一行
        reader.setLineMapper(userLineMapper());                      //设置lineMapper,用于把文件中的每行数据映射到工程的bean中
        return reader;
    }

    //3.1 定义一个LineMapper,用于把文件中的每行数据映射到工程的bean中*/
    private LineMapper userLineMapper(){
        DefaultLineMapper lineMapper = new DefaultLineMapper<>();
        lineMapper.setLineTokenizer(userLineTokenizer());        //设置文件的解析器
        lineMapper.setFieldSetMapper(new UserFieldStepMapper()); //设置文件的映射器
        lineMapper.afterPropertiesSet();                         //完成文件的配置
        return lineMapper;
    }

    //3.2 定义文件的解析器,解析文件中的ID、NAME、AGE三列*/
    private LineTokenizer userLineTokenizer(){
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames(new String[]{"ID", "NAME", "AGE"});
        return tokenizer;
    }

    //3.3 实现FieldSetMapper,用于从User.txt文件中读取数据,用读取的数据创建工程中的User对象*/
    private static class UserFieldStepMapper implements FieldSetMapper{

        /*一个fieldSed对应文件中的一行数据*/
        @Override
        public User mapFieldSet(FieldSet fieldSet) throws BindException {
            // TODO Auto-generated method stub
            return new User(fieldSet.readString("ID"), 
                    fieldSet.readString("NAME"), 
                    fieldSet.readString("AGE"));
        }

    }

    @Bean
    public ItemWriter jdbcItemWrite(){
        JdbcBatchItemWriter jdbcWrite = new JdbcBatchItemWriter<>();
        jdbcWrite.setDataSource(dataSource);
        jdbcWrite.setSql("INSERT INTO USERS(id, name, age)" +
                "VALUES(:id, :name, :age)");
        /*把user对象中的数据映射到 :id, :name, :age中*/
        jdbcWrite.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
        jdbcWrite.afterPropertiesSet(); //对参数校验是否为空
        return jdbcWrite;
    }

}

启动工程,运行该作业,在数据库users中可以观察到写入数据库的内容:
数据写入——写入到数据库中_第1张图片

你可能感兴趣的:(spring,batch)