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

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

– Start


假设我们有如下表。

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

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

id        name         1                      zhangsan         2                          lisi         3                        wangwuTotal line3

让我们来看看代码吧。

package shangbo.springbatch.example5;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.example5;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.example5;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/example5/ExtractFileJob.xml");		// job 和 job 参数		Map
parameters = new HashMap<>(); parameters.put("business_date", new JobParameter("20170721")); 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 2017-05-17
– Written by ShangBo on 2017-07-19
– End

你可能感兴趣的文章
本科生的编程水平到底有多高
查看>>
AngularJS2中最基本的文件说明
查看>>
从头开始学习jsp(2)——jsp的基本语法
查看>>
使用与或运算完成两个整数的相加
查看>>
备忘:java中的递归
查看>>
DIV/CSS:一个贴在左上角的标签
查看>>
Solr及Spring-Data-Solr入门学习
查看>>
Vue组件
查看>>
python_time模块
查看>>
python_configparser(解析ini)
查看>>
selenium学习资料
查看>>
<转>文档视图指针互获
查看>>
从mysql中 导出/导入表及数据
查看>>
HQL语句大全(转)
查看>>
几个常用的Javascript字符串处理函数 spilt(),join(),substring()和indexof()
查看>>
javascript传参字符串 与引号的嵌套调用
查看>>
swiper插件的的使用
查看>>
layui插件的使用
查看>>
JS牛客网编译环境的使用
查看>>
9、VUE面经
查看>>