SpringBoot + SpringBatch + Mybatis + Scheduled 定时任务数据批处理

pom文件:

批处理包

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-batch</artifactId>
</dependency>

定时任务包

<dependency>
   <groupId>org.quartz-scheduler</groupId>
   <artifactId>quartz</artifactId>
</dependency>

启动类加载定时任务:

@EnableScheduling
@Bean
public SchedulerFactory schedulerFactory() {
    return new org.quartz.impl.StdSchedulerFactory();
}
@Bean
public Scheduler scheduler() throws SchedulerException {
    return schedulerFactory().getScheduler();
}

SpringBatch 三大核心:

1、读数据Reader;

2、处理数据Processor;

3、写数据Writer;

Reader:

import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;

public class BatchReader implements ItemReader<Object> {

    @Override
    public FlatFileItemReader<Music> read() throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        String filePath = "D:\\music.csv";

        FlatFileItemReader<Music> reader = new FlatFileItemReader<>();

        Resource resource = new FileSystemResource(filePath);

        reader.setResource(resource);

        BeanWrapperFieldSetMapper fieldSetMapper = new BeanWrapperFieldSetMapper();
        fieldSetMapper.setTargetType(Music.class);

        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setDelimiter(",");
        tokenizer.setNames("id", "com_code");

        DefaultLineMapper<Music> lineMapper = new DefaultLineMapper<>();
        lineMapper.setFieldSetMapper(fieldSetMapper);
        lineMapper.setLineTokenizer(tokenizer);

        reader.setLineMapper(lineMapper);

        return reader;
    }
}

Processor:

import org.springframework.batch.item.ItemProcessor;

/**
 *   处理业务数据
 */
public class BatchProcessor implements ItemProcessor<Music, Music> {
    @Override
    public Music process(Music item) throws Exception {
        System.out.println("in [BatchProcessor], processing credit bill ... ");
        return item;
    }

}

Writer:

import ins.framework.utils.Beans;
import ins.platform.part.partloc.dao.BrandToModelDao;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 *   批量写入业务数据
 */
public class BatchWriter implements ItemWriter<Music> {

    @Autowired
    BrandToModelDao brandToModelDao;

    @Override
    public void write(List<? extends Music> items) throws Exception {
        List<Music> aaMusics = Beans.copyDepth().from(items).toList(Music.class);
        brandToModelDao.batchSave(aaMusics);
    }
}

配置类:

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.item.ItemWriter;
import org.springframework.batch.item.NonTransientResourceException;
import org.springframework.batch.item.ParseException;
import org.springframework.batch.item.UnexpectedInputException;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;



@Configuration
@EnableBatchProcessing // 开启批处理的支持
public class BatchConfig {
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;


    /** 读取业务数据 */
    @Bean
    public FlatFileItemReader<Music> batchReader()throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException {
        return new BatchReader().read();
    }

    /** 处理业务数据 */
    @Bean
    public BatchProcessor createProcessor(){
        return new BatchProcessor();
    }

    /** 批量写入业务数据 */
    @Bean
    public ItemWriter<Music> batchWriter(){
        return new BatchWriter();
    }

    /**
     *  将数据写入数据库
     */
    @Bean
    public Job createJob()throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException{
        return jobBuilderFactory.get("Job").start(createStep()).build();
    }

    @Bean
    public Step createStep()throws Exception, UnexpectedInputException, ParseException, NonTransientResourceException{
        return stepBuilderFactory.get("Step")
                .<Music, Music>chunk(2)//每次读取条数
                .reader(batchReader())//读取数据
                .processor(createProcessor())//处理数据
                .writer(batchWriter())//写数据
                .build();
    }

}


实体类:

import lombok.Data;

import java.io.Serializable;

@Data
public class Music implements Serializable {
   private static final long serialVersionUID = 1L;

   private Long id;
   private String comCode;
}

定时任务:

/**
 *  批处理
 */
@Async
@Scheduled(cron="*/5 * 0-23 * * ?")
public void batchData() {
   log.info("批处理定时任务开始");

   try {
        log.info("IP = {},获取到分布式锁,开始执行任务:{}", ip);

         try{
            JobParametersBuilder paramBuilder = new JobParametersBuilder();
            paramBuilder.addLong("DS", System.currentTimeMillis());
            JobExecution jobExecution = launcher.run(importJob, paramBuilder.toJobParameters());
            System.out.println("JobExecution : " + jobExecution.toString());
         } catch (Exception e){
            e.printStackTrace();
         }
   } catch (Exception e) {
      log.error("IP = {},获取到分布式锁,执行任务:{}失败,原因:{}", ip, e);
   } finally {
      // 释放共享锁
      //stringRedisTemplate.delete(autoJobKey);
      log.info("批处理定时任务结束");
   }
}

xml:

<insert id="batchSave" useGeneratedKeys="true" keyProperty="id" parameterType="map">
   INSERT INTO music
   (id, com_code)
   VALUES
   <foreach collection="list" item="item" index="index" separator=",">
      (#{item.id},#{item.comCode})
   </foreach>
</insert>

dao:

int batchSave(@Param("list")List<Music> list);

yml配置:

spring.batch.initialize-schema = always
spring.batch.job.enabled = false

  • 0
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于搭建一个使用Spring BootSpring BatchMyBatis和MySQL的项目,您可以按照以下步骤进行操作: 1. 创建一个Spring Boot项目:您可以使用Spring Initializr(https://start.spring.io/)或者在IDE中创建一个新的Spring Boot项目。 2. 添加所需的依赖:在项目的pom.xml文件中添加以下依赖: ```xml <dependencies> <!-- Spring Boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- Spring Batch --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency> <!-- MyBatis --> <dependency> <groupId>org.mybatis.spring.boot</groupId> <artifactId>mybatis-spring-boot-starter</artifactId> </dependency> <!-- MySQL Connector --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> </dependencies> ``` 3. 配置数据源:在application.properties或application.yaml文件中配置MySQL数据库连接信息,例如: ```yaml spring: datasource: url: jdbc:mysql://localhost:3306/mydatabase username: your-username password: your-password ``` 4. 创建数据库表:使用MySQL客户端或其他工具创建您需要的数据库表。 5. 创建实体类和Mapper接口:根据您的数据表结构创建对应的Java实体类,并使用MyBatis的注解或XML配置文件创建Mapper接口。 6. 创建Spring Batch的Job配置:创建一个继承自org.springframework.batch.core.configuration.annotation.JobConfiguration的配置类,在其中定义Batch Job的步骤和任务。 7. 编写Batch Job的业务逻辑:根据需求在Batch Job的步骤中编写相应的业务逻辑,例如读取数据、处理数据、写入数据等。 8. 运行Batch Job:在应用程序中启动Batch Job,可以通过命令行、定时任务或其他方式触发。 以上是一个简单的搭建步骤,您可以根据具体需求进行更详细的配置和开发。希望能对您有所帮助!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值