开发手册 欢迎您!
软件开发者资料库

Spring Batch - MySQL到平面文件

Spring批量MySQL到平面文件 - 从简单和简单的步骤学习Spring Batch,从基本到高级概念,包括概述,环境,架构,应用程序,配置,读者,编写器和处理器,基本应用程序,XML到MySQL,CSV到XML ,MySQL到XML,MySQL到平面文件。

在本章中,我们将创建一个使用MySQL Reader和 Flatfile Writer(.txt)的Spring Batch应用程序.

Reader : 我们在应用程序中使用的Reader是 JdbcCursorItemReader 来从MySQL数据库中读取数据.

假设我们在MySQL数据库中创建了一个表,如下所示./p>

CREATE TABLE details.xml_mysql(    person_id int(10) NOT NULL,    sales VARCHAR(20),    qty int(3),    staffName VARCHAR(20),    date VARCHAR(20) );

假设我们已将以下记录插入其中.

mysql> select * from tutorialsdata; +-------------+-----------------+----------------+-----------------+ | tutorial_id | tutorial_author | tutorial_title | submission_date | +-------------+-----------------+----------------+-----------------+ |         101 | Sanjay          | Learn Java     | 06-05-2007      | |         102 | Abdul S         | Learn MySQL    | 19-04-2007      | |         103 | Krishna Kasyap  | Learn JavaFX   | 06-07-2017      | +-------------+-----------------+----------------+-----------------+ 3 rows in set (0.00 sec)

作家 : 我们在应用程序中使用的Writer是 FlatFileItemWriter ,用于将数据写入 flatfile (.txt).

处理器 : 我们在应用程序中使用的处理器是一个自定义处理器,它只打印从CSV文件读取的记录.

jobConfig.xml

以下是我们的示例Spring Batch应用程序的配置文件.在此文件中,我们将定义作业和步骤.除此之外,我们还为ItemReader,ItemProcessor和ItemWriter定义了bean. (这里,我们将它们与各个类相关联,并传递所需属性的值来配置它们.)

                                                                                                                                                                                                

Context.xml

以下是我们的Spring Batch应用程序的 context.xml .在此文件中,我们将定义诸如作业存储库,作业启动程序和事务管理器之类的bean.

                                                                                                                                      

CustomItemProcessor.java

以下是Processor类.在这个类中,我们在应用程序中编写处理代码.在这里,我们打印每条记录的内容.

import org.springframework.batch.item.ItemProcessor;  // Implementing the ItemProcessor interface public class CustomItemProcessor implements ItemProcessor {      @Override    public Tutorial process(Tutorial item) throws Exception {       System.out.println("Processing..." + item);       return item;    } }

TutorialRowMapper.java

以下是 TutorialRowMapper 将数据设置为 Tutorial 类的类.

public class TutorialRowMapper implements RowMapper {        @Override    public Tutorial mapRow(ResultSet rs, int rowNum) throws SQLException {          Tutorial tutorial = new Tutorial();          tutorial.setTutorial_id(rs.getInt("tutorial_id"));       tutorial.setTutorial_title(rs.getString("tutorial_title"));       tutorial.setTutorial_author(rs.getString("tutorial_author"));       tutorial.setSubmission_date(rs.getString("submission_date"));        return tutorial;    } }

Tutorial.java

以下是教程上课.它是一个简单的Java类,带有 setter getter 方法.在这个类中,我们使用注释将此类的方法与XML文件的标记相关联.

public class Tutorial {    private int tutorial_id;    private String tutorial_title;    private String tutorial_author;    private String submission_date;      public int getTutorial_id() {       return tutorial_id;    }        public void setTutorial_id(int tutorial_id) {       this.tutorial_id = tutorial_id;    }      public String getTutorial_title() {       return tutorial_title;    }       public void setTutorial_title(String tutorial_title) {       this.tutorial_title = tutorial_title;    }        public String getTutorial_author() {       return tutorial_author;    }      public void setTutorial_author(String tutorial_author) {       this.tutorial_author = tutorial_author;    }      public String getSubmission_date() {       return submission_date;    }     public void setSubmission_date(String submission_date) {       this.submission_date = submission_date;    }      @Override    public String toString() {       return " [id=" + tutorial_id + ", title=" +       tutorial_title                      + ",       author=" + tutorial_author + ", date=" +       submission_date + "]";    } }

App.java

以下是批量生产的代码处理.在本课程中,我们将通过运行JobLauncher启动批处理应用程序.

  import org.springframework.batch.core.Job;  import org.springframework.batch.core.JobExecution;  import org.springframework.batch.core.JobParameters;  import org.springframework.batch.core.launch.JobLauncher;  import org.springframework.context.ApplicationContext;  import org.springframework.context.support.ClassPathXmlApplicationContext;  public class App { public static void main(String [] args)throws Exception { String [] springConfig = {"jobs/job_hello_world.xml"}; //创建应用程序上下文对象 ApplicationContext context = new ClassPathXmlApplicationContext(springConfig); //创建作业启动器 JobLauncher jobLauncher =(JobLauncher)context.getBean("jobLauncher"); //创建作业作业作业=(作业)context.getBean("helloWorldJob"); //执行JOB  JobExecution执行= jobLauncher.run(job,new JobParameters());  System.out.println("退出状态:"+ execution.getStatus()); } }

执行此应用程序时,它将产生以下输出.

May 09, 2017 5:44:48 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3d646c37: startup date [Tue May 09 17:44:48 IST 2017]; root of context hierarchy May 09, 2017 5:44:48 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions  May 09, 2017 5:44:56 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run INFO: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}] May 09, 2017 5:44:56 PM org.springframework.batch.core.job.SimpleStepHandler handleStep INFO: Executing step: [step1] Processing...Report [id=101, title=Learn Java, author=Sanjay, date=06-05-2007] Processing...Report [id=102, title=Learn MySQL, author=Abdul S, date=19-04-2007] Processing...Report [id=103, title=Learn JavaFX, author=Krishna Kasyap, date=0607-2017] May 09, 2017 5:44:57 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run INFO: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED] Hello Exit Status : COMPLETED

这将生成带有以下内容的 .txt 文件.

Report [id=101, title=Learn Java, author=Sanjay, date=06-05-2007] Report [id=102, title=Learn MySQL, author=Abdul S, date=19-04-2007] Report [id=103, title=Learn JavaFX, author=Krishna Kasyap, date=06-07-2017]