美文网首页
spring batch -->将读取的文件存入数据库

spring batch -->将读取的文件存入数据库

作者: 刘小刀tina | 来源:发表于2020-04-28 09:47 被阅读0次
/**
 * @program: demo-spring-batch
 * @description:  将读取的数据写入mysql数据库
 * @author: tina.liu
 * @create: 2020-04-28 09:05
 **/
@Configuration
@EnableBatchProcessing
public class ItemWriter implements org.springframework.batch.item.ItemWriter<User> {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    @Qualifier(value = "flatFileReader")
    private ItemReader<User> flatFileReader;

    @Autowired
    @Qualifier(value = "itemWriterDb")
    private JdbcBatchItemWriter itemWriterDb;
//    private org.springframework.batch.item.ItemWriter<User> itemWriterDb;

    @Bean(value = "itemWriterJob")
    public Job itemWriterJob(){
        return jobBuilderFactory.get("itemWriterJob")
                .start(itemWriterStep())
                .build();
    }

    @Bean(value = "itemWriterStep")
    public Step itemWriterStep() {

        return stepBuilderFactory.get("itemWriterStep")
                .<User,User>chunk(2)
                .reader(flatFileReader)
                .writer(itemWriterDb)
                .build();
    }

    @Override
    public void write(List<? extends User> list) throws Exception {

    }
}


// 创建一个实体类
@Data
 class User {
    private String id;
    private String name;
    private String age;
}


//创建一个类reader
@Configuration
class FlatFileReaderConfig {
    @Bean(value = "flatFileReader")
    public FlatFileItemReader<User>  flatFileReader() {
        FlatFileItemReader<User> reader = new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("user.csv")); //指定文件名
        reader.setLinesToSkip(1);// 跳过第一行

        //开始解析数据
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames(new String[]{"id", "name", "age"});
        //把解析出的一行数据映射为user对象
        DefaultLineMapper<User> mapper = new DefaultLineMapper<>();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(new FieldSetMapper<User>() {
            @Override
            public User mapFieldSet(FieldSet fieldSet) throws BindException {
                User user = new User();
                user.setId(fieldSet.readString("id"));
                user.setName(fieldSet.readString("name"));
                user.setAge(fieldSet.readString("age"));
                return user;
            }
        });
        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);
        return reader;
    }
}


//创建一个类writer
@Configuration
class ItemWriterDbConfig{

    @Autowired
    private DataSource dataSource;

    @Bean(value = "itemWriterDb")
    public JdbcBatchItemWriter itemWriterDb(){
        JdbcBatchItemWriter writer = new JdbcBatchItemWriter();
        writer.setDataSource(dataSource);
        writer.setSql("insert into t_user (id,name,age) values (:id,:name,:age)");
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
        System.out.println("实现向数据库返回对象");
        return writer;
    }
}




相关文章

网友评论

      本文标题:spring batch -->将读取的文件存入数据库

      本文链接:https://www.haomeiwen.com/subject/zlgmwhtx.html