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

版权声明:转载请注明出处 https://blog.csdn.net/u010502101/article/details/79518174

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.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

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

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </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")
                .<User, User>chunk(4)                                           //每4次提交一次
                .reader(fileItemReader())                           //读取文件,并把文件中每行数据映射到工程中的User bean中
                .writer(jdbcItemWrite())
                .allowStartIfComplete(true)
                .build();
    }

    //3、配置要读取文件的特性*/
    @Bean
    public ItemReader<User> fileItemReader(){
        FlatFileItemReader<User> 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<User> userLineMapper(){
        DefaultLineMapper<User> 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<User>{

        /*一个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<User> jdbcItemWrite(){
        JdbcBatchItemWriter<User> 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中可以观察到写入数据库的内容:
这里写图片描述

阅读更多

没有更多推荐了,返回首页