spring-batch - 从数据库分页读取数据然后输出

阅读更多

一、pom文件



    
        spring-batch-demo
        com.joandora
        1.0-SNAPSHOT
    
    4.0.0

    spring-batch-integration
    jar

    
        UTF-8
        4.2.0.RELEASE
        1.7.1.RELEASE
        3.0.7.RELEASE
        2.2
        1.8.2
        0.9.1.2
        2.2.5
        1.7.13
        1.2.17
        
        4.12
        
        0.2.3
        2.3.2
        1.0
    
    
        
            org.springframework
            spring-context
            ${spring.core.version}
        
        
            org.springframework
            spring-context-support
            ${spring.core.version}
        
        
            org.springframework
            spring-core
            ${spring.core.version}
            jar
            compile
        

        
            org.springframework
            spring-tx
            ${spring.core.version}
            jar
            compile
        

        
            org.springframework
            spring-jdbc
            ${spring.core.version}
            jar
            compile
        

        
            org.springframework
            spring-oxm
            ${spring.core.version}
            jar
            compile
        

        
            org.springframework
            spring-aspects
            ${spring.core.version}
            jar
            compile
        

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

        
        
            org.aspectj
            aspectjrt
            ${aspectj.version}
        

        
            org.aspectj
            aspectjweaver
            ${aspectj.version}
        

        
        
            cglib
            cglib-nodep
            ${cglib.version}
            jar
            compile
        

        
        
            log4j
            log4j
            ${log4j.version}
            jar
            compile
        

        
        
            org.slf4j
            slf4j-api
            ${slf4j.version}
            jar
            compile
        
        
            org.slf4j
            slf4j-log4j12
            ${slf4j.version}
            jar
            compile
        
        
            org.slf4j
            jcl-over-slf4j
            ${slf4j.version}
            jar
            compile
        

        
        
            org.springframework.data
            spring-data-jpa
            ${spring.data.jpa.version}
        

        
        
            c3p0
            c3p0
            ${c3p0.version}
            jar
            compile
        

        
        
            junit
            junit
            ${junit.version}
            jar
            test
        

        
            org.springframework
            spring-test
            ${spring.core.version}
            jar
            test
        
        
        
            org.hsqldb
            hsqldb
            2.3.2
        

    
    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                2.3.2
                
                    1.6
                    1.6
                
            

            
                maven-jar-plugin
                2.4
                
                    
                        
                            true
                        
                    
                
            
        
    

 

二、spring总配置文件

    applicationContext.xml

 



    
    
    
    
    

    
    

    
    
    

 

   spring.properties

# database properties
#app.jdbc.driverClassName=com.mysql.jdbc.Driver
app.jdbc.driverClassName=org.hsqldb.jdbcDriver
#app.jdbc.url=jdbc:mysql://localhost/physiandb
app.jdbc.url=jdbc:hsqldb:mem:physiandb;sql.enforce_strict_size=true;hsqldb.tx=mvcc
#app.jdbc.username=root
app.jdbc.username=sa
#app.jdbc.password=root
app.jdbc.password=

# batch properties
job.commit.interval=8
job.skip_limit=10

 

三、数据库配置

 

   spring-data.xml

 



	
	
	
	
	
	
		
	

	
       			

	
	
		
		
		
	

 

    blacklist.sql,程序启动将执行此脚本

 

 

CREATE TABLE T_BLACKLIST (
NAME varchar(100) NOT NULL PRIMARY KEY
);
insert into T_BLACKLIST values('tb1');
insert into T_BLACKLIST values('tb2');
insert into T_BLACKLIST values('tb3');
insert into T_BLACKLIST values('tb4');
insert into T_BLACKLIST values('tb5');
insert into T_BLACKLIST values('tb6');
insert into T_BLACKLIST values('tb7');
insert into T_BLACKLIST values('tb8');
insert into T_BLACKLIST values('tb9');
insert into T_BLACKLIST values('tb10');
 

 

 四、spring-batch配置

 

   spring-batch.xml

 

 



	
	
	
	
	
	
	
	
 

 

 

 五、job配置

 

    spring-batch-job.xml

 

 



	
		
		
		
	
	
		
			
				
			
		
	
	
		
		
			
				
				
				
				
				
			
		
		
			
				
			
		
		
		
		
	
 

 

六、启动类

    com.joandora.spring.batch.server.Startup

 

public class Startup {
	public static void main(String[] args) throws Exception {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");

		BlackListLoader blackListLoaderTask = (BlackListLoader) context.getBean("blackListLoader");
		blackListLoaderTask.loadBlackList();
	}
}
 

 

   启动job的类:com.joandora.spring.batch.loader.BlackListLoader

@Component
public class BlackListLoader {
    private static final Logger LOG = LoggerFactory.getLogger(BlackListLoader.class);
    @Resource
    private JobLauncher jobLauncher;

    @Resource
    private Job blackListJob;

    public void loadBlackList() {
        try {
            JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
            jobParametersBuilder.addString("name", "%tb%");

            jobLauncher.run(blackListJob, jobParametersBuilder.toJobParameters());

        } catch (JobInstanceAlreadyCompleteException ex) {
            LOG.debug("This job has been completed already!");

        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("over----------------");
    }
}

    解析:

    1、定义了一个名称为name的参数,值为%tb%。意思就是替换sql的模糊查询。

 

 七、reader

 


		
		
			
				
				
				
				
				
			
		
		
			
				
			
		
		
		
		
	
   解析:

 

   1、使用spring-batch已实现的分页类:SqlPagingQueryProviderFactoryBean

 2、selectClause、fromClause、fromClause组合成一条sql。

 3、sortKey很重要,如果任务失败,下次启动时不至于处理的记录混乱。

 

 com.joandora.spring.batch.mapper.BlackListRowMapper

 

@Component
public class BlackListRowMapper implements RowMapper {

    public BlackListEntity mapRow(ResultSet rs, int rowNum) throws SQLException {
        BlackListEntity blackList = new BlackListEntity();
        blackList.setName(rs.getString("NAME"));
        return blackList;
    }
}
 

 

八、Processor

   com.joandora.spring.batch.processor.BlackListProcessor

 

@Component
public class BlackListProcessor implements ItemProcessor {

    @Override
    public BlackListEntity process(BlackListEntity item) throws Exception {
        item.setName(item.getName()+"-ByProcessor");
        return item;
    }
}
 

 

九、writer

   com.joandora.spring.batch.writer.BlackListWriter

@Component
public class BlackListWriter  implements ItemWriter {

    private static final Logger LOG = LoggerFactory.getLogger(BlackListEntity.class);

    @Override
    public void write(List blackListEntities) {
        try {
            for (BlackListEntity blackList : blackListEntities) {
                System.out.println("spring batch writer show"+blackList.getName());
            }
        } catch (Exception ple) {
            LOG.debug(ple.getMessage());
        }
    }
}

 

你可能感兴趣的:(spring,batch)