在Spring Batch中从数据库读取记录 [英] Reading Records From a Database in Spring Batch
问题描述
我试图使用循环从数据库中读取一些记录,然后对记录进行一些计算(更新名为total的字段).
I'm trying to read some records from a database using loops then do some calculations on the records (updating a field called total).
但是我是春季批处理的新手,所以任何人都可以给我一些提示.
But i'm new to spring batch so please can anyone provide me with some tips.
推荐答案
这听起来像块模式可以解决的问题.您可以使用重复使用的现有Spring Batch组件从数据库中读取,组成自己的处理器,然后传递回Spring Batch组件进行存储.
this sounds like something the chunk pattern would address. you can use re-use existing Spring Batch components to read from the database, compose your own processor, then pass back to a Spring Batch component to store.
说用例是这样的; -阅读记录 -record.setTotalColumn(record.getColumn2()+ record.getColumn3()) -更新
say the use case is like this; - read a record - record.setTotalColumn(record.getColumn2() + record.getColumn3()) - update
此配置可能看起来像这样
this configuration might look like this
<batch:job id="recordProcessor">
<batch:step id="recordProcessor.step1">
<batch:tasklet>
<batch:chunk reader="jdbcReader" processor="calculator" writer="jdbcWriter" commit-interval="10"/>
</batch:tasklet>
</batch:step>
</batch:job>
<bean id="jdbcReader" class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="dataSource"/>
<property name="sql" value="select idColumn,column1,column2,totalColumn from my_table"/>
<property name="rowMapper" ref="myRowMapper"/>
</bean>
<bean id="jdbcWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource"/>
<property name="sql" value="update my_table set totalColumn = :value where idColumn = :id"/>
</bean>
<bean id="calculator" class="de.incompleteco.spring.batch.step.item.CalculationProcessor"/>
这意味着您唯一需要从头开始写"的东西就是计算器处理器,它可能像这样;
this means that the only thing you have to 'write' from scratch would be the calculator processor which could e something like this;
package de.incompleteco.spring.batch.step.item;
import org.springframework.batch.item.ItemProcessor;
public class CalculationProcessor implements ItemProcessor<MyObject, MyObject> {
@Override
public MyObject process(MyObject item) throws Exception {
//do the math
item.setTotalColumn(item.getColumn1() + item.getColumn2());
//return
return item;
}
}
这篇关于在Spring Batch中从数据库读取记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!