This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<dependencies> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-tomcat</artifactId> | |
<scope>provided</scope> | |
</dependency> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Product { | |
private String id; | |
private String name; | |
private Integer price; | |
private String description; | |
public Product(String id, String name, Integer price, String description) { | |
super(); | |
this.id = id; | |
this.name = name; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
public FlatFileItemReader<Product> csvProductReader() { | |
FlatFileItemReader<Product> reader = new FlatFileItemReader<Product>(); | |
reader.setResource(new ClassPathResource("products.csv")); | |
reader.setLineMapper(new DefaultLineMapper<Product>() { | |
{ | |
setLineTokenizer(new DelimitedLineTokenizer() { | |
{ | |
setNames(new String[] { "id", "name", "price", "description" }); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
ItemProcessor<Product, Product> csvProductProcessor() { | |
return new ProductProcessor(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.batch.item.ItemProcessor; | |
import id.edijun.example.springbatch.model.Product; | |
public class ProductProcessor implements ItemProcessor<Product, Product> { | |
private static final Logger log = LoggerFactory.getLogger(ProductProcessor.class); | |
@Override |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
public JdbcBatchItemWriter<Product> csvProductWriter() { | |
JdbcBatchItemWriter<Product> csvProductWriter = new JdbcBatchItemWriter<Product>(); | |
csvProductWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<Product>()); | |
csvProductWriter.setSql("INSERT INTO product (id, name, price, description) VALUES (:id, :name, :price, :description)"); | |
csvProductWriter.setDataSource(dataSource); | |
return csvProductWriter; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
public Step csvFileToDatabaseStep() { | |
return stepBuilderFactory | |
.get("csvFileToDatabaseStep") | |
.<Product, Product>chunk(1) | |
.reader(csvProductReader()) | |
.processor(csvProductProcessor()) | |
.writer(csvProductWriter()) | |
.build(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Bean | |
Job csvFileToDatabaseJob(JobCompletionNotificationListener listener) { | |
return jobBuilderFactory | |
.get("csvFileToDatabaseJob") | |
.incrementer(new RunIdIncrementer()).listener(listener) | |
.flow(csvFileToDatabaseStep()) | |
.end().build(); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Override | |
public void setDataSource(DataSource dataSource) { | |
// override to do not set datasource even if a datasource exist. | |
// initialize will use a Map based JobRepository (instead of database) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.sql.ResultSet; | |
import java.sql.SQLException; | |
import java.util.List; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import org.springframework.batch.core.BatchStatus; | |
import org.springframework.batch.core.JobExecution; | |
import org.springframework.batch.core.listener.JobExecutionListenerSupport; |
OlderNewer