Spring Batch和Spring集成

编程入门 行业动态 更新时间:2024-10-26 09:23:18
本文介绍了Spring Batch和Spring集成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想使用Spring Batch和Spring Integration从数据库导入数据,并将其写入文件,然后通过ftp将其传输到远程服务器.

I want to use Spring Batch and Spring Integration to import data from database and write them into a file and ftp them to a remote server.

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

But I guess my problem is I don't want to create Domain Object for my table. My queries are random and I want something that just reads the data and writes it to files and transfer.

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

Can I use Spring Batch and Integration without creating respective domain objects?

推荐答案

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

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).

我经常在Spring Batch中执行此操作,这几乎只是将事情连接起来的问题.

I do this pretty often with Spring Batch and it's pretty much just a matter of wiring things up.

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

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="www.springframework/schema/batch" xmlns:beans="www.springframework/schema/beans" xmlns:aop="www.springframework/schema/aop" xmlns:tx="www.springframework/schema/tx" xmlns:p="www.springframework/schema/p" xmlns:xsi="www.w3/2001/XMLSchema-instance" xmlns:util="www.springframework/schema/util" xsi:schemaLocation=" www.springframework/schema/beans www.springframework/schema/beans/spring-beans-2.0.xsd www.springframework/schema/batch www.springframework/schema/batch/spring-batch-2.0.xsd www.springframework/schema/aop www.springframework/schema/aop/spring-aop-2.0.xsd www.springframework/schema/tx www.springframework/schema/tx/spring-tx-2.0.xsd www.springframework/schema/util www.springframework/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>

更多推荐

Spring Batch和Spring集成

本文发布于:2023-08-01 20:13:20,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1271350.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Spring   Batch

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!