欢迎访问 生活随笔!

凯发k8官方网

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

javascript

springbatch 读取xml文件(staxeventitemreader)用法(十三) -凯发k8官方网

发布时间:2025/1/21 javascript 15 豆豆
凯发k8官方网 收集整理的这篇文章主要介绍了 springbatch 读取xml文件(staxeventitemreader)用法(十三) 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

    • 一、pom文件引入需要读取xml文件jar包
    • 二、抽取读取xml文件公共reader
    • 三、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)用法(十二)

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

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

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

二、抽取读取xml文件公共reader

spring batch为读取xml记录、将它们映射到java对象以及将java对象写入xml记录提供了事务基础设施。
stax api用于i/o,因为其他标准xml解析api不适合批处理需求(dom一次将整个输入加载到内存中,sax通过允许用户只提供回调来控制解析过程)。
我们需要考虑xml输入和输出如何在spring批处理中工作。首先,有一些概念与文件读取和写入不同,但在spring批处理xml过程中是常见的。在xml处理中,假设xml资源是与单个记录对应的“片段”集合,而不是需要标记的记录行(fieldset实例)
官方原图片段集合

package com.sl.common;import org.springframework.batch.item.xml.staxeventitemreader; import org.springframework.core.io.filesystemresource;/*** @author shuliangzhao* @title: commonxmlreader* @projectname spring-boot-learn* @description: todo* @date 2019/9/19 19:33*/ public class commonxmlreader extends staxeventitemreader {public commonxmlreader(class clz) {setname("xmlreader");setresource(new filesystemresource("d:\\aplus\\shuqian\\target\\" clz.getsimplename() ".xml"));setfragmentrootelementname("cat");} }

三、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: catxmlprocessor* @projectname spring-boot-learn* @description: todo* @date 2019/9/19 19:56*/ @component @stepscope public class catxmlprocessor extends commonprocessor {@overridepublic void processor(cafecat o, cat cat) {o.setcatname(cat.getcatname());o.setcatage(cat.getcatage());o.setcataddress(cat.getcataddress());} }

四、配置读取xml文件job

package com.sl.config;import com.sl.common.commonfileitemwriter; import com.sl.common.commonxmlreader; import com.sl.entity.cafecat; import com.sl.entity.cat; import com.sl.processor.catxmlprocessor; 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: catxmlconfiguration* @projectname spring-boot-learn* @description: todo* @date 2019/9/19 19:53*/ @configuration @enablebatchprocessing public class catreadxmlconfiguration {@autowiredprivate jobbuilderfactory jobbuilderfactory;@autowiredprivate stepbuilderfactory stepbuilderfactory;@autowiredprivate catxmlprocessor catxmlprocessor;@beanpublic job catreadxmljob() {return jobbuilderfactory.get("catreadxmljob").start(catreadxmlstep()).build();}@beanpublic step catreadxmlstep() {return stepbuilderfactory.get("catreadxmlstep").chunk(10).reader(catcommonxmlreader()).processor(catxmlprocessor).writer(cafecatxmlcommonfileitemwriter()).build();}@bean@stepscopepublic commonxmlreader catcommonxmlreader() {commonxmlreader commonxmlreader = new commonxmlreader(cat.class);commonxmlreader.setunmarshaller(trademarshaller());return commonxmlreader;}@beanpublic xstreammarshaller trademarshaller() {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;}@bean@stepscopepublic commonfileitemwriter cafecatxmlcommonfileitemwriter() {return new commonfileitemwriter<>(cafecat.class);}}

注意:在本例中,我们选择使用xstreammarshaller,它接受作为映射传入的别名,其中第一个键和值是片段(即根元素)的名称和要绑定的对象类型。然后,与fieldset类似,将映射到对象类型中的字段的其他元素的名称描述为映射中的键/值对。

五、执行job

写出csv文件:

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

总结

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

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

网站地图