Set up the Spring Batch project
1. Annotation @enablebatchProcessing: Use this annotation to perform batch processing
2. Dependencies of Spring Batch:
<dependency>
<groupId> org.springframework.boot</groupId>
<artifactId>spring-boot-start-batch</artifactId>
</dependency>
Copy the code
Create a Job
1. Inject the object that created the task object
private JobBuilderFactory jobBuilderFactory;
2. Inject the object that created the Step object
private StepBuilderFactory stepBuilderFactory;
3. Create a task object
Create multiple task objects:
A. The first method
@Bean
public Job ****Job(){// Specify what step the task executesreturn jobBuilderFactory.get("****Job")
.start(step1())
.next(step2())
.next(step3())
.build();
}
Copy the code
B. In the second method, according to the different states returned by each step operation, decide whether to enter the next step or carry out other processing processes. :
@Bean
public Job ****Job(){// Specify what step the task executesreturn jobBuilderFactory.get("****Job")
.start(step1())
.on("completed").to(step2())
.from(step2()).on("completed").to(step3()) // fail() stopAndRestart
.from(step3()).end()
.build();
}
Copy the code
Create a step object:
@Bean
public Step step1() {return stepBuilderFactory.get("step1")
.tasklet(new Tasklet() {
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("Hello Spring Batch");
return RepeatStatus.FINISHED;
}
}).build();
}
Copy the code
4. Replace the Spring Batch database with the mysql data source
SQL > alter table spring Batch job; Demo using mysql # spring. The datasource. Schema = classpath: / org/springframework/batch/core/schema – mysql. SQL spring.jpa.database-platform=org.hibernate.dialect.MySQLDialect
#spring.batch.initialize-schema=always
Job #spring.batch.job. Enabled = false
<br/><br/><br/>
Copy the code