Spring Batch 集成 MyBatis,durid 连接池兼容MyBatisCursorItemReader 存在bug, 选用其他连接池


import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.batch.MyBatisCursorItemReader;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameter;
import org.springframework.batch.core.Step;
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.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemStreamReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Scope;
import org.springframework.context.annotation.ScopedProxyMode;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName readByMybatisJob
 * @Description: 读取数据来自mybtatis from reader
 * @Author 
 * @Date 2020/5/29 9:54
 * @Version V1.0
 **/
@Slf4j
@Component
public class ReadByMybatisJob {

    @Autowired
    private SqlSessionFactory sqlSessionFactory;

    @Autowired
    protected JobBuilderFactory jobBuilderFactory;

    @Autowired
    protected StepBuilderFactory stepBuilderFactory;

    @Bean
    @StepScope
    public ItemStreamReader myBatisCursorItemDemoReader(@Value("#{jobParameters[operationTypeId]}") String operationTypeId, @Value("#{jobParameters[messageType]}") String messageType) {
        MyBatisCursorItemReader reader = new MyBatisCursorItemReader();
        Map parameterValues = new HashMap<>();
        parameterValues.put("operationTypeId", operationTypeId);
        parameterValues.put("messageType", messageType);

        reader.setSqlSessionFactory(sqlSessionFactory);
        reader.setParameterValues(parameterValues);
        reader.setQueryId("*.mapper.BatchMessageFieldMapper.getBatchMessageFields");
        return reader;
    }


    /***
     * 公共写demo
     * @methodName myBatisCursorItemWriter
     * @param
     * @return org.springframework.batch.item.ItemWriter
     * @author 
     * @date 2020/5/29 9:38
     * @throws
     */
    @Bean("myBatisCursorItemWriter")
    public ItemWriter myBatisCursorItemWriter() {
        CommonFileItemWriter commonFileItemWriter = new CommonFileItemWriter(BatchMessageField.class);
        return commonFileItemWriter;
    }


    /**
     * 定义Job
     *
     * @param jobBuilderFactory
     * @param myBatisCursorStep 步骤
     * @return
     */
    @Bean("myBatisCursorDemoJob")
    public Job readJsonFileDemoJob(JobBuilderFactory jobBuilderFactory, Step myBatisCursorStep) {
        return jobBuilderFactory.get("myBatisCursorDemoJob")
                .incrementer(parameters -> {
                    Map parameterMap = parameters.getParameters();
                    parameterMap.put("key", new JobParameter(UUID.randomUUID().toString()));
                    return parameters;
                }).start(myBatisCursorStep).build();

    }


    /**
     * step 配置
     *
     * @param
     * @param myBatisCursorItemDemoReader 读bean
     * @param myBatisCursorItemWriter     写bean
     * @return
     */
    @Bean("myBatisCursorStep")
    public Step myBatisCursorStep( ItemReader myBatisCursorItemDemoReader, @Qualifier("myBatisCursorItemWriter") ItemWriter myBatisCursorItemWriter) {
       return  stepBuilderFactory.get("myBatisCursorStep")
                .chunk(1)
                .reader(myBatisCursorItemDemoReader)
                // .processor(processor)
                .writer(myBatisCursorItemWriter)
                .build();

    }


}

 

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