Spring Batch是一个开源 的、全面的、轻量级的批处理框架,通过Spring Boot 可以实现强大的批处理应用程序开发。
Spring Batch提供了ItemReader、ItemProcessor和ItemWriter来完成数据的读取、处理及写出操作,并且可以将批处理的执行状态 持久化到数据库中。
https://spring.io/projects/spring-batch
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
2.添加配置 信息
server.port=9000
spring.http.encoding.force-response=true
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.url=jdbc:mysql:///batch
spring.datasource.username = root
spring.datasource.password = kangxg198811
#项目启动时创建数据表的SQL脚本
spring.datasource.schema=classpath:/org/springframework/batch/core/schema-mysql.sql
#项目启动时执行建表SQL
spring.batch.initialize-schema=always
#配置后不会自动执行
spring.batch.job.enabled=false
3.在项目启动类上添加@EnableBatchProcessing注解开开启Spring Batch支持
@SpringBootApplication
@EnableBatchProcessing
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
}
4.配置批处理
@Configuration
public class CsvBatchJobConfig {
@Autowired
JobBuilderFactory jobBuilderFactory;
@Autowired
StepBuilderFactory stepBuilderFactory;
@Autowired
DataSource dataSource;
class CustomLineCallbackHandler implements LineCallbackHandler{
@Override
public void handleLine(String result)
{
System.out.println(result);
}
}
@Bean
ItemReader<User> itemReader()
{
FlatFileItemReader<User> reader = new FlatFileItemReader<User>();
reader.setLinesToSkip(1);
reader.setResource(new ClassPathResource("data.csv"));
reader.setLineMapper(new DefaultLineMapper<User>(){
{
setLineTokenizer(new DelimitedLineTokenizer(){{
setNames(new String[]{"id","username","address","gender"});
//setDelimiter("\t");
}});
setFieldSetMapper(new BeanWrapperFieldSetMapper<User>(){{
setTargetType(User.class);
}});
}
});
reader.setSkippedLinesCallback(new CustomLineCallbackHandler());
return reader;
}
@Bean
JdbcBatchItemWriter jdbcBatchItemWriter()
{
JdbcBatchItemWriter writer = new JdbcBatchItemWriter();
writer.setDataSource(dataSource);
writer.setSql("insert into user(id,username,address,gender)" + "values(:id,:username,:address,:gender)");
writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider());
return writer;
}
@Bean
Step csvStep()
{
return stepBuilderFactory.get("csvStep")
.<User,User>chunk(2)
.reader(itemReader())
.writer(jdbcBatchItemWriter())
.build();
}
@Bean
Job csvJob()
{
return jobBuilderFactory.get("csvJob")
.start(csvStep())
.build();
}
}
5.实体类
public class User {
private Integer id;
private String username;
private String address;
private String gender;
public User(Integer id,String username,String address,String gender)
{
this.username= username;
this.id = id;
this.address = address;
this.gender = gender;
}
public User()
{
super();
}
public void setId(Integer id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
public void setGender(String gender) {
this.gender = gender;
}
public void setAddress(String address) {
this.address = address;
}
public String getUsername() {
return username;
}
public String getGender() {
return gender;
}
public Integer getId() {
return id;
}
public String getAddress() {
return address;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", address='" + address + '\'' +
", gender='" + gender + '\'' +
'}';
}
}
6.创建Controller
@RestController
public class BatchController {
@Autowired
JobLauncher jobLauncher;
@Autowired
Job job;
@GetMapping("/batch")
public void batch()
{
try {
jobLauncher.run(job,new JobParametersBuilder().toJobParameters());
}catch (Exception e)
{
e.printStackTrace();
}
}
}
这里配置就结束了,数据库会增加一些自动创建到表,但是User表不能自动创建和字段需要提前设置,否则会出现错误,还需要进一步处理,待完成。暂做标记!