springbatch读取数据和写数据的配置

读取数据:

@Configuration
public class ItemReadConfig {
    @Autowired
    private DataSource dataSource;
    @Value("classpath:/file*.txt")
    private Resource[] fileResource;
    @Bean
    public JdbcPagingItemReader dbReader() {
        JdbcPagingItemReader reader = new JdbcPagingItemReader<>();
        reader.setDataSource(dataSource);
        reader.setFetchSize(1);
        reader.setRowMapper(new RowMapper() {
            @Override
            public Person mapRow(ResultSet resultSet, int i) throws SQLException {
                Person person = new Person();
                person.setId(resultSet.getInt(1));
                person.setName(resultSet.getString(2));
                person.setAge(resultSet.getInt(3));
                person.setPassword(resultSet.getString(4));
                person.setPerms(resultSet.getString(5));
                return person;
            }
        });
        //指定sql
        MySqlPagingQueryProvider provider = new MySqlPagingQueryProvider();
        provider.setSelectClause("id,name,age,password,perms");
        provider.setFromClause("person");
        //排序
        Map order = new HashMap<>(1);
        order.put("id", Order.DESCENDING);
        provider.setSortKeys(order);
        reader.setQueryProvider(provider);
        return reader;
    }
    @Bean
    public StaxEventItemReader xmlReader() throws ClassNotFoundException {
        StaxEventItemReader reader = new StaxEventItemReader<>();
        //路径
        reader.setResource(new ClassPathResource("person.xml"));
        //根标签
        reader.setFragmentRootElementName("person");
        //xml转对象
        XStreamMarshaller unmarshaller = new XStreamMarshaller();
        Map map = new HashMap<>();
        map.put("person", Person.class);
        unmarshaller.setAliases(map);
        reader.setUnmarshaller(unmarshaller);
        return reader;
    }
    @Bean
    public FlatFileItemReader fileReader() {
        FlatFileItemReader reader = new FlatFileItemReader<>();
        reader.setResource(new ClassPathResource("file2.txt"));
        //跳过行
        reader.setLinesToSkip(1);
        //解析数据
        DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer();
        tokenizer.setNames(new String[]{"id","name","age","password","perms"});
        //数据映射
        DefaultLineMapper mapper = new DefaultLineMapper<>();
        mapper.setLineTokenizer(tokenizer);
        mapper.setFieldSetMapper(new FieldSetMapper() {
            @Override
            public Person mapFieldSet(FieldSet fieldSet) throws BindException {
                Person person = new Person();
                person.setId(fieldSet.readInt(0));
                person.setName(fieldSet.readString(1));
                person.setAge(fieldSet.readInt(2));
                person.setPassword(fieldSet.readString(3));
                person.setPerms(fieldSet.readString(4));
                return person;
            }
        });
        mapper.afterPropertiesSet();
        reader.setLineMapper(mapper);
        return reader;
    }
    @Bean
    public MultiResourceItemReader multiFileReader() {
        MultiResourceItemReader reader = new MultiResourceItemReader<>();
        reader.setDelegate(fileReader());
        reader.setResources(fileResource);
        return reader;
    }
}

写数据:

@Configuration
public class ItemWriteConfig {
    @Autowired
    private DataSource dataSource;

    @Bean
    public JdbcBatchItemWriter jdbcItemWrite() {
        JdbcBatchItemWriter writer = new JdbcBatchItemWriter<>();
        writer.setDataSource(dataSource);
        writer.setSql("insert into person(id,name,age,password,perms) values(:id,:name,:age,:password,:perms)");
        writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>());
        return writer;
    }

    @Bean
    public FlatFileItemWriter fileWrite() throws Exception {
        FlatFileItemWriter writer = new FlatFileItemWriter<>();
        String path = "C:\\Users\\Administrator\\Desktop\\person.txt";
        //输出位置
        writer.setResource(new FileSystemResource(path));
        //转换
        writer.setLineAggregator(new LineAggregator() {
            @Override
            public String aggregate(Person person) {
                ObjectMapper om = new ObjectMapper();
                try {
                    return om.writeValueAsString(person);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                    return null;
                }
            }
        });
        writer.afterPropertiesSet();
        return writer;
    }
    @Bean
    public StaxEventItemWriter xmlWrite() throws Exception {
        StaxEventItemWriter writer = new StaxEventItemWriter<>();
        XStreamMarshaller marshaller = new XStreamMarshaller();
        Map map = new HashMap<>();
        map.put("person", Person.class);
        marshaller.setAliases(map);
        writer.setMarshaller(marshaller);
        writer.setRootTagName("persons");
        String path = "C:\\Users\\Administrator\\Desktop\\person.xml";
        writer.setResource(new FileSystemResource(path));
        writer.afterPropertiesSet();
        return writer;
    }
    @Bean
    public CompositeItemWriter multiFileWrite() throws Exception {
        CompositeItemWriter writer = new CompositeItemWriter<>();
        writer.setDelegates(Arrays.asList(fileWrite(),xmlWrite()));
        writer.afterPropertiesSet();
        return writer;
    }
    //分类
    @Bean
    public ClassifierCompositeItemWriter classifierMultiWriter(){
        ClassifierCompositeItemWriter writer = new ClassifierCompositeItemWriter<>();
        writer.setClassifier(new Classifier>() {
            @Override
            public ItemWriter classify(Person person) {
                ItemWriter itemWriter = null;
                try {
                    itemWriter = person.getId()%2==0?fileWrite():xmlWrite();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                return itemWriter;
            }
        });
        return writer;
    }
}

 

你可能感兴趣的:(spring,boot)