spring batch之二 一个简单的spring batch的例子.

在实际工作中我们可能需要快速的实现一个spring batch.本文作为一个参考例子。 主要使用maven ,STS tool.   spring.framework.version 3.0.6.RELEASE. spring.batch.version 2.1.7.RELEASE mysql database. 

1: spring maven pom

    
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4.0.0
org.springframework.samples.batch
spring-batch-simple
2.0.0.CI-SNAPSHOT
jar
Command Line Spring Batch Module
http://www.springframework.org/spring-batch/archetypes/simple-cli-archetype

true
3.0.6.RELEASE
2.1.7.RELEASE




junit
junit
4.7
test


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


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


org.springframework
spring-context
${spring.framework.version}


cglib
cglib-nodep
2.2


org.springframework
spring-aop
${spring.framework.version}


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


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


commons-dbcp
commons-dbcp
1.4


commons-io
commons-io
1.4


mysql
mysql-connector-java
5.1.6


org.aspectj
aspectjrt
1.6.8


org.aspectj
aspectjweaver
1.6.8


log4j
log4j
1.2.14






Codehaus
http://repository.codehaus.org/

false



在mysql database 创建spring batch 系列表.

解压缩spring-batch-code-2.1.7-RELEASE.jar 在  org.springframework.batch.core 包下找到schema-mysql.sql. 在mysql database中执行该文件,创建spring 相关的表.

在mysql中创建student 表

创建student 表,主要包含id, name,age

spring batch 配置,将所有学生的age+1

   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">









      class="org.springframework.jdbc.datasource.DataSourceTransactionManager">

  



 
  

 
 
 

   
       
           
                                       isolation="DEFAULT" propagation="REQUIRED" timeout="3" />
                                               processor="itemProcessor1" writer="itemWriter1"
                         commit-interval="2">
             

         

   



class="org.springframework.batch.item.database.JdbcCursorItemReader"
scope="step">


value="select * from student" />



class="com.springbatch.demo.item.StudentProcessor">



class="com.springbatch.demo.item.StudentWriter"
scope="step">







class="com.springbatch.demo.dao.rowmapper.StudentRowMapper">
value="com.springbatch.demo.entity.Student">

     ............................


item processor 和 item writer

package com.springbatch.demo.item;

import org.springframework.batch.item.ItemProcessor;
import com.springbatch.demo.entity.Student;
public class StudentProcessor implements ItemProcessor {

    @Override
    public InventoryAdjustmentIn process(final Student student) throws Exception {
student.setAge(student.getAge()+1);
return student;
    }
}


package com.springbatch.demo.item;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.batch.item.ItemWriter;
import com.springbatch.demo.dao.StudentDao;
import com.springbatch.demo.entity.Student;


public class StudentWriter implements ItemWriter {

    private StudentDao studentDao;

    @Override
    public void write(final List students) throws Exception {

for (Student item : students) {
studentDao.update(item);
}
    }
}


main test class

package com.vipshop.springbatch.demo.pl3;


import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
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 studentTest {


    public static void main(final String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("launch-context.xml");
JobLauncher jobLauncher = context.getBean("jobLauncher", JobLauncher.class);
Job job = context.getBean("pl3-repository-adjust", Job.class);
JobParameters jobParam = new JobParametersBuilder().addString("test", "test1")
.addLong("currenttime", System.currenttime).toJobParameters();
try {
   jobLauncher.run(job, jobParam);
}
catch (JobExecutionAlreadyRunningException e) {
   e.printStackTrace();
}
catch (JobRestartException e) {
   e.printStackTrace();
}
catch (JobInstanceAlreadyCompleteException e) {
   e.printStackTrace();
}
catch (JobParametersInvalidException e) {
   e.printStackTrace();
}
    }
}

.....

其他的类就不在一一描述了.

你可能感兴趣的:(spring batch之二 一个简单的spring batch的例子.)