项目的目录结构

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

applicatonContext.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd"
default-autowire="byName">
<context:component-scan base-package="com.aliyun.springbatch" /> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository"/>
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager">
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 引入外部数据源配置信息 -->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:com/aliyun/springbatch/sample/db/jdbc.properties</value>
</property>
</bean>
<!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
</beans>

batch.xml的配置

<?xml version="1.0" encoding="UTF-8"?>
<bean:beans xmlns="http://www.springframework.org/schema/batch"
xmlns:bean="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> <!-- <bean:import resource="dataSource.xml" /> -->
<bean:import resource="applicationContext.xml" />
<!-- Job的配置信息 -->
<!-- commit-interval="1" 表示每处理完1条数据提交一次事务 -->
<job id="dbJob">
<step id="dbReadAndWriterStep" >
<tasklet>
<chunk reader="userReader" writer="jdbcItemWriter"
commit-interval="1">
</chunk>
</tasklet>
</step>
</job> <!-- <bean:bean id="jdbcItemReader" class="org.springframework.batch.item.database.JdbcCursorItemReader"
scope="step"> <bean:property name="dataSource" ref="dataSource" /> <bean:property
name="sql" value="select id,name,age,score from t_user" /> <bean:property
name="rowMapper"> <bean:bean class="org.springframework.jdbc.core.BeanPropertyRowMapper">
<bean:property name="mappedClass" value="com.aliyun.springbatch.sample.db.User"
/> </bean:bean> </bean:property> </bean:bean> -->
<!-- 读文件 多文件上传-->
<bean:bean id="userReader" class="org.springframework.batch.item.file.MultiResourceItemReader"
scope="step">
<!-- 单个文件读取 -->
<!-- <property name="resource" value="file:./sample.csv" /> -->
<!-- 多个文件读取 读取文件的位置 -->
<bean:property name="resources" value="file:#{jobParameters['inputFile']}" />
<!-- 引入单个文件的读取对象 -->
<bean:property name="delegate" ref="flatFileItemReader" />
</bean:bean>
<!-- 单个文件的读取对象 -->
<bean:bean id="flatFileItemReader"
class="org.springframework.batch.item.file.FlatFileItemReader">
<!-- 跳过读取文件的第一行 因为第一行是列名-->
<bean:property name="linesToSkip" value="1"/>
<!-- 文件的行映射 -->
<bean:property name="lineMapper">
<bean:bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<!-- 行的字段映射 -->
<bean:property name="lineTokenizer">
<!-- 映射的字段以下面names属性,以,隔开 -->
<bean:bean
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<bean:property name="names" value="id,name,age,score" />
</bean:bean>
</bean:property>
<!-- 设置 读取的字段映射给实体对象 -->
<bean:property name="fieldSetMapper">
<bean:bean
class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<bean:property name="prototypeBeanName" value="user" />
</bean:bean>
</bean:property>
</bean:bean>
</bean:property>
</bean:bean> <bean:bean id="user" class="com.aliyun.springbatch.sample.db.User"></bean:bean>
<!-- db数据的写 -->
<!-- <bean:bean id="jdbcItemWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<bean:property name="dataSource" ref="dataSource" />
<bean:property name="sql"
value="insert into T_DESTUSER (ID,USERID,USERNAME,PASSWORD,UPDATETIME,UPDATEUSER)
values
(:id,:userId,:userName,:password,:updateDate,:updateUser)" />
<bean:property name="itemSqlParameterSourceProvider">
<bean:bean
class="org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider" />
</bean:property>
</bean:bean> --> <!-- 这是自定义的实现ItemWriter接口的ItemWriter的实现类 -->
<bean:bean id="jdbcItemWriter" class="com.aliyun.springbatch.sample.db.JdbcItemWriter">
</bean:bean>
</bean:beans>

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();
}
}

over::

spring batch 读取多个文件数据导入数据库的更多相关文章

  1. larave5.6 将Excel文件数据导入数据库代码实例

    <?php namespace App\Admin\Controllers; use App\AdminUser; use Illuminate\Http\Request; use Excel; ...

  2. 小数据量csv文件数据导入数据库(思路)

    大致写写思路,因为sqlserver提供了可以直接导入的图形界面. 1.private static string GetConnectionString(string folderPath)  // ...

  3. 读取Execl表数据 导入数据库

    不知不觉博客园园林都两年多了,我是今年毕业的应届生,最近公司项目需要改动,很多的数据需要导入,很多的实体类需要些.考虑到这些问题自己写了两个winform版的小工具,一个是读取Execl数据导入数据库 ...

  4. MsSqlServer bak文件数据导入

    MsSqlServer  bak文件数据导入 第一步首先在你的数据库中建立一个空数据库 选中新建的数据库 鼠标右键 任务 还原 数据库 这个时候会弹出这种一个框 之后选择原设备 会弹出 点击加入 找到 ...

  5. springMVC(5)---导入excel文件数据到数据库

    springMVC(5)---导入excel文件数据到数据库 上一篇文章写了从数据库导出数据到excel文件,这篇文章悄悄相反,写的是导入excel文件数据到数据库.上一篇链接:springMVC(4 ...

  6. C#选择多个文件并读取多个文件数据

    原文:C#选择多个文件并读取多个文件数据 版权声明:本文为博主原创文章,转载请附上链接地址. https://blog.csdn.net/ld15102891672/article/details/8 ...

  7. 将Excel中数据导入数据库(一)

    在工作中经常要将Excel中数据导入数据库,这里介绍一种方法. 假如Excel中的数据如下: 数据库建表如下: 其中Id为自增字段: Excel中数据导入数据库帮助类如下: using System; ...

  8. 使用python将excel数据导入数据库

    使用python将excel数据导入数据库 因为需要对数据处理,将excel数据导入到数据库,记录一下过程. 使用到的库:xlrd 和 pymysql (如果需要写到excel可以使用xlwt) 直接 ...

  9. 使用openpyxl模块将Excel中的数据导入数据库

    这里将不介绍openpyxl模块的详细操作. 主要就是记录一个使用openpyxl模块将Excel表格的数据导入数据库中的实例. from openpyxl import load_workbook ...

随机推荐

  1. Java探索之旅(14)——文本I/O与读写

    1文件类File    ❶封装文件或路径的属性.不包括创建和读写文件操作.File实例并不会实际创建文件.不论文件存在与否,可以创建任意文件名的实例.两种实例创建方式如下:               ...

  2. !important定义为最高级不可替代

    <!DOCTYPE html> /*!important定义为最高级不可替代*/ <html lang="en"> <head> <met ...

  3. 将gridview 的数据导出EXCEL

    gridview数据 单击“导出EXCEL”按钮后        1.在上面的代码中,先将gridview绑定到指定的数据源中,然后在button按钮(用来做导出到EXCEL的)的事件中,写入相关的代 ...

  4. ubuntu下root用户默认密码及修改方法

    [ubuntu下root用户默认密码及修改方法] 很多朋友用ubuntu,一般都是装完ubuntu系统,马上就修改root密码了,那么root用户的默认密码是多少,当忘记root用户密码时如何找回呢, ...

  5. R语言对矩阵按某一列排序

    [plain] view plaincopy a <- c(5,4,3,2,1) b <- c(1,2,3,4,5) c <- cbind(a,b) [plain] view pla ...

  6. java File基本操作,以及递归遍历文件夹

    java 的文件操作,相对来说是比较重要的,无论是编写CS还是BS程序,都避免不了要与文件打交道,例如读写配置文件等.虽然现在很多框架都直接帮你做好了这一步! java.io.File 底层是调用与c ...

  7. CEPH安装教程(上)

    环境拓扑 主机 配置 地址 运行服务 node CPU:1 内存:2GB 磁盘:vda(20GB) br-mgmt:92.0.0.250 br-ex:192.168.203.250/19 ntp an ...

  8. Spring第一天:Spring的概述、SpringIOC入门(XML)、Spring的Bean管理、Spring属性注入

    记得引入约束 上图路径. 此时 只需修改配置文件 便可以随意更换实现类 无需修改代码. 传统方法必须用实现类(不面向接口了)来调用方法设置属性. 而在Spring中:在创建类的过程中发现实现类有nam ...

  9. 「BZOJ3600」没有人的算术 替罪羊树+线段树

    题目描述 过长--不想发图也不想发文字,所以就发链接吧-- 没有人的算术 题解 \(orz\)神题一枚 我们考虑如果插入的数不是数对,而是普通的数,这就是一道傻题了--直接线段树一顿乱上就可以了. 于 ...

  10. HDU2050 折线分割平面

    题目:acm.hdu.edu.cn/showproblem.php?pid=2050 递推: 从直线入手,第n条直线,最多和平面上的直线有n-1个交点,多出(n-1)+1个部分 序号 1 2 3 .. ...