Spring Batch Hello World

原创转载请注明出处:https://www.cnblogs.com/agilestyle/p/11995146.html

 

Project Directory

 

Maven Dependency

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 3          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
 4     <modelVersion>4.0.0</modelVersion>
 5     <parent>
 6         <groupId>org.springframework.boot</groupId>
 7         <artifactId>spring-boot-starter-parent</artifactId>
 8         <version>2.2.1.RELEASE</version>
 9         <relativePath/>
10     </parent>
11 
12     <groupId>org.fool.springbatch</groupId>
13     <artifactId>hello-springbatch</artifactId>
14     <version>0.0.1-SNAPSHOT</version>
15     <name>hello-springbatch</name>
16 
17     <properties>
18         <java.version>1.8</java.version>
19     </properties>
20 
21     <dependencies>
22         <dependency>
23             <groupId>org.springframework.boot</groupId>
24             <artifactId>spring-boot-starter-batch</artifactId>
25         </dependency>
26 
27         <dependency>
28             <groupId>org.springframework.boot</groupId>
29             <artifactId>spring-boot-starter-jdbc</artifactId>
30         </dependency>
31 
32         <dependency>
33             <groupId>mysql</groupId>
34             <artifactId>mysql-connector-java</artifactId>
35         </dependency>
36 
37         <dependency>
38             <groupId>org.projectlombok</groupId>
39             <artifactId>lombok</artifactId>
40         </dependency>
41 
42         <dependency>
43             <groupId>org.springframework.boot</groupId>
44             <artifactId>spring-boot-starter-test</artifactId>
45             <scope>test</scope>
46             <exclusions>
47                 <exclusion>
48                     <groupId>org.junit.vintage</groupId>
49                     <artifactId>junit-vintage-engine</artifactId>
50                 </exclusion>
51             </exclusions>
52         </dependency>
53         <dependency>
54             <groupId>org.springframework.batch</groupId>
55             <artifactId>spring-batch-test</artifactId>
56             <scope>test</scope>
57         </dependency>
58     </dependencies>
59 
60     <build>
61         <plugins>
62             <plugin>
63                 <groupId>org.springframework.boot</groupId>
64                 <artifactId>spring-boot-maven-plugin</artifactId>
65             </plugin>
66         </plugins>
67     </build>
68 
69 </project>
View Code

 

application.properties

1 spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
2 spring.datasource.url=jdbc:mysql://localhost:3306/springbatch?useUnicode=true&characterEncoding=utf8&useSSL=false
3 spring.datasource.username=root
4 spring.datasource.password=123456
5 spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
6 spring.batch.initialize-schema=always

 

Source Code

Application.java

 1 package org.fool.springbatch;
 2 
 3 import org.springframework.boot.SpringApplication;
 4 import org.springframework.boot.autoconfigure.SpringBootApplication;
 5 
 6 @SpringBootApplication
 7 public class Application {
 8 
 9     public static void main(String[] args) {
10         SpringApplication.run(Application.class, args);
11     }
12 
13 }
View Code

 

JobConfiguration.java

 1 package org.fool.springbatch.config;
 2 
 3 import lombok.extern.slf4j.Slf4j;
 4 import org.springframework.batch.core.Job;
 5 import org.springframework.batch.core.Step;
 6 import org.springframework.batch.core.StepContribution;
 7 import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
 8 import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
 9 import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
10 import org.springframework.batch.core.scope.context.ChunkContext;
11 import org.springframework.batch.core.step.tasklet.Tasklet;
12 import org.springframework.batch.repeat.RepeatStatus;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.context.annotation.Bean;
15 import org.springframework.context.annotation.Configuration;
16 
17 @Configuration
18 @EnableBatchProcessing
19 @Slf4j
20 public class JobConfiguration {
21 
22     @Autowired
23     private JobBuilderFactory jobBuilderFactory;
24 
25     @Autowired
26     private StepBuilderFactory stepBuilderFactory;
27 
28     @Bean
29     public Job helloWorldJob() {
30         return jobBuilderFactory.get("helloWorldJob").start(step1()).build();
31     }
32 
33     @Bean
34     public Step step1() {
35         return stepBuilderFactory.get("step1").tasklet(new Tasklet() {
36             @Override
37             public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
38                 log.info("Hello World");
39                 return RepeatStatus.FINISHED;
40             }
41         }).build();
42     }
43 }

 

Run

查看DB自动生成的相关Schema 

 

 

 

 

posted @ 2019-12-06 14:41  李白与酒  阅读(410)  评论(0编辑  收藏  举报