SpringBatch读数据库(MyBatisPagingItemReader)(三)

文章目录

    • 一、抽取顺序读取数据库公共reader
    • 二、封装实体
    • 三、mapper配置文件
    • 四、配置文件application.properties
    • 五、读取数据库job
    • 六、继承公共CommonProcesor
    • 七、执行job

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

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

SpringBatch其它文章直通车:

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

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

CommonMybatisItemReader继承MyBatisPagingItemReader

package com.sl.common;

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

/**
 * @author shuliangzhao
 * @Title: CommonMybatisItemReader
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/8 14:14
 */
public class CommonMybatisItemReader extends MyBatisPagingItemReader {

    public CommonMybatisItemReader(SqlSessionFactory sqlSessionFactory,String name) {
        setSqlSessionFactory(sqlSessionFactory);
        setQueryId("com.sl.entity."+name+"selectList");
        setPageSize(100);
    }
}

二、封装实体

读取数据库映射实体类Cat

package com.sl.entity;

/**
 * @author shuliangzhao
 * @Title: Cat
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/8 14:29
 */
public class Cat {

    private Integer id;

    private String catname;

    private String catage;

    private String cataddress;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public String getCatname() {
        return catname;
    }

    public void setCatname(String catname) {
        this.catname = catname;
    }

    public String getCatage() {
        return catage;
    }

    public void setCatage(String catage) {
        this.catage = catage;
    }

    public String getCataddress() {
        return cataddress;
    }

    public void setCataddress(String cataddress) {
        this.cataddress = cataddress;
    }
}

process返回封装实体

package com.sl.entity;

/**
 * @author shuliangzhao
 * @Title: CafeCat
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/8 14:36
 */
public class CafeCat {

    private String catname;

    private String catage;

    private String cataddress;


    public String getCatname() {
        return catname;
    }

    public void setCatname(String catname) {
        this.catname = catname;
    }

    public String getCatage() {
        return catage;
    }

    public void setCatage(String catage) {
        this.catage = catage;
    }

    public String getCataddress() {
        return cataddress;
    }

    public void setCataddress(String cataddress) {
        this.cataddress = cataddress;
    }
}

三、mapper配置文件





    
    

        
        
        
        

    



    




四、配置文件application.properties

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost/chapter01?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=root


mybatis.type-aliases-package=com.sl.entity
mybatis.mapperLocations=classpath:mapper/*.xml

五、读取数据库job

package com.sl.config;

import com.sl.common.CommonFileItemWriter;
import com.sl.common.CommonMybatisItemReader;
import com.sl.entity.CafeCat;
import com.sl.entity.Cat;
import com.sl.processor.CafeCatProcessor;
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: UserConfiguration
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/7 17:06
 */
@Configuration
@EnableBatchProcessing
public class CatConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private CafeCatProcessor cafeCatProcessor;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

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

    @Bean
    public Step catStep() {
        return stepBuilderFactory.get("catStep")
                .chunk(10)
                .reader(catCommonMybatisItemReader())
                .processor(cafeCatProcessor)
                .writer(cafeCatCommonFileItemWriter())
                .build();
    }

    @Bean
    @StepScope
    public CommonMybatisItemReader catCommonMybatisItemReader() {
        return new CommonMybatisItemReader(sqlSessionFactory,Cat.class.getSimpleName());
    }

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

}

六、继承公共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: CafeCatProcessor
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/8 14:36
 */
@Component
@StepScope
public class CafeCatProcessor extends CommonProcessor {
    @Override
    public void processor(CafeCat o, Cat cat) {
        o.setCatname(cat.getCatname());
        o.setCatage(cat.getCatage());
        o.setCataddress(cat.getCataddress());
    }
}

七、执行job

运行完job,结果如图:
在这里插入图片描述
以上就是SpringBatch读取数据库写文件的全部过程。

你可能感兴趣的:(数据库,springboot系列文章,springbatch)