最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring batch 讀取多個(gè)文件數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫示例

 更新時(shí)間:2017年03月01日 11:50:25   作者:zhangzhen894095789  
本篇文章主要介紹了spring batch 讀取多個(gè)文件數(shù)據(jù)導(dǎo)入數(shù)據(jù)庫示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

項(xiàng)目的目錄結(jié)構(gòu)

需要讀取文件的的數(shù)據(jù)格式

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>
   
   
   
   <!-- 引入外部數(shù)據(jù)源配置信息 -->
  <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
      <value>classpath:com/aliyun/springbatch/sample/db/jdbc.properties</value>
    </property>
  </bean>
   <!-- 配置數(shù)據(jù)源 -->
  <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條數(shù)據(jù)提交一次事務(wù) -->
  <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">
<!-- 單個(gè)文件讀取 -->
    <!-- <property name="resource" value="file:./sample.csv" /> -->
<!-- 多個(gè)文件讀取 讀取文件的位置 -->
    <bean:property name="resources" value="file:#{jobParameters['inputFile']}" />
  <!-- 引入單個(gè)文件的讀取對(duì)象 -->
    <bean:property name="delegate" ref="flatFileItemReader" />
  </bean:bean>
  <!-- 單個(gè)文件的讀取對(duì)象 -->
  <bean:bean id="flatFileItemReader"
    class="org.springframework.batch.item.file.FlatFileItemReader">
  <!-- 跳過讀取文件的第一行 因?yàn)榈谝恍惺橇忻?->
  <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>
    <!-- 設(shè)置 讀取的字段映射給實(shí)體對(duì)象 -->
    <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數(shù)據(jù)的寫 -->
  <!-- <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> -->
  
  <!-- 這是自定義的實(shí)現(xiàn)ItemWriter接口的ItemWriter的實(shí)現(xiàn)類 -->
<bean:bean id="jdbcItemWriter" class="com.aliyun.springbatch.sample.db.JdbcItemWriter">
</bean:bean>
</bean:beans>

jdbc.properties  mysql數(shù)據(jù)源配置文件

#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

封裝數(shù)據(jù)的實(shí)體類就自己寫吧 

測試主方法:

 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執(zhí)行,設(shè)置參數(shù)添加讀取文件的路徑
      JobExecution result = launcher.run(
          job,
          //添加job參數(shù)時(shí),將讀取的文件目錄加入到j(luò)ob的參數(shù)中
          new JobParametersBuilder()
              .addString("inputFile",
                  "src/main/java/com/aliyun/springbatch/sample/db/inputFile*.csv")
              .toJobParameters());
      // 運(yùn)行結(jié)果輸出
      System.out.println(result.toString());
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SparkSQL讀取hive數(shù)據(jù)本地idea運(yùn)行的方法詳解

    SparkSQL讀取hive數(shù)據(jù)本地idea運(yùn)行的方法詳解

    這篇文章主要介紹了SparkSQL讀取hive數(shù)據(jù)本地idea運(yùn)行的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • java實(shí)現(xiàn)的密碼強(qiáng)度檢測功能完整示例

    java實(shí)現(xiàn)的密碼強(qiáng)度檢測功能完整示例

    這篇文章主要介紹了java實(shí)現(xiàn)的密碼強(qiáng)度檢測功能,結(jié)合完整實(shí)例形式分析了java針對(duì)密碼強(qiáng)度檢測相關(guān)的字符串遍歷、判斷,以及輸出密碼強(qiáng)度等級(jí)相關(guān)操作技巧,需要的朋友可以參考下
    2019-06-06
  • 一文帶你學(xué)會(huì)Spring?JDBC的使用

    一文帶你學(xué)會(huì)Spring?JDBC的使用

    JDBC?就是?數(shù)據(jù)庫開發(fā)?操作的?代名詞,因?yàn)橹灰乾F(xiàn)代商業(yè)項(xiàng)目的開發(fā)那么一定是離不開?數(shù)據(jù)庫?的,不管你搞的是什么,只要是想使用動(dòng)態(tài)的開發(fā)結(jié)構(gòu),那么一定就是?JDBC?,那么下面來教教大家傳統(tǒng)JDBC的使用
    2022-09-09
  • Springboot集成fastDFS配置過程解析

    Springboot集成fastDFS配置過程解析

    這篇文章主要介紹了Springboot集成fastDFS配置過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • springboot?正確的在異步線程中使用request的示例代碼

    springboot?正確的在異步線程中使用request的示例代碼

    這篇文章主要介紹了springboot中如何正確的在異步線程中使用request,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-07-07
  • Java?NIO實(shí)戰(zhàn)之多人聊天室

    Java?NIO實(shí)戰(zhàn)之多人聊天室

    這篇文章主要為大家詳細(xì)介紹了Java?NIO實(shí)戰(zhàn)之多人聊天室,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • 超詳細(xì)解析Spring Bean的創(chuàng)建過程

    超詳細(xì)解析Spring Bean的創(chuàng)建過程

    這篇文章主要揭秘了Spring Bean的創(chuàng)建過程,文中通過代碼示例和圖文結(jié)合的方式解析的超級(jí)詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-05-05
  • java二叉查找樹的實(shí)現(xiàn)代碼

    java二叉查找樹的實(shí)現(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了java二叉查找樹的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • JPA?使用criteria簡單查詢工具類方式

    JPA?使用criteria簡單查詢工具類方式

    這篇文章主要介紹了JPA?使用criteria簡單查詢工具類方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • 詳解Java?Map中三個(gè)冷門容器的使用

    詳解Java?Map中三個(gè)冷門容器的使用

    本篇文章主要講解下Map家族中3個(gè)相對(duì)冷門的容器,分別是WeakHashMap、EnumMap、IdentityHashMap,?想必大家在平時(shí)的工作中也很少用到,或者壓根不知道他們的特性以及適用場景,本篇文章就帶你一探究竟
    2022-12-12

最新評(píng)論

拉萨市| 建瓯市| 金塔县| 长泰县| 噶尔县| 合川市| 舟山市| 肥乡县| 阜宁县| 清镇市| 刚察县| 华安县| 长沙县| 扶余县| 开平市| 山阴县| 武冈市| 土默特左旗| 临夏县| 紫阳县| 大丰市| 阆中市| 洞口县| 雅安市| 吴江市| 万全县| 民勤县| 治县。| 施秉县| 项城市| 香港| 额尔古纳市| 加查县| 泰顺县| 常宁市| 隆尧县| 绿春县| 临漳县| 上思县| 商河县| 于都县|