Java Reference
In-Depth Information
In order for paging to work, you need to be able to query based on a page size and page number (the
number of records to return and which page you are currently processing). For example, if your total
number of records is 10,000 and your page size is 100 records, you need to be able to specify that you are
requesting the 20 th page of 100 records (or records 2,000 through 2100). To do this, you provide an
implementation of the org.springframework.batch.item.database.PagingQueryProvider interface to the
JdbcPagingItemReader. The PagingQueryProvider interface provides all of the functionality required to
navigate a paged ResultSet.
Unfortunately, each database offers its own paging implementation. Because of this, you have the
following two options:
1.
Configure a database-specific implementation of the PagingQueryProvider. As
of this writing, Spring Batch provides implementations for DB2, Derby, H2,
HSql, MySql, Oracle, Postgres, SqlServer, and Sybase.
2. Configure your reader to use the
org.springframework.batch.item.database.support.SqlPagingQueryProviderF
actoryBean . This factory detects what database implementation to use.
Although the easier route is definitely the SqlPagingQueryProviderFactoryBean, it is important to
note that each of the different databases implement paging in a different way. Because of this, you may
want to use database specific options when tuning your jobs. Given that the analysis of each database
type is out of the scope for this topic, you will use the SqlPagingQueryProviderFactoryBean for your
example.
To configure the JdbcPagingItemReader, you have four dependencies: a datasource, the
PagingQueryProvider implementation, your RowMapper implementation, and the size of your page.
You also have the opportunity to configure your SQL statement's parameters to be injected by Spring.
Listing 7-42 shows the configuration for the JdbcPagingItemReader.
Listing 7-42. JdbcPagingItemReader Configuration
<beans:bean id="customerItemReader"
class="org.springframework.batch.item.database.JdbcPagingItemReader"
scope="step">
<beans:property name="dataSource" ref="dataSource"/>
<beans:property name="queryProvider">
<beans:bean class="org.springframework.batch.item.database.support.
SqlPagingQueryProviderFactoryBean">
<beans:property name="selectClause" value="select *"/>
<beans:property name="fromClause" value="from Customer"/>
<beans:property name="whereClause" value="where city = :city"/>
<beans:property name="sortKey" value="lastName"/>
<beans:property name="dataSource" ref="dataSource"/>
</beans:bean>
</beans:property>
<beans:property name="parameterValues">
<beans:map>
<beans:entry key="city" value="#{jobParameters[city]}"/>
</beans:map>
</beans:property>
<beans:property name="pageSize" value="10"/>
<beans:property name="rowMapper" ref="customerRowMapper"/>
 
Search WWH ::




Custom Search