分享

java – Spring Batch和Spring Integration

 印度阿三17 2019-10-08

我想使用Spring Batch和Spring Integration从数据库导入数据并将它们写入文件并将它们ftp到远程服务器.

但我想我的问题是我不想为我的表创建域对象.我的查询是随机的,我想要的东西只是读取数据并将其写入文件和传输.

我可以在不创建相应域对象的情况下使用Spring Batch和Integration吗?

解决方法:

绝对.您可以使用JDBC ItemReaders或JPA ItemReader与ColumnMapRowMapper一起检索结果集的Map.您可以非常简单地使用FlatFileItemWriter以您喜欢的任何格式输出数据(使用提供的类分隔非常容易;固定宽度意味着编写一个类将Map转换为固定宽度字符串).

我经常使用Spring Batch做这件事,而这几乎只是一个问题.

除了定义资源,数据源和提供SQL之外,这个(未经测试的)配置几乎完全符合您的要求:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www./schema/batch"
    xmlns:beans="http://www./schema/beans" xmlns:aop="http://www./schema/aop"
    xmlns:tx="http://www./schema/tx" xmlns:p="http://www./schema/p"
    xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:util="http://www./schema/util"
    xsi:schemaLocation="
        http://www./schema/beans http://www./schema/beans/spring-beans-2.0.xsd
        http://www./schema/batch http://www./schema/batch/spring-batch-2.0.xsd
        http://www./schema/aop http://www./schema/aop/spring-aop-2.0.xsd
        http://www./schema/tx http://www./schema/tx/spring-tx-2.0.xsd
        http://www./schema/util http://www./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>
来源:https://www./content-1-494901.html

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多