Spring batch Job read from multiple sources

后端 未结 3 1551
借酒劲吻你
借酒劲吻你 2020-11-29 07:30

How can I read items from multiples databases? I already know that is possible from files.
the following example works for read from multiples files

...
         


        
3条回答
  •  情书的邮戳
    2020-11-29 08:08

    I suggest a tricky way. If we assume that one is your mysql datasource's table is base and every row in that table corresponds other mysql datasource table's row(like a join tables which are in different datasources), you could do it in your batch job itemreader. Ex of this way;

    Spring DataSource Configuration;

    
        
        
        
        
        
    
    
    
        
        
        
        
        
    
    

    Your batch-job.xml

    
        
        
        
            
                SELECT * FROM xyz
            
        
    
    
    
        
        
            
                SELECT * FROM abc
            
        
    
    

    Your RowMapper looks like;

    public class MultiDatasourceRowMapper implements RowMapper {
    
        private DataSource secondDataSource;
    
        private String secondSql;
    
        public String mapRow(ResultSet rs, int arg1) throws SQLException {
            Connection conn = secondDataSource.getConnection();
            PreparedStatement prep = conn.prepareStatement(secondSql); 
    
            // Do Something
    
            return "";
        }
    
        public void setSecondDataSource(DataSource secondDataSource) {
            this.secondDataSource = secondDataSource;
        }
    
        public void setSecondSql(String secondSql) {
            this.secondSql = secondSql;
        }
    
    }
    

提交回复
热议问题