欢迎访问 生活随笔!

凯发k8官方网

当前位置: 凯发k8官方网 > 前端技术 > javascript >内容正文

javascript

springbatch 写xml文件(staxeventitemwriter)用法(十四) -凯发k8官方网

发布时间:2025/1/21 javascript 14 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 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包

com.thoughtworks.xstreamxstream1.4.11.1org.springframeworkspring-oxm5.1.9.release

二、抽取写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 extends staxeventitemwriter {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 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 {@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").chunk(10).reader(cafecatxmlcommonfileitemreader()).processor(catprocessor).writer(commonxmlwriter()).build();}@bean@stepscopepublic commonfileitemreader cafecatxmlcommonfileitemreader() {return new commonfileitemreader<>(cafecat.class);}@bean@stepscopepublic commonxmlwriter commonxmlwriter() {return new commonxmlwriter(tradexmlmarshaller(),cat.class);}@beanpublic xstreammarshaller tradexmlmarshaller() {map 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文件:

与50位技术专家面对面20年技术见证,附赠技术全景图

总结

以上是凯发k8官方网为你收集整理的springbatch 写xml文件(staxeventitemwriter)用法(十四)的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得凯发k8官方网网站内容还不错,欢迎将凯发k8官方网推荐给好友。

网站地图