SpringBatch 写xml文件(StaxEventItemWriter)用法(十四)

前言:在一些业务场景中,可能需要读取xml文件,做业务逻辑处理,SpringBatch已经帮我们封装好了读取xml的reader

SpringBatch其它文章直通车:

代码已上传GitHub上面地址:git源码地址

一、pom文件引入需要读取xml文件jar包

<dependency>
            <groupId>com.thoughtworks.xstream</groupId>
            <artifactId>xstream</artifactId>
            <version>1.4.11.1</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/org.springframework/spring-oxm -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
            <version>5.1.9.RELEASE</version>
        </dependency>

二、抽取写xml文件公共writer

输出与输入是对称的。StaxEventItemWriter需要一个资源、一个编组程序和一个rootTagName。Java对象被传递给编组程序(通常是标准的Spring OXM编组程序),编组程序使用自定义事件编写器来过滤OXM工具为每个片段生成的StartDocument和EndDocument事件,从而向资源写入。

package com.sl.common;

import org.springframework.batch.item.xml.StaxEventItemWriter;
import org.springframework.core.io.FileSystemResource;
import org.springframework.oxm.Marshaller;

/**
 * 公共写xml文件
 * @author shuliangzhao
 * @Title: CommonXmlWriter
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/20 18:46
 */
public class CommonXmlWriter<T> extends StaxEventItemWriter<T> {


    public CommonXmlWriter(Marshaller marshaller,Class clz) {
        setName("commonXmlWriter");
        setMarshaller(marshaller);
        setResource(new FileSystemResource("D:\\aplus\\shuqian\\source\\"+ clz.getSimpleName() + ".xml"));
        setRootTagName("cat");
        setOverwriteOutput(true);
    }
}

前面的配置设置了三个必需的属性,并设置了可选的overwriteOutput=true属性,该属性在本章前面提到,用于指定是否可以覆盖现有文件。配置编辑器

 @Bean
    public XStreamMarshaller tradeXmlMarshaller() {
        Map<String, Class> aliases = new HashMap<>();
        aliases.put("cat", Cat.class);
        aliases.put("id", Integer.class);
        aliases.put("catname", String.class);
        aliases.put("catage", String.class);
        aliases.put("cataddress", String.class);

        XStreamMarshaller marshaller = new XStreamMarshaller();

        marshaller.setAliases(aliases);
        return marshaller;
    }

三、processor

package com.sl.processor;

import com.sl.common.CommonProcessor;
import com.sl.entity.CafeCat;
import com.sl.entity.Cat;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.stereotype.Component;

/**
 * @author shuliangzhao
 * @Title: CatProcessor
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/10 20:13
 */
@Component
@StepScope
public class CatProcessor extends CommonProcessor<CafeCat, Cat> {
    @Override
    public void processor(Cat o, CafeCat cafeCat) {
        o.setCataddress(cafeCat.getCataddress());
        o.setCatage(cafeCat.getCatage());
        o.setCatname(cafeCat.getCatname());
    }
}

四、配置写xml文件job

package com.sl.config;

import com.sl.common.CommonFileItemReader;
import com.sl.common.CommonXmlWriter;
import com.sl.entity.CafeCat;
import com.sl.entity.Cat;
import com.sl.processor.CatProcessor;
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.core.configuration.annotation.StepScope;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.oxm.xstream.XStreamMarshaller;

import java.util.HashMap;
import java.util.Map;

/**
 * 写出xml文件
 * @author shuliangzhao
 * @Title: CafeXmlWriterConfiguration
 * @ProjectName spring-boot-learn
 * @Description: TODO
 * @date 2019/9/20 18:50
 */
@Configuration
@EnableBatchProcessing
public class CafeXmlWriterConfiguration {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Autowired
    private CatProcessor catProcessor;

    @Bean
    public Job cafeXmlCatJob() {
        return jobBuilderFactory.get("cafeXmlCatJob")
                .start(cafeXmlCatStep())
                .build();
    }

    @Bean
    public Step cafeXmlCatStep() {
        return stepBuilderFactory.get("cafeXmlCatStep")
                .<CafeCat, Cat>chunk(10)
                .reader(cafeCatXmlCommonFileItemReader())
                .processor(catProcessor)
                .writer(commonXmlWriter())
                .build();
    }

    @Bean
    @StepScope
    public CommonFileItemReader<CafeCat> cafeCatXmlCommonFileItemReader() {
        return new CommonFileItemReader<>(CafeCat.class);
    }

    @Bean
    @StepScope
    public CommonXmlWriter commonXmlWriter() {
        return new CommonXmlWriter(tradeXmlMarshaller(),Cat.class);
    }

    @Bean
    public XStreamMarshaller tradeXmlMarshaller() {
        Map<String, Class> aliases = new HashMap<>();
        aliases.put("cat", Cat.class);
        aliases.put("id", Integer.class);
        aliases.put("catname", String.class);
        aliases.put("catage", String.class);
        aliases.put("cataddress", String.class);

        XStreamMarshaller marshaller = new XStreamMarshaller();

        marshaller.setAliases(aliases);
        return marshaller;
    }

}

五、执行job

写出xml文件:
在这里插入图片描述


版权声明:本文为TreeShu321原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。