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