當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBatch 写xml文件(StaxEventItemWriter)用法(十四)
生活随笔
收集整理的這篇文章主要介紹了
SpringBatch 写xml文件(StaxEventItemWriter)用法(十四)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
文章目錄
- 一、pom文件引入需要讀取xml文件jar包
- 二、抽取寫xml文件公共writer
- 三、processor
- 四、配置寫xml文件job
- 五、執行job
前言:在一些業務場景中,可能需要讀取xml文件,做業務邏輯處理,SpringBatch已經幫我們封裝好了讀取xml的reader
SpringBatch其它文章直通車:
- SpringBatch讀單個文件(FlatFileItemReader)和寫單個文件(FlatFileItemWriter)(一)
- SpringBatch順序讀取多文件(MultiResourceItemReader)和順序寫文件(MultiResourceItemWriter)(二)
- SpringBatch讀數據庫(MyBatisPagingItemReader)(三)
- SpringBatch讀文件(FlatFileItemReader)寫據庫(MyBatisBatchItemWriter)(四)
- SpringBatch 監聽器之Job監聽器(JobExecutionListener)和Step監聽器(StepExecutionListener)(五)
- SpringBatch 監聽器之Chunk監聽器(ChunkListener)和Skip監聽器(SkipListener)(六)
- SpringBatch 多線程(TaskExecutor)啟動Job詳解 (七)
- SpringBatch 配置并行啟動Job詳解 (八)
- SpringBatch 批處理分區(Partitioner )分片(九)
- SpringBatch tasklet實現和用法(十)
- SpringBatch 讀取JSON(JsonItemReader)用法(十一)
- SpringBatch 寫文件JSON(JsonFileItemWriter)用法(十二)
- SpringBatch 讀取xml文件(StaxEventItemReader)用法(十三)
代碼已上傳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屬性,該屬性在本章前面提到,用于指定是否可以覆蓋現有文件。配置編輯器
@Beanpublic 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> {@Overridepublic 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 {@Autowiredprivate JobBuilderFactory jobBuilderFactory;@Autowiredprivate StepBuilderFactory stepBuilderFactory;@Autowiredprivate CatProcessor catProcessor;@Beanpublic Job cafeXmlCatJob() {return jobBuilderFactory.get("cafeXmlCatJob").start(cafeXmlCatStep()).build();}@Beanpublic Step cafeXmlCatStep() {return stepBuilderFactory.get("cafeXmlCatStep").<CafeCat, Cat>chunk(10).reader(cafeCatXmlCommonFileItemReader()).processor(catProcessor).writer(commonXmlWriter()).build();}@Bean@StepScopepublic CommonFileItemReader<CafeCat> cafeCatXmlCommonFileItemReader() {return new CommonFileItemReader<>(CafeCat.class);}@Bean@StepScopepublic CommonXmlWriter commonXmlWriter() {return new CommonXmlWriter(tradeXmlMarshaller(),Cat.class);}@Beanpublic 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文件:
總結
以上是生活随笔為你收集整理的SpringBatch 写xml文件(StaxEventItemWriter)用法(十四)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SpringBatch 读取xml文件(
- 下一篇: SpringBatch 自定义ItemR