I'm unable to save data into DB from CSV
file in Spring Batch
. I've taken a reference from link: http://www.javabeat.net/spring-batch-example/?et_monarch_popup=true, when executing the Main
method, I see following tables gets created, but it don't create Employee
table. Why? Please guide, what's wrong happening here?
This doesn't have employee table, also if I create it, it doesn't save data for employee table.
Employee.java
@XmlRootElement(name="employee")
public class Employee implements Serializable {
private static final long serialVersionUID = 1L;
private String empId;
private String city;
private String country;
@XmlElement(name="EMP_ID")
public String getEmpId() {
return empId;
}
public void setEmpId(String empId) {
this.empId = empId;
}
@XmlElement(name="CITY")
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
@XmlElement(name="COUNTRY")
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
EmployeeDataMapper.java
public class EmployeeDataMapper implements FieldSetMapper<Employee>{
@Override
public Employee mapFieldSet(FieldSet fieldSet) throws BindException {
Employee employee = new Employee();
employee.setEmpId(fieldSet.readString(0));
employee.setCity(fieldSet.readString(1));
employee.setCountry(fieldSet.readString(2));
return employee;
}
}
OrderItemWriter.java
public class OrderItemWriter implements ItemWriter<Employee> {
private static final String INSERT_EMPLOYEE = "insert into employee(empid,city,country) values(?,?,?)";
private JdbcTemplate jdbcTemplate;
public void write(List<? extends Employee> employees) throws Exception {
for (Employee order : employees) {
jdbcTemplate.update(INSERT_EMPLOYEE, order.getCity(),order.getCountry(), order.getCountry());
}
}
public OrderItemWriter(DataSource ds) {
this.jdbcTemplate = new JdbcTemplate(ds);
}
}
Main.java
public class Main {
public static void main(String[] args) {
final String[] config = { "job-config.xml" };
ApplicationContext context = new ClassPathXmlApplicationContext(config);
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
Job job = (Job) context.getBean("employeeJob");
try {
JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("Exit Status : " + execution.getStatus());
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("~~~~~ Finished Execution of Batch Job ~~~~~~");
}
}
job-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- stored job-meta in memory
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager" /> </bean>-->
<!-- stored job-meta in database -->
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.JobRepositoryFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="transactionManager" ref="transactionManager" />
<property name="databaseType" value="mysql" />
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
</beans>
job-config.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:batch="http://www.springframework.org/schema/batch"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-3.0.xsd">
<import resource="datasource-config.xml" />
<import resource="job-context.xml" />
<!-- JOB -->
<job id="employeeJob" xmlns="http://www.springframework.org/schema/batch">
<step id="employeeprocessor">
<tasklet>
<chunk reader="reader" writer="writer" commit-interval="3" skip-limit="2">
<skippable-exception-classes>
<include class="org.springframework.batch.item.file.FlatFileParseException" />
</skippable-exception-classes>
</chunk>
</tasklet>
</step>
</job>
<!-- ItemReader -->
<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="classpath:input/employees.csv" />
<property name="linesToSkip" value="1" />
<property name="lineMapper">
<bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
<property name="lineTokenizer">
<bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="EMP_ID,CITY,COUNTRY" />
<property name="delimiter" value="," />
</bean>
</property>
<property name="fieldSetMapper">
<bean class="javabeat.net.EmployeeDataMapper" />
</property>
</bean>
</property>
</bean>
<bean id="writer" class="javabeat.net.OrderItemWriter">
<constructor-arg ref="dataSource" />
</bean>
</beans>
datasource-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:property-placeholder location="classpath:database.properties" />
<!-- connect to database -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.mysql.driverClassName}" />
<property name="url" value="${jdbc.mysql.url}" />
<property name="username" value="${jdbc.mysql.username}" />
<property name="password" value="${jdbc.mysql.password}" />
</bean>
<bean id="transactionManager"
class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<!-- create job-meta tables automatically In production you don't need to
create this every time. Just create once in production. -->
<jdbc:initialize-database data-source="dataSource">
<jdbc:script location="org/springframework/batch/core/schema-drop-mysql.sql" />
<jdbc:script location="org/springframework/batch/core/schema-mysql.sql" />
</jdbc:initialize-database>
</beans>
employees.csv
1,Pune,India
Edit-1: When I debug the code, I see debug coming on below code
public OrderItemWriter(DataSource ds) {
this.jdbcTemplate = new JdbcTemplate(ds);
}
But write()
method not executing. Why ?
DB table I created for reference:
CREATE TABLE `batch`.`employee` (
`empId` VARCHAR(40) NOT NULL,
`city` VARCHAR(45) NULL,
`country` VARCHAR(45) NULL,
PRIMARY KEY (`empId`)
);
database.properties:
jdbc.mysql.driverClassName=com.mysql.jdbc.Driver
jdbc.mysql.url=jdbc:mysql://localhost:3306/batch
jdbc.mysql.username=root
jdbc.mysql.password=password
pom.xml
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
<springframework.version>4.2.4.RELEASE</springframework.version>
<springbatch.version>3.0.6.RELEASE</springbatch.version>
<mysql.version>5.1.31</mysql.version>
<joda-time.version>2.8</joda-time.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring XML to/back object -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${springframework.version}</version>
</dependency>
<!-- Spring Batch -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>${springbatch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>${springbatch.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.version}</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>${joda-time.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>