1. maven
org.springframework.batch
spring-batch-core
3.0.8.RELEASE
org.springframework.batch
spring-batch-infrastructure
3.0.8.RELEASE
2. BatchTest类
public class BatchTest {
@Test
public void test() throws Exception {
ResourcelessTransactionManager transactionManager = new ResourcelessTransactionManager();
MapJobRepositoryFactoryBean jobRepositoryFactoryBean = new MapJobRepositoryFactoryBean();
jobRepositoryFactoryBean.setTransactionManager(transactionManager);
JobRepository jobRepository = jobRepositoryFactoryBean.getObject();
SimpleJobLauncher jobLauncher = new SimpleJobLauncher();
jobLauncher.setJobRepository(jobRepository);
SyncTaskExecutor taskExecutor = new SyncTaskExecutor();
jobLauncher.setTaskExecutor(taskExecutor);
StepBuilderFactory stepBuilderFactory = new StepBuilderFactory(jobRepository,transactionManager);
Step step = personStep(stepBuilderFactory,reader("person.csv")
,new PersonWriter(), new PersonItemProcessor());
JobBuilderFactory jobBuilderFactory = new JobBuilderFactory(jobRepository);
Job job = jobBuilderFactory.get("job")
.start(step)
.build();
jobLauncher.run(job,new JobParameters());
}
public ItemReader reader(String inputFile){
if(inputFile == null)
return null;
FlatFileItemReader reader = new FlatFileItemReader<>();
reader.setResource(new ClassPathResource(inputFile));
reader.setLineMapper(lineMapper());
reader.setLinesToSkip(1);
ExecutionContext executionContext = new ExecutionContext();
return reader;
}
public Step personStep(StepBuilderFactory stepBuilderFactory, ItemReader reader,
ItemWriter writer, ItemProcessor processor){
return stepBuilderFactory.get("personStep")
.chunk(1)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
public LineMapper lineMapper(){
DefaultLineMapper lineMapper = new DefaultLineMapper<>();
DelimitedLineTokenizer lineTokenizer = new DelimitedLineTokenizer();
lineTokenizer.setDelimiter(",");
lineTokenizer.setStrict(false);
lineTokenizer.setNames(new String[]{"name","age"});
BeanWrapperFieldSetMapper fieldSetMapper = new BeanWrapperFieldSetMapper<>();
fieldSetMapper.setTargetType(Person.class);
lineMapper.setLineTokenizer(lineTokenizer);
lineMapper.setFieldSetMapper(new PersonFieldSetMapper());
return lineMapper;
}
}
3. PersonWriter类
public class PersonWriter implements ItemWriter {
@Override
public void write(List extends Person> list) throws Exception {
//TODO 插入数据库
for(Person person:list){
System.out.println(person);
}
}
}
4. PersonItemProcessor
public class PersonItemProcessor implements ItemProcessor {
public String inputFile;
public PersonItemProcessor(){}
public PersonItemProcessor(String inputFile){
this.inputFile = inputFile;
}
@Override
public Person process(Person person) {
System.out.println("handle person " + person.getName());
return person;
}
}
5. PersonFieldSetMapper类
public class PersonFieldSetMapper implements FieldSetMapper {
@Override
public Person mapFieldSet(FieldSet fieldSet) throws BindException {
String name = fieldSet.readRawString("name");
int age = fieldSet.readInt("age");
Person person = new Person();
person.setName(name);
person.setAge(age);
return person;
}
}
6. person.csv
name,age
yaoming,33