2013-06-24 25 views
0

我有一個要求,我在兩個不同的Query從數據庫中讀取。每個Query都有自己的SQLSQLs是相似的,大部分都遵循相同的表格,只有細微的差異。我想檢查我是否可以在ItemReader中有兩個SQLs或者可以使用jdbctemplateOne ItemReader,2 SQL Query,jdbcTemplate?

任何想法,示例代碼?

+0

是兩個SQL的返回相同的對象或複合對象? –

+0

是的,兩個SQL返回相同的對象,但結果不同。 –

回答

2

如果您想'重新使用'現有的JdbcCursorItemReader(或其他Spring Batch Jdbc * ItemReaders之一),您可以通過利用步驟範圍動態地切換SQL。以下是基於JobParameterssqlKey屬性切換SQL的配置示例。聲明的來源是一張簡單的地圖。

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:batch="http://www.springframework.org/schema/batch" 
    xmlns:util="http://www.springframework.org/schema/util" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd 
     http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd"> 

    <batch:job id="switchSQL"> 
     <batch:step id="switchSQL.step1"> 
      <batch:tasklet> 
       <batch:chunk reader="sqlItemReader" writer="outItemWriter" commit-interval="10"/> 
      </batch:tasklet> 
     </batch:step> 
    </batch:job> 

    <bean id="sqlItemReader" 
     class="org.springframework.batch.item.database.JdbcCursorItemReader" scope="step"> 
     <property name="dataSource" ref="dataSource"/> 
     <property name="sql" value="#{@sqlStatements[jobParameters['sqlKey']]}"/> 
     <property name="rowMapper"> 
      <bean class="de.incompleteco.spring.batch.data.ColumnRowMapper"/> 
     </property> 
    </bean> 

    <util:map id="sqlStatements"> 
     <entry key="sql1" value="select * from table_one"/> 
     <entry key="sql2" value="select * from table_two"/> 
    </util:map> 

    <bean id="outItemWriter" 
     class="org.springframework.batch.item.adapter.ItemWriterAdapter"> 
     <property name="targetObject" ref="outWriter"/> 
     <property name="targetMethod" value="write"/> 
    </bean> 

    <bean id="outWriter" class="de.incompleteco.spring.batch.item.SystemOutItemWriter"/> 
</beans> 

這裏是輔助類;

(一個簡單的itemwriter)

package de.incompleteco.spring.batch.item; 

public class SystemOutItemWriter { 

    public void write(Object object) { 
     System.out.println(object); 
    } 

} 

(和一個簡單的RowMapper)

package de.incompleteco.spring.batch.data; 

import java.sql.ResultSet; 
import java.sql.SQLException; 

import org.springframework.jdbc.core.RowMapper; 

public class ColumnRowMapper implements RowMapper<String> { 

    public String mapRow(ResultSet rs, int rowNum) throws SQLException { 
     return rs.getString(1); 
    } 

} 

和這裏的其餘配置

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> 


    <bean id="jobRepository" 
     class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/> 

    <bean id="jobExplorer" 
     class="org.springframework.batch.core.explore.support.MapJobExplorerFactoryBean"> 
     <property name="repositoryFactory" ref="&amp;jobRepository"/> 
    </bean> 

    <bean id="jobLauncher" 
     class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> 
     <property name="jobRepository" ref="jobRepository"/> 
    </bean> 


    <bean id="transactionManager" 
     class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"> 
    </bean> 

    <bean class="org.springframework.batch.core.scope.StepScope"/> 
</beans> 

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd"> 


    <jdbc:embedded-database id="dataSource" type="H2"> 
     <jdbc:script location="classpath:/META-INF/sql/schema-h2.sql"/> 
     <jdbc:script location="classpath:/META-INF/sql/insert-h2.sql"/> 
    </jdbc:embedded-database> 

    <bean id="dataSourceTransactionManager" 
     class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 
</beans> 

和SQL東西

create table table_one (
    column_a varchar(50) 
); 

create table table_two (
    column_a varchar(50) 
); 

--table one 
insert into table_one (column_a) values ('hello'); 
insert into table_one (column_a) values ('world'); 

--table two 
insert into table_two (column_a) values ('hey'); 

現在終於單元測試

package de.incompleteco.spring; 

import static org.junit.Assert.assertFalse; 

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.JobParametersBuilder; 
import org.springframework.batch.core.explore.JobExplorer; 
import org.springframework.batch.core.launch.JobLauncher; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration({"classpath:/META-INF/spring/*-context.xml"}) 
public class SwitchSQLIntegrationTest { 

    @Autowired 
    private Job job; 

    @Autowired 
    private JobLauncher jobLauncher; 

    @Autowired 
    private JobExplorer jobExplorer; 

    @Test 
    public void test() throws Exception { 
     //setup the parameters 
     JobParameters parameters = new JobParametersBuilder().addLong("runtime",System.currentTimeMillis()) 
       .addString("sqlKey", "sql1").toJobParameters(); 
     //run 
     JobExecution execution = jobLauncher.run(job,parameters); 
     //test 
     while (jobExplorer.getJobExecution(execution.getId()).isRunning()) { 
      Thread.sleep(100); 
     }//end while 
     //load 
     execution = jobExplorer.getJobExecution(execution.getId()); 
     //test 
     assertFalse(execution.getStatus().isUnsuccessful()); 
     //run it again 
     parameters = new JobParametersBuilder().addLong("runtime",System.currentTimeMillis()) 
       .addString("sqlKey", "sql2").toJobParameters(); 
     //run 
     execution = jobLauncher.run(job,parameters); 
     //test 
     while (jobExplorer.getJobExecution(execution.getId()).isRunning()) { 
      Thread.sleep(100); 
     }//end while 
     //load 
     execution = jobExplorer.getJobExecution(execution.getId()); 
     //test 
     assertFalse(execution.getStatus().isUnsuccessful());   
    } 

} 
+0

謝謝你我的靈感來自你的例子 –

+0

嘿,你能指導我們如何將來自兩個不同表格的數據寫入單個平面文件?請指導。謝謝,Neha – Prateek