Java Reference
In-Depth Information
Output
The next component to do work is the
writer
, which is responsible for taking the aggregated collection
of items read from the reader. In this case, you might imagine a
java.util.List<UserRegistration>
containing up to as many elements as was configured for the
commit-interval
attribute on
the
chunk
element. Because you're trying to write to a database, you use Spring Batch's
org.springframework.batch.item.database.JdbcBatchItemWriter
. This class contains support for taking
input and writing it to a database. It is up to the developer to provide the input and to specify what SQL
should be run for the input. It will run the SQL specified by the
sql
property, in essence reading from the
database, as many times as specified by the
chunk
element's
commit-interval,
and then commit the
whole transaction. Here, you're doing a simple insert. The names and values for the named parameters
are being created by the bean configured for the
itemSqlParameterSourceProvider
property, an instance
of
org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider
, whose
sole job it is to take JavaBean properties and make them available as named parameters corresponding
to the property name on the JavaBean.
<beans:bean
id="jdbcItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter"
p:assertUpdates="true"
p:dataSource-ref="dataSource">
<beans:property name="sql">
<beans:value>
<![CDATA[
insert into USER_REGISTRATION(
FIRST_NAME, LAST_NAME, COMPANY, ADDRESS,
CITY, STATE, ZIP, COUNTY,
URL, PHONE_NUMBER, FAX )
values ( :firstName, :lastName, :company, :address, :city , :state, :
➥
zip, :county, :url, :phoneNumber, :fax )
]]> </beans:value>
</beans:property>
<beans:property name="itemSqlParameterSourceProvider">
<beans:bean
class="org.springframework.batch.item.database.BeanPropertyItemSql
➥
ParameterSourceProvider" />
</beans:property>
</beans:bean>
And that's it! A working solution. With little configuration and no custom code, you've built a
solution for taking large CSV files and reading them into a database. This solution is bare bones and
leaves a lot of edge cases uncared for. You might want to do processing on the item as it's read (before
it's inserted), for example.
This exemplifies a simple
job
. It's important to remember that there are similar classes for doing the
exact opposite transformation: reading from a database and writing to a CSV file.
Search WWH ::
Custom Search