diff --git a/pom.xml b/pom.xml index 38d49b1..7445dab 100644 --- a/pom.xml +++ b/pom.xml @@ -14,29 +14,42 @@ spring-batch-lecture Demo project for Spring Boot - 11 + 1.8 org.springframework.boot spring-boot-starter-batch - + + org.springframework.boot + spring-boot-starter-data-jpa + mysql mysql-connector-java runtime - org.springframework.boot - spring-boot-configuration-processor - true + com.h2database + h2 + runtime org.projectlombok lombok true + + org.springframework + spring-oxm + 5.3.7 + + + com.thoughtworks.xstream + xstream + 1.4.16 + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/io/springbatch/springbatchlecture/Customer.java b/src/main/java/io/springbatch/springbatchlecture/Customer.java new file mode 100644 index 0000000..d1559d6 --- /dev/null +++ b/src/main/java/io/springbatch/springbatchlecture/Customer.java @@ -0,0 +1,17 @@ + +package io.springbatch.springbatchlecture; + +import lombok.AllArgsConstructor; +import lombok.Data; + +import java.util.Date; + +@Data +@AllArgsConstructor +public class Customer { + + private final long id; + private final String firstName; + private final String lastName; + private final Date birthdate; +} diff --git a/src/main/java/io/springbatch/springbatchlecture/CustomerRowMapper.java b/src/main/java/io/springbatch/springbatchlecture/CustomerRowMapper.java new file mode 100644 index 0000000..fe34382 --- /dev/null +++ b/src/main/java/io/springbatch/springbatchlecture/CustomerRowMapper.java @@ -0,0 +1,16 @@ +package io.springbatch.springbatchlecture; + +import org.springframework.jdbc.core.RowMapper; + +import java.sql.ResultSet; +import java.sql.SQLException; + +public class CustomerRowMapper implements RowMapper { + @Override + public Customer mapRow(ResultSet rs, int i) throws SQLException { + return new Customer(rs.getLong("id"), + rs.getString("firstName"), + rs.getString("lastName"), + rs.getDate("birthdate")); + } +} diff --git a/src/main/java/io/springbatch/springbatchlecture/JdbcBatchConfiguration.java b/src/main/java/io/springbatch/springbatchlecture/JdbcBatchConfiguration.java new file mode 100644 index 0000000..f1c9373 --- /dev/null +++ b/src/main/java/io/springbatch/springbatchlecture/JdbcBatchConfiguration.java @@ -0,0 +1,100 @@ +package io.springbatch.springbatchlecture; + +import lombok.RequiredArgsConstructor; +import org.springframework.batch.core.*; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.core.launch.support.RunIdIncrementer; +import org.springframework.batch.item.database.*; +import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder; +import org.springframework.batch.item.database.support.MySqlPagingQueryProvider; +import org.springframework.batch.item.json.JacksonJsonObjectMarshaller; +import org.springframework.batch.item.json.JsonFileItemWriter; +import org.springframework.batch.item.json.builder.JsonFileItemWriterBuilder; +import org.springframework.batch.item.xml.StaxEventItemWriter; +import org.springframework.batch.item.xml.builder.StaxEventItemWriterBuilder; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.io.ClassPathResource; +import org.springframework.core.io.FileSystemResource; +import org.springframework.oxm.xstream.XStreamMarshaller; + +import javax.sql.DataSource; +import java.util.*; + +@RequiredArgsConstructor +@Configuration +public class JdbcBatchConfiguration { + + private final JobBuilderFactory jobBuilderFactory; + private final StepBuilderFactory stepBuilderFactory; + private final DataSource dataSource; + + @Bean + public Job job() throws Exception { + return jobBuilderFactory.get("batchJob") + .incrementer(new RunIdIncrementer()) + .start(step1()) + .build(); + } + + @Bean + public Step step1() throws Exception { + return stepBuilderFactory.get("step1") + .chunk(100) + .reader(customItemReader()) + .writer(customItemWriter()) + .build(); + } + + @Bean + public JdbcPagingItemReader customItemReader() { + + JdbcPagingItemReader reader = new JdbcPagingItemReader<>(); + + reader.setDataSource(this.dataSource); + reader.setFetchSize(100); + reader.setRowMapper(new CustomerRowMapper()); + + MySqlPagingQueryProvider queryProvider = new MySqlPagingQueryProvider(); + queryProvider.setSelectClause("id, firstName, lastName, birthdate"); + queryProvider.setFromClause("from customer"); +// queryProvider.setWhereClause("where firstname like :firstname"); + + Map sortKeys = new HashMap<>(1); + + sortKeys.put("id", Order.ASCENDING); + queryProvider.setSortKeys(sortKeys); + reader.setQueryProvider(queryProvider); + + HashMap parameters = new HashMap<>(); + parameters.put("firstname", "A%"); + +// reader.setParameterValues(parameters); + + return reader; + } + + @Bean + public JdbcBatchItemWriter customItemWriter() { + return new JdbcBatchItemWriterBuilder() + .dataSource(dataSource) + .sql("insert into customer2 values (:id, :firstName, :lastName, :birthdate)") + .beanMapped() +// .columnMapped() + .build(); + } + + /*@Bean + public JdbcBatchItemWriter customItemWriter() { + JdbcBatchItemWriter itemWriter = new JdbcBatchItemWriter<>(); + + itemWriter.setDataSource(this.dataSource); + itemWriter.setSql("insert into customer2 values (:id, :firstName, :lastName, :birthdate)"); + itemWriter.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider()); + itemWriter.afterPropertiesSet(); + + return itemWriter; + }*/ +} + diff --git a/src/main/java/io/springbatch/springbatchlecture/SpringBatchLectureApplication.java b/src/main/java/io/springbatch/springbatchlecture/SpringBatchLectureApplication.java index 0d8b2d4..8930a69 100644 --- a/src/main/java/io/springbatch/springbatchlecture/SpringBatchLectureApplication.java +++ b/src/main/java/io/springbatch/springbatchlecture/SpringBatchLectureApplication.java @@ -1,9 +1,11 @@ package io.springbatch.springbatchlecture; +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication +@EnableBatchProcessing public class SpringBatchLectureApplication { public static void main(String[] args) { diff --git a/src/main/resources/application.properties b/src/main/resources/application.properties deleted file mode 100644 index 8b13789..0000000 --- a/src/main/resources/application.properties +++ /dev/null @@ -1 +0,0 @@ - diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml new file mode 100644 index 0000000..4fb13ef --- /dev/null +++ b/src/main/resources/application.yml @@ -0,0 +1,37 @@ +spring: + profiles: + active: local + batch: + job: +# enabled: false + names: ${job.name:NONE} + +--- +spring: + config: + activate: + on-profile: local + datasource: + hikari: + jdbc-url: jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE + username: sa + password: + driver-class-name: org.h2.Driver + batch: + jdbc: + initialize-schema: embedded + +--- +spring: + config: + activate: + on-profile: mysql + datasource: + hikari: + jdbc-url: jdbc:mysql://localhost:3306/springbatch?useUnicode=true&characterEncoding=utf8 + username: root + password: pass + driver-class-name: com.mysql.jdbc.Driver + batch: + jdbc: + initialize-schema: always \ No newline at end of file diff --git a/src/main/resources/customer.json b/src/main/resources/customer.json new file mode 100644 index 0000000..e69de29