Spring Batch and Spring Integration

怎甘沉沦 提交于 2019-12-01 09:31:55

Absolutely. You can use either of the JDBC ItemReaders 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>

With my current experiance with Spring Batch. If you are going do handle your database call using JdbcTemplate or old way then same POJOs can be reused in different application layers. However, if you paln to use JAXB, JIXB, etc for xml and Hinernate, MyBatis, etc for persiatance then you will run into issues. The POJOs get tighly coouple with underlying specific annotations and api contraints.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!