Java Reference
In-Depth Information
2. Update the SQL to use named parameters instead of question marks for
The org.springframework.batch.item.database.ItemSqlParameterSourceProvider interface is
slightly different from the ItemPreparedStatementSetter interface in that it doesn't set the parameters on
the statement to be executed. Instead, an implementation of the ItemSqlParameterSourceProvider 's
responsibility is to extract the parameter values from an item and return them as an
org.springframework.jdbc.core.namedparam.SqlParameterSource object.
The nice thing about this approach is that not only is it the safer approach (no concerns about
needing to keep the SQL in the XML file in synch with the code of the ItemPreparedStatementSetter
implementation) but Spring Batch provides implementations of this interface that allow you to use
convention over code to extract the values from the items. In this example, you use Spring Batch's
BeanPropertyItemSqlParameterSourceProvider (try saying that three times fast) to extract the values from
the items to be populated in the SQL. Listing 9-30 shows the updated jdbcBatchWriter configuration for
this change.
Listing 9-30. jdbcBatchWriter using BeanPropertyItemSqlParameterSourceProvider
<beans:bean id="jdbcBatchWriter"
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="sql" value="insert into customer (firstName, middleInitial, lastName,
address, city, state, zip) values (:firstName, :middleInitial, :lastName, :address, :city,
:state, :zip)"/>
<beans:property name="itemSqlParameterSourceProvider">
You can quickly note in Listing 9-30 that there is no reference to the ItemPreparedStatementSetter
implementation. By using this configuration, you don't need any custom code. Yet the results are the
Although JDBC is known for its speed compared to other persistence framework that lie on top of it,
other frameworks are popular in the enterprise. Next you look at how to use the most popular of those to
do database writing: Hibernate.
When you have most of your database tables and applications already mapped with Hibernate, reusing
all that is a logical choice to start. You saw how Hibernate works as a competent reader in Chapter 7.
This section looks at how you can use HibernateItemWriter to write the changes to a database.
Like JdbcBatchItemWriter , org.springframework.batch.item.database.HibernateItemWriter serves
as a thin wrapper to Spring's org.springframework.orm.hibernate3.HibernateTemplate . When a chunk
completes, the list of items is passed to HibernateItemWriter where HibernateTemplate 's saveOrUpdate
method is called for each item. When all the items have been saved or updated, HibernateItemWriter
makes a single call to HibernateTemplate 's flush method, executing all the changes at once. This
Search WWH ::

Custom Search