欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-03-05 10:27:30
项目的目录结构 需要读取文件的的数据格式 applicatoncontext.xml的配置

项目的目录结构

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

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

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。