栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

创建一个简单的Spring Batch项目(一)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

创建一个简单的Spring Batch项目(一)

一、首先创建一个maven项目,名字为“sping-batch”,maven项目创建方法参考我的另一篇文章

地址:IDEA创建一个maven项目_人……杰的博客-CSDN博客_idea新建一个maven项目

二、将maven项目改造成一个Spring Batch项目

1.项目目录结构:

2.在pom.xml引入Spring Batch依赖的jar包

pom.xml:

    
    
        1.8
        4.3.8.RELEASE
        3.0.7.RELEASE
        5.1.25
        4.11
    

    
    
        
        
            org.springframework
            spring-core
            ${spring.version}
        

        
        
            org.springframework
            spring-jdbc
            ${spring.version}
        

        
        
            org.springframework
            spring-oxm
            ${spring.version}
        

        
        
            mysql
            mysql-connector-java
            ${mysql.driver.version}
        

        
        
            org.springframework.batch
            spring-batch-core
            ${spring.batch.version}
        

        
            org.springframework.batch
            spring-batch-infrastructure
            ${spring.batch.version}
        

        
        
            org.springframework.batch
            spring-batch-test
            ${spring.batch.version}
        

        
        
            junit
            junit
            ${junit.version}
            test
        
    

3.配置作业仓库、作业调度器、事务管理器、读步骤、处理步骤、写步骤

demo1-jobContext.xml:




    
    
    

    
    
        
        
    

    
    

    
    
    
        
        
        
        
            
                
                
                
                
                    
                        
                    
                
            
        
    

    
    
        
        
        
        
            
                accountID
                name
                amount
                date
                address
            
        
    

    
    

    
    

    
    
        
        
        
        
            
            
                
                
                
                
                    
                        
                        
                    
                
            
        
    

4.配置作业和作业步

demo1-job.xml:




    
    

    
    
        
        
            
                
                
                
            
        
    

5.编写实体对象类CreditBill、处理类CreditBillProcessor和启动方法BatchMain:

CreditBill.java:

package com.xj.demo1;


public class CreditBill {
    //银行卡账户ID
    private String accountID = "";
    //持卡人姓名
    private String name = "";
    //消费金额
    private double amount = 0;
    //消费日期
    private String date = "";
    //消费场所
    private String address = "";

    public String getAccountID() {
        return accountID;
    }

    public void setAccountID(String accountID) {
        this.accountID = accountID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getAmount() {
        return amount;
    }

    public void setAmount(double amount) {
        this.amount = amount;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    @Override
    public String toString() {
        return this.accountID + "," + this.name + "," + this.amount + "," + this.date + "," + this.address;
    }
}

CreditBillProcessor.java:

package com.xj.demo1;

import org.springframework.batch.item.ItemProcessor;


public class CreditBillProcessor implements ItemProcessor {
    @Override
    public CreditBill process(CreditBill bill) throws Exception {

        System.out.println(bill.toString());
        //做一些简单的处理
        bill.setAccountID(bill.getAccountID() + "1");
        bill.setName(bill.getName() + "2");
        bill.setAmount(bill.getAmount() + 3);
        bill.setDate(bill.getDate() + "4");
        bill.setAddress(bill.getAddress() + 5);

        return bill;
    }
}

BatchMain.java:

package com.xj.demo1;

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 BatchMain {
    public static void main(String[] args) {

        ApplicationContext context = new ClassPathXmlApplicationContext("demo1/job/demo1-job.xml");
        //Spring Batch的作业启动器,
        JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");
        //在batch.xml中配置的一个作业
        Job job  = (Job)context.getBean("billJob");

        try{
            //开始执行这个作业,获得处理结果(要运行的job,job参数对象)
            JobExecution result = launcher.run(job, new JobParameters());
            System.out.println(result.toString());
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

6.编写测试类

JobLaunchTest.java:

package demo1;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"/demo1/job/demo1-job.xml"})
public class JobLaunchTest {

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    @Qualifier("billJob")
    private Job job;

    @Before
    public void setUp() throws Exception{

    }

    @After
    public void tearDown() throws Exception{

    }

    @Test
    public void importProducts() throws Exception{
        JobExecution result = jobLauncher.run(job, new JobParameters());
        System.out.println(result.toString());
    }
}

7.配置读文件和写文件:

demo1-inputFile.csv

4101231234656,tom,100.00,2013-12-31 12:00:08,Lu lit
4101236543210,tom,120.00,2013-12-31 12:00:08,Lu Zui

demo1-outputFile.csv

是一个空文件,专门用来写入的数据。

三、进行运行测试

1.运行BatchMain:

运行结果:

 2.运行测试文件JobLaunchTest:

运行结果:

代码地址:

SpringBatch: 学习《Spring Batch 批处理框架》过程中所做的代码备份。

 心得:

整天看书看书,结果发现好多东西不去实践就无法真正的理解吃透,所以开始一边看书一边去实践,并做相应的笔记。这篇文章是基于一本书中的某一章来写的,结果书中代码有问题,调试了好久都无非解决,最后连续参考了好多博主的文章,才发现问题,并完成了这篇博文。整个历程耗时将近三天吧,不过是边工作边学习的三天。最后还是想吐槽一下那本书的作者,书中代码有遗漏的部分也没有发现,坑人不浅,缺乏严谨的态度……

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/356061.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号