博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Batch 例子: 从数据库导出分隔符文件
阅读量:4049 次
发布时间:2019-05-25

本文共 4096 字,大约阅读时间需要 13 分钟。

– Start


假设我们有如下表。

CREATE TABLE PEOPLE(    ID    NUMBER(8,0),     NAME  VARCHAR2(30));

我们需要把表中的数据导出到如下的分隔符文件中,有标题行和结尾行。

id|name1|zhangsan2|lisi3|wangwuTotal line|3

让我们来看看代码吧。

package shangbo.springbatch.example4;public class People implements java.io.Serializable{	private static final long serialVersionUID = 8904705906008476310L;		private Integer id;	private String name;	public Integer getId() {		return id;	}	public void setId(Integer id) {		this.id = id;	}	public String getName() {		return name;	}	public void setName(String name) {		this.name = name;	}}
package shangbo.springbatch.example4;import java.io.IOException;import java.io.Writer;import java.util.List;import org.springframework.batch.item.ExecutionContext;import org.springframework.batch.item.ItemStreamException;import org.springframework.batch.item.ItemStreamWriter;import org.springframework.batch.item.file.FlatFileFooterCallback;import org.springframework.batch.item.file.FlatFileHeaderCallback;public class MyFileItemWriter implements FlatFileFooterCallback, FlatFileHeaderCallback, ItemStreamWriter
{ private ItemStreamWriter
delegate; private String header; private int totalLine = 0; // Override from FlatFileHeaderCallback @Override public void writeHeader(Writer writer) throws IOException { writer.write(header); } // Override from FlatFileFooterCallback @Override public void writeFooter(Writer writer) throws IOException { writer.write("Total line|" + totalLine); } // Override from ItemStreamWriter @Override public void write(List
items) throws Exception { totalLine += items.size(); delegate.write(items); } @Override public void open(ExecutionContext executionContext) throws ItemStreamException { delegate.open(executionContext); } @Override public void update(ExecutionContext executionContext) throws ItemStreamException { delegate.update(executionContext); } @Override public void close() throws ItemStreamException { delegate.close(); } // Setter public void setDelegate(ItemStreamWriter
delegate) { this.delegate = delegate; } public void setHeader(String header) { this.header = header; }}
package shangbo.springbatch.example4;import java.util.HashMap;import java.util.Map;import org.springframework.batch.core.Job;import org.springframework.batch.core.JobParameter;import org.springframework.batch.core.JobParameters;import org.springframework.batch.core.JobParametersInvalidException;import org.springframework.batch.core.launch.JobLauncher;import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;import org.springframework.batch.core.repository.JobRestartException;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class App {	public static void main(String[] args) throws JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException, JobParametersInvalidException {		ApplicationContext context = new ClassPathXmlApplicationContext("shangbo/springbatch/example4/ExtractFileJob.xml");		// job 和 job 参数		Map
parameters = new HashMap<>(); parameters.put("business_date", new JobParameter("20170719")); JobParameters jobParameters = new JobParameters(parameters); Job job = context.getBean(Job.class); // 运行 job JobLauncher jobLauncher = context.getBean(JobLauncher.class); jobLauncher.run(job, jobParameters); }}

– 声 明:转载请注明出处
– Last Updated on 2018-05-17
– Written by ShangBo on 2017-07-19
– End

你可能感兴趣的文章
计算机网络复习要点
查看>>
Variable property attributes or Modifiers in iOS
查看>>
NSNotificationCenter 用法总结
查看>>
C primer plus 基础总结(一)
查看>>
剑指offer算法题分析与整理(三)
查看>>
Ubuntu 13.10使用fcitx输入法
查看>>
pidgin-lwqq 安装
查看>>
mint/ubuntu安装搜狗输入法
查看>>
C++动态申请数组和参数传递问题
查看>>
opencv学习——在MFC中读取和显示图像
查看>>
C/C++中关于动态生成一维数组和二维数组的学习
查看>>
JVM最简生存指南
查看>>
JVM并发机制探讨—内存模型、内存可见性和指令重排序
查看>>
持续可用与CAP理论 – 一个系统开发者的观点
查看>>
nginx+tomcat+memcached (msm)实现 session同步复制
查看>>
c++字符数组和字符指针区别以及str***函数
查看>>
c++类的操作符重载注意事项
查看>>
c++模板与泛型编程
查看>>
WAV文件解析
查看>>
WPF中PATH使用AI导出SVG的方法
查看>>