spring-batch - 从csv读取数据放入数据库

 spring-batch 应用:从csv读取数据写入到数据库

 

一、spring配置文件

 

applicationContext.xml

 

 



	
	
	
	
	

	
	

	
	
	
 

 

    spring-data.xml

 

 



	
	
	
	
	
	
		
	

	
       			
     
     		
    
	
	
		
		
		
	
 

 

    spring-batch.xml

 

 



	
	
	
	
	
	
	
	
 

 

   physician-spring-batch-job-.xml

 

 




	
		
			
				
				
	            
					
						
						
					
					
						
						
					
					
						
					
	           
			
		
		
			
			
		

		
	

	
		
		
		
			
				
					
						
					
				
				
					
				
			
		
	
 

 

 

二、主程序入口

 

 

/***
 * 启动类
* 从文件中读取数据 保存到数据库中 */ public class Startup { public static void main(String[] args) throws Exception { ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); BlackListLoaderJob blackListLoaderTask = (BlackListLoaderJob) context.getBean("blackListLoaderJob"); blackListLoaderTask.loadBlackList(); } }
 

 

 blackListLoaderJob定义在physician-spring-batch-job-.xml中定义的一个任务。

 使用如下标签定义:

 

 
 

 

 

一个job可以定义多个step,可以理解成一个任务分为多少个步骤:

 

 


 

 

   在step里面定义一个tasklet:

 


	
	
	            
		
			
			
		
		
			
			
		
		
			
		
	
 

 

  chunk的reader属性从文件读取数据,processor是将读取数据进行自定义处理,write将处理过的数据写入数据库。

  commit-interval属性是一次事务提交时处理的记录数。skip-limit属性是允许跳过的最大记录数。retry-limit属性是允许重试的最大记录数。

  skippable-exception-classes属性可以定义报某种错时就跳过该条记录的处理。

  retryable-exception-classes属性可以定义报某种错时就重试该条记录。

  no-rollback-exception-classes属性可以定义报某种错时事务不会回滚。

 

三、读取csv文件 

 


		
		
		
			
				
					
						
					
				
				
					
				
			
		
	

 

   属性解释:

   resource:csv文件所在路径

   linesToSkip:csv文件读取数据时,跳过的起始行数

   

四、处理从csv文件读取的每条数据

@Component
public class BlackListItemProcessor implements ItemProcessor {
    @Override
    public BlackListDomain process(BlackListDomain item) throws Exception {
        return item;
    }
}

 

 

五、将处理过的数据写入数据库

@Component
public class BlackListItemWriter implements ItemWriter {

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

    @Resource
    private IBlackListDao physicianDao;

    @Override
    public void write(List blackListDomains) {
    try {
        for (BlackListDomain blackListDomain : blackListDomains) {
              physicianDao.loadBlackList(blackListDomain);
        }
        physicianDao.queryBlackList();
    } catch (Exception ple) {
        LOG.debug(ple.getMessage());
    }
  }
}

 

 

六、BlackListLoaderJob 

@Component
public class BlackListLoaderJob {

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

    private JobLauncher jobLauncher;

    private Job blackListJob;

    public Job getPhysicianJob() {
        return blackListJob;
    }

    @Autowired
    @Qualifier("blackListJob")
    public void setPhysicianJob(Job physicianJob) {
        this.blackListJob = physicianJob;
    }

    public JobLauncher getJobLauncher() {
        return jobLauncher;
    }

    @Autowired
    @Required
    public void setJobLauncher(JobLauncher jobLauncher) {
        this.jobLauncher = jobLauncher;
    }

    public void loadBlackList() {
        LOG.info("Entered sendEvents");
        try {
            Map parameters = new HashMap();
            parameters.put("date", new JobParameter(new Date()));
            jobLauncher.run(blackListJob, new JobParameters(parameters));

        } catch (JobInstanceAlreadyCompleteException ex) {
            LOG.debug("This job has been completed already!");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        System.out.println("over----------------");
    }
}

 

 

 七、pom文件



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

    spring-batch-writers

    
        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)