spring batch 读取多个文件数据导入数据库示例

网友投稿 447 2023-06-06


spring batch 读取多个文件数据导入数据库示例

项目的目录结构

需要读取文件的的数据格式

applicatonContext.xml的配置

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframewzOSXmyFork.org/schema/p"

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd"

default-autowire="byName">

class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">

zOSXmyF

classpath:com/aliyun/springbatch/sample/db/jdbc.properties

xmlns:xsi="http://w3.org/2001/XMLSchema-instance" xmlns:p="http://springframewzOSXmyFork.org/schema/p"

xmlns:tx="http://springframework.org/schema/tx" xmlns:aop="http://springframework.org/schema/aop"

xmlns:context="http://springframework.org/schema/context"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd"

default-autowire="byName">

class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">

class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">

zOSXmyF

classpath:com/aliyun/springbatch/sample/db/jdbc.properties

batch.xml的配置

xmlns:bean="http://springframework.org/schema/beans" xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p" xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop" xmlns:context="http://springframework.org/schema/context"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/batch

http://springframework.org/schema/batch/spring-batch-3.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util.xsd">

commit-interval="1">

scope="step">

class="org.springframework.batch.item.file.FlatFileItemReader">

class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">

zOSXmyF

xmlns:bean="http://springframework.org/schema/beans" xmlns:xsi="http://w3.org/2001/XMLSchema-instance"

xmlns:p="http://springframework.org/schema/p" xmlns:tx="http://springframework.org/schema/tx"

xmlns:aop="http://springframework.org/schema/aop" xmlns:context="http://springframework.org/schema/context"

xmlns:util="http://springframework.org/schema/util"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans-3.0.xsd

http://springframework.org/schema/tx

http://springframework.org/schema/tx/spring-tx-3.0.xsd

http://springframework.org/schema/aop

http://springframework.org/schema/aop/spring-aop-3.0.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context-3.0.xsd

http://springframework.org/schema/batch

http://springframework.org/schema/batch/spring-batch-3.0.xsd

http://springframework.org/schema/util http://springframework.org/schema/util/spring-util.xsd">

commit-interval="1">

commit-interval="1">

scope="step">

scope="step">

class="org.springframework.batch.item.file.FlatFileItemReader">

class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">

class="org.springframework.batch.item.file.FlatFileItemReader">

class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">

class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">

zOSXmyF

jdbc.properties  mysql数据源配置文件

#Oracle

#hibernate.dialect=org.hibernate.dialect.OracleDialect

#validationQuery.sqlserver=SELECT 1 FROM DUAL

#jdbc.driver=oracle.jdbc.driver.OracleDriver

#jdbc.url=jdbc:oracle:thin:@localhost:1521:orcl

#jdbc.username=activitproject

#jdbc.password=activitproject

#Mysql

jdbc.driver=com.mysql.jdbc.Driver

jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_batch_demo

jdbc.username=root

jdbc.password=root

封装数据的实体类就自己写吧

测试主方法:

public static void main(String[] args) {

ApplicationContext context = new ClassPathXmlApplicationContext(

"com/aliyun/springbatch/sample/db/batch.xml");

JobLauncher launcher = (JobLauncher) context.getBean("jobLauncher");

Job job = (Job) context.getBean("dbJob");

try {

// JOB执行,设置参数添加读取文件的路径

JobExecution result = launcher.run(

job,

//添加job参数时,将读取的文件目录加入到job的参数中

new JobParametersBuilder()

.addString("inputFile",

"src/main/java/com/aliyun/springbatch/sample/db/inputFile*.csv")

.toJobParameters());

// 运行结果输出

System.out.println(result.toString());

} catch (Exception e) {

e.printStackTrace();

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:性能优化之代码优化页面加载速度
下一篇:利用java批量给pdf加水印的方法示例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~