代码如下
实体类
package com.springBatch.springBatch.PO;
public class CustomerPo {
private int id;
private String firsrName;
private String latname;
private String birthday;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getFirsrName() {
return firsrName;
}
public void setFirsrName(String firsrName) {
this.firsrName = firsrName;
}
public String getLatname() {
return latname;
}
public void setLatname(String latname) {
this.latname = latname;
}
public String getBirthday() {
return birthday;
}
public void setBirthday(String birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "CustomerPo{" +
"id=" + id +
", firsrName='" + firsrName + '\'' +
", latname='" + latname + '\'' +
", birthday='" + birthday + '\'' +
'}';
}
}
创建要读取文件
Job配置类
package com.springBatch.springBatch.config;
import com.springBatch.springBatch.PO.CustomerPo;
import org.springframework.batch.core.Job;
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.item.ItemWriter;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.mapping.DefaultLineMapper;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.validation.BindException;
@Configuration
public class FileItemReaderDemo {
/**
* 注入创建任务对象的对象
*/
@Autowired
private JobBuilderFactory jobBuilderFactory;
/**
* 任务的执行由 step 决定
* 注入创建 Step 对象 的工厂
*/
@Autowired
private StepBuilderFactory stepBuilderFactory;
/**
* 注入对象输入对象
*/
@Autowired
@Qualifier("flatfileWriter")
private ItemWriter<? super CustomerPo> firstItemWriter;
@Bean
public Job FileItemReaderDemoJob() {
return jobBuilderFactory.get("FileItemReaderDemoJob")
.start(FileItemReaderDemoStep()).build();
}
@Bean
public Step FileItemReaderDemoStep() {
return stepBuilderFactory.get("FileItemReaderDemoStep")
// 每读取两行为写一次
.<CustomerPo, CustomerPo>chunk(2)
.reader(firstItemReader())
.writer(firstItemWriter)
.build();
}
// 因为是读取文件 ItemReader 改用 FlatFileItemReader
@Bean
@StepScope
public FlatFileItemReader<CustomerPo> firstItemReader() {
FlatFileItemReader<CustomerPo> reader = new FlatFileItemReader<CustomerPo>();
// 告知reader所要读取问价的位置
reader.setResource(new ClassPathResource("file.txt"));
// 因为文件中的第一行为表头,想要跳过第一行读取数据
reader.setLinesToSkip(1);
// 解析数据
DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
// 指明表头字段
tokenizer.setNames("id","firsrName","latname","birthday");
// 把解析出的来的银一行数据映射为CustomerPo对象
DefaultLineMapper<CustomerPo> mapper = new DefaultLineMapper<>();
// 指明读取字段映射
mapper.setLineTokenizer(tokenizer);
mapper.setFieldSetMapper(new FieldSetMapper<CustomerPo>() {
@Override
public CustomerPo mapFieldSet(FieldSet fieldSet) throws BindException {
CustomerPo po = new CustomerPo();
po.setId(fieldSet.readInt("id"));
po.setFirsrName(fieldSet.readString("firsrName"));
po.setLatname(fieldSet.readString("latname"));
po.setBirthday(fieldSet.readString("birthday"));
return po;
}
});
// mapper内置校验方法
// Assert.notNull(this.tokenizer, "The LineTokenizer must be set");
// Assert.notNull(this.fieldSetMapper, "The FieldSetMapper must be set");
mapper.afterPropertiesSet();
reader.setLineMapper(mapper);
return reader;
}
}
Writer
package com.springBatch.springBatch.config;
import com.springBatch.springBatch.PO.CustomerPo;
import org.springframework.batch.item.ItemWriter;
import org.springframework.stereotype.Component;
import java.util.List;
@Component("flatfileWriter")
public class FlatfileWriter implements ItemWriter<CustomerPo> {
@Override
public void write(List<? extends CustomerPo> list) throws Exception {
System.out.println("****************************");
for (CustomerPo po : list) {
System.out.println(po);
}
System.out.println("****************************");
}
}
启动类
package com.springBatch.springBatch;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
@EnableBatchProcessing
public class SpringBatchApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBatchApplication.class, args);
}
}
运行