Absolutely. You can use either of the JDBC ItemReader
s or the JPA ItemReader
with a ColumnMapRowMapper
to retrieve a Map
of the result set. You can use the FlatFileItemWriter
pretty simply to output the data in whatever format you like (delimited being very easy with the provided classes; fixed width means writing one class to translate the Map
to your fixed width string).
I do this pretty often with Spring Batch and it's pretty much just a matter of wiring things up.
Aside from defining a resource, data source, and providing the SQL, this (untested) configuration would pretty much do exactly what you're asking:
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.0.xsd">
<job-repository id="jobRepository"
data-source="jobDataSource"/>
<beans:bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager"
p:dataSource-ref="jobDataSource" />
<beans:bean id="extractReader" scope="step"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<beans:property name="dataSource" ref="appDataSource" />
<beans:property name="rowMapper">
<beans:bean
class="org.springframework.jdbc.core.ColumnMapRowMapper" />
</beans:property>
<beans:property name="sql">
<beans:value>
. . .
</beans:value>
</beans:property>
</beans:bean>
<beans:bean id="extractWriter"
class="org.springframework.batch.item.file.FlatFileItemWriter" scope="step">
<beans:property name="resource" ref="fileResource" />
<beans:property name="lineAggregator">
<beans:bean
class="org.springframework.batch.item.file.transform.DelimitedLineAggregator">
<beans:property name="delimiter">
<util:constant
static-field="org.springframework.batch.item.file.transform.DelimitedLineTokenizer.DELIMITER_TAB" />
</beans:property>
<beans:property name="fieldExtractor">
<beans:bean
class="org.springframework.batch.item.file.transform.PassThroughFieldExtractor" />
</beans:property>
</beans:bean>
</beans:property>
</beans:bean>
<job id="extractJob" restartable="true">
<step id="extractStep" >
<tasklet>
<chunk reader="extractReader" writer="extractWriter"
commit-interval="100" />
</tasklet>
</step>
</job>
</beans:beans>