沒有一個隨時可用的組件來執行你所要求的;唯一的解決方案是編寫一個代表JdbcCursorItemReader
(或HibernateCursorItemReader
或任何通用ItemReader
實現)的自定義ItemReader<>
。
您需要準備好所有必需的東西(數據源,會話,真實數據庫讀取器)並將所有委派的讀者綁定到您的自定義閱讀器。
編輯: 您需要模擬一個循環,使用recrecusion ItemReader.read()
和mantain reader並在作業重新啓動時委託狀態。
class MyItemReader<T> implements ItemReader<T>, ItemStream {
private ItemReader[] delegates;
private int delegateIndex;
private ItemReader<T> currentDelegate;
private ExecutionContext stepExecutionContext;
public void setDelegates(ItemReader[] delegates) {
this.delegates = delegates;
}
@BeforeStep
private void beforeStep(StepExecution stepExecution) {
this.stepExecutionContext = stepExecution.getExecutionContext();
}
public T read() {
T item = null;
if(null != currentDelegate) {
item = currentDelegate.read();
if(null == item) {
((ItemStream)this.currentDelegate).close();
this.currentDelegate = null;
}
}
// Move to next delegate if previous was exhausted!
if(null == item && this.delegateIndex< this.delegates.length) {
this.currentDelegate = this.delegates[this.currentIndex++];
((ItemStream)this.currentDelegate).open(this.stepExecutionContext);
update(this.stepExecutionContext);
// Recurse to read() to simulate loop through delegates
item = read();
}
return item;
}
public void open(ExecutionContext ctx) {
// During open restore last active reader and restore its state
if(ctx.containsKey("index")) {
this.delegateIndex = ctx.getInt("index");
this.currentDelegate = this.delegates[this.delegateIndex];
((ItemStream)this.currentDelegate).open(ctx);
}
}
public void update(ExecutionContext ctx) {
// Update current delegate index and state
ctx.putInt("index", this.delegateIndex);
if(null != this.currentDelegate) {
((ItemStream)this.currentDelegate).update(ctx);
}
}
public void close(ExecutionContext ctx) {
if(null != this.currentDelegate) {
((ItemStream)this.currentDelegate).close();
}
}
<bean id="myItemReader" class=path.to.MyItemReader>
<property name="delegates">
<array>
<ref bean="itemReader1"/>
<ref bean="itemReader2"/>
<ref bean="itemReader3"/>
</array>
</property>
</bean>
EDIT2:請記住,設置屬性名;這是必要讓MyItemReader.read()正常工作
<bean id="itemReader1" class="JdbcCursorItemReader">
<property name="name" value="itemReader1" />
<!-- Set other properties -->
</bean>
你能提供更多關於你的情況的信息嗎? 喜歡; - 您是否從相同數據庫類型(MySQL,Oracle或DB2)讀取? - 您是否從不同的數據庫實例中讀取同一個表?如果答案是否定的,你如何將你的輸入映射到同一個對象? – nsylmz
是MySQL與不同的表,我只想採取一個字符串 – xedo