Spring Batch Connect to Oracle Example

Spring Batch Connect to Oracle Example:

Application.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-2.5.xsd"

    default-autowire="byName">

 

    <context:property-placeholder location="classpath:/com/ermdashboard/loadCSV/batch-oracle.properties"/>

    <beanclass="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>

   

    <bean id="dataSource"class="org.springframework.jdbc.datasource.SingleConnectionDataSource">

       <propertyname="driverClassName" value="${datasource.driver}" />

       <property name="url" value="${datasource.url}"/>

       <property name="username"value="${datasource.username}" />

       <property name="password"value="${datasource.password}" />

       <propertyname="suppressClose" value="true" />

    </bean>

   

    <bean id="jobLauncher"class="org.springframework.batch.core.launch.support.SimpleJobLauncher">

        <propertyname="jobRepository" ref="jobRepository"/>

    </bean>

 

    <bean id="jobRepository"class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean"/>

 

    <bean id="transactionManager"class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>

   

    <beanclass="org.springframework.jdbc.core.JdbcTemplate">

            <constructor-arg ref="dataSource"/>

      </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"

   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-2.5.xsd

http://www.springframework.org/schema/batch

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

   

    <job id="csvJob">

        <step id="csvStep">

            <tasklettransaction-manager="transactionManager">

                <chunkreader="csvItemReader" writer="csvItemWriter"processor="csvItemProcessor" commit-interval="1">

                </chunk>

            </tasklet>

        </step>

    </job>

   

    <bean:beanid="csvItemProcessor" class="com.ermdashboard.loadCSV.CsvItemProcessor">

    </bean:bean>

   

    <bean:bean id="csvItemReader"

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

        <bean:propertyname="resource" value="classpath:inputFile.csv"/>

        <bean:propertyname="lineMapper">

            <bean:bean

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

                <bean:propertyname="lineTokenizer" ref="lineTokenizer"/>

                <bean:propertyname="fieldSetMapper">

                    <bean:bean

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

                        <bean:propertyname="prototypeBeanName" value="student"></bean:property>

                    </bean:bean>

                </bean:property>

            </bean:bean>

        </bean:property>

    </bean:bean>

 

    <bean:bean id="student"class="com.ermdashboard.loadCSV.Student"></bean:bean>

 

    <bean:bean id="lineTokenizer"class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">

        <bean:propertyname="delimiter" value=","/>

        <bean:propertyname="names">

            <bean:list>

                <bean:value>ID</bean:value>

                <bean:value>name</bean:value>

                <bean:value>age</bean:value>

                <bean:value>score</bean:value>

            </bean:list>

        </bean:property>

    </bean:bean>

   

    <bean:bean id="csvItemWriter"class="com.ermdashboard.loadCSV.WriteToDB">

        <bean:constructor-arg ref="dataSource"/>

    </bean:bean>

</bean:beans>

 

writeToDo.java

import java.util.List;

import javax.sql.DataSource;

importorg.springframework.batch.item.ItemWriter;

importorg.springframework.jdbc.core.JdbcTemplate;

 

public class WriteToDB implements ItemWriter<Student>{

    private static final String INSERT_PRODUCT = "INSERT INTO ERM_MAIN(MAIN_COL1,MAIN_COL2,MAIN_COL3,MAIN_COL4,MAIN_COL5,MAIN_COL6) VALUES(?,?,?,?,?,?)";     

      private static final String UPDATE_PRODUCT = "update ERM_MAIN setMAIN_COL2=?, MAIN_COL3=?, MAIN_COL4=?, MAIN_COL5=?, MAIN_COL6=? where MAIN_COL1= ?";     

      private JdbcTemplate jdbcTemplate

      public WriteToDB(DataSource dataSource) {

            System.out.println("dataSource="+dataSource);

            this.jdbcTemplate = newJdbcTemplate(dataSource);

      }

      public WriteToDB(){

           

      }

      /* (non-Javadoc)

       *@see org.springframework.batch.item.ItemWriter#write(java.util.List)

       */

      public void write(List<? extends Student> items) throws Exception {

           

            for(Student item : items) {

                  int updated =jdbcTemplate.update(UPDATE_PRODUCT,

                              '1','2','3','4','5',401

                  );

                  if(updated == 0) {

                        jdbcTemplate.update(

                              INSERT_PRODUCT,

                              '1','2','3','4','5',401

                        );   

                  }                                  

            }

      }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值