需求:将txt文件内容解析 所有地区相关信息汇总拼接到User对象的address里
文件:users2.txt
1#dadfei#18#广东#广州#天河区
2#erfei#19#广东#广州#天河区
3#sanfei#17#广东#广州#天河区
4#sifei#16#广东#广州#天河区
5#wufei#15#广东#广州#天河区
对象:User.class
@Data
public class User {
private Long id;
private String name;
private int age;
private String address;
}
解决:采用字段映射方式
实现:FieldSetMapper
public interface FieldSetMapper<T> {
T mapFieldSet(FieldSet var1) throws BindException;
}
在当前场景下 上述代码块的泛型T是User
上述代码块的 FieldSet 可以理解为手动封装数据
封装示例:
package com.springBatch.demo.flatFileReaderJob;
import org.springframework.batch.item.file.mapping.FieldSetMapper;
import org.springframework.batch.item.file.transform.FieldSet;
import org.springframework.validation.BindException;
//将解析出来的数据进行自定义封装
public class UserFieldSetMapper implements FieldSetMapper<User> {
@Override
public User mapFieldSet(FieldSet fieldSet) throws BindException {
//获取解析的数据
Long id = fieldSet.readLong("id");
String name = fieldSet.readString("name");
int age = fieldSet.readInt("age");
//对解析的数据进行拼接
String address = fieldSet.readString("province") + fieldSet.readString("city") + fieldSet.readString("area");
//将数据封装为对象返回
User user = new User();
user.setId(id);
user.setName(name);
user.setAge(age);
user.setAddress(address);
return user;
}
}
实现代码:
package com.springBatch.demo.flatFileReaderJob;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.*;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.batch.item.file.builder.FlatFileItemReaderBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import java.util.List;
@EnableBatchProcessing
@SpringBootApplication
public class FlatReaderJob {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
// first job--step--chunk--itemreader --writer
@Bean
public ItemWriter<User> itemWriter() {
return new ItemWriter<User>() {
@Override
public void write(List<? extends User> items) throws Exception {
items.forEach(System.out::println);
}
};
}
//这里将封装注入
@Bean
public UserFieldSetMapper fieldSetMapper() {
return new UserFieldSetMapper();
}
@Bean
public FlatFileItemReader<User> itemReader() {
return new FlatFileItemReaderBuilder<User>()
.name("user-itemReader")
.resource(new ClassPathResource("users2.txt"))
.delimited().delimiter("#")
.names("id","name","age","province","city","area")
// .targetType(User.class) //自动封装
.fieldSetMapper(fieldSetMapper())
.build();
}
@Bean
public Step step() {
return stepBuilderFactory.get("step1")
.<User,User>chunk(1)
.reader(itemReader())
.writer(itemWriter())
.build();
}
@Bean
public Job job() {
return jobBuilderFactory.get("flat-reader-job")
.start(step())
.build();
}
public static void main(String[] args) {
SpringApplication.run(FlatReaderJob.class, args);
}
}
结果:
User(id=1, name=dadfei, age=18, address=广东广州天河区)
User(id=2, name=erfei, age=19, address=广东广州天河区)
User(id=3, name=sanfei, age=17, address=广东广州天河区)
User(id=4, name=sifei, age=16, address=广东广州天河区)
User(id=5, name=wufei, age=15, address=广东广州天河区)