我使用来自Spring Framework的FlatFileItemReader
和JdbcItemWriter
批量写入从文件到Oracle数据库的数据。
配置commit-interval
>时1,让我们说2,Reader从文件中读取两行,然后写入器将数据从第二行写入数据库两次。
我的配置是这样的:
<batch:job id="Job">
<batch:step id="step">
<batch:tasklet>
<batch:chunk reader="Reader" writer="Writer"
commit-interval="2" processor-transactional="false" />
</batch:tasklet>
<batch:fail on="FAILED" />
</batch:step>
</batch:job>
<bean id="Reader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="linesToSkip" value="1" />
<!-- TODO: FilePath definieren (Umgebungsabhängig) -->
<property name="resource" value="file:in/Users_kurz.csv" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="name1, name2, name3, name4" />
<property name="delimiter" value=";" />
</bean>
</property>
<property name="fieldSetMapper">
<bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<property name="prototypeBeanName" value="lineObject" />
</bean>
</property>
</bean>
</property>
</bean>
<bean id="LineObject" class="this.is.my.LineObject" />
<bean id="dcIdentityWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="dataSource" />
<property name="sql"
value="INSERT INTO table(col1, col2, col3, col4)
VALUES (:name1, name2, name3, name4)" />
<property name="itemSqlParameterSourceProvider">
<bean class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
</property>
</bean>