SpringBatch读文件(FlatFileItemReader)写据库(MyBatisBatchItemWriter)(四)

文章目录

    • 一、抽取顺序读取数据库公共Writer
    • 二、mapper配置文件
    • 三、写数据库的job
    • 四、继承公共CommonProcesor
    • 五、执行job

前言:我们在日常开发中可能会遇到写数据库,SpringBatch封装的也有写数据库的Writer,但是我们这次做实验用的是Mybatis封装的MyBatisBatchItemWriter

代码已上传GitHub上面地址:https://github.com/FadeHub/spring-boot-learn/tree/master/spring-boot-springbatch

注意:本章job使用的所有实体类均是上一章SpringBatch读数据库(MyBatisPagingItemReader)(三)的实体,所以这一章
就不在一一列举了。

SpringBatch其它文章直通车:

SpringBatch读单个文件(FlatFileItemReader)和写单个文件(FlatFileItemWriter)(一)
SpringBatch顺序读取多文件(MultiResourceItemReader)和顺序写文件(MultiResourceItemWriter)(二)
SpringBatch读数据库(MyBatisPagingItemReader)(三)
SpringBatch读文件(FlatFileItemReader)写据库(MyBatisBatchItemWriter)(四)
SpringBatch 监听器之Job监听器(JobExecutionListener)和Step监听器(StepExecutionListener)(五)
SpringBatch 监听器之Chunk监听器(ChunkListener)和Skip监听器(SkipListener)(六)

一、抽取顺序读取数据库公共Writer

CommonMybatisItemWriter继承MyBatisBatchItemWriter

package com.sl.common;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisBatchItemWriter;

/**
 * 写数据库
 * @author shuliangzhao
 * @Title: CommonMybatisItemWriter
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/10 19:18
 */
public class CommonMybatisItemWriter extends MyBatisBatchItemWriter {

    public  CommonMybatisItemWriter(SqlSessionFactory sqlSessionFactory,String name) {
        setSqlSessionFactory(sqlSessionFactory);
        setStatementId("com.sl.entity." + name + ".insertCat");
        setAssertUpdates(false);
    }


}

二、mapper配置文件





    
    

        
        
        
        

    


    
           insert into cat(id, catname, catage, cataddress)
               values(#{id},#{catname}, #{catage}, #{cataddress})
   

    




三、写数据库的job

package com.sl.config;

import com.sl.common.CommonFileItemReader;
import com.sl.common.CommonMybatisItemWriter;
import com.sl.entity.CafeCat;
import com.sl.entity.Cat;
import com.sl.processor.CatProcessor;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author shuliangzhao
 * @Title: CafeCatConfiguration
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/10 19:31
 */
@Configuration
@EnableBatchProcessing
public class CafeCatConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private CatProcessor catProcessor;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Bean
    public Job cafeCatJob() {
        return jobBuilderFactory.get("cafeCatJob")
                .start(cafeCatStep())
                .build();
    }

    @Bean
    public Step cafeCatStep() {
        return stepBuilderFactory.get("cafeCatStep")
                .chunk(10)
                .reader(cafeCatCommonFileItemReader())
                .processor(catProcessor)
                .writer(catCommonMybatisItemWriter())
                .build();
    }

    @Bean
    @StepScope
    public CommonFileItemReader cafeCatCommonFileItemReader() {
        return new CommonFileItemReader<>(CafeCat.class);
    }


    @Bean
    @StepScope
    public CommonMybatisItemWriter catCommonMybatisItemWriter() {
         return new CommonMybatisItemWriter<>(sqlSessionFactory,Cat.class.getSimpleName());
    }
}

四、继承公共CommonProcesor

package com.sl.processor;

import com.sl.common.CommonProcessor;
import com.sl.entity.CafeCat;
import com.sl.entity.Cat;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.stereotype.Component;

/**
 * @author shuliangzhao
 * @Title: CatProcessor
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/10 20:13
 */
@Component
@StepScope
public class CatProcessor extends CommonProcessor {
    @Override
    public void processor(Cat o, CafeCat cafeCat) {
        o.setCataddress(cafeCat.getCataddress());
        o.setCatage(cafeCat.getCatage());
        o.setCatname(cafeCat.getCatname());
    }
}

五、执行job

执行完job,可以查看数据库已经有数据
SpringBatch读文件(FlatFileItemReader)写据库(MyBatisBatchItemWriter)(四)_第1张图片
以上例子就是读文件和写数据库的例子。

你可能感兴趣的:(java技能提升,springboot系列文章,springbatch)