diff --git a/70.spring-batch-itemprocessor/pom.xml b/70.spring-batch-itemprocessor/pom.xml new file mode 100644 index 00000000..63920a4b --- /dev/null +++ b/70.spring-batch-itemprocessor/pom.xml @@ -0,0 +1,49 @@ + + + 4.0.0 + + org.springframework.boot + spring-boot-starter-parent + 2.2.5.RELEASE + + + cc.mrbird + spring-batch-itemprocessor + 0.0.1-SNAPSHOT + spring-batch-itemprocessor + Demo project for Spring Boot + + + 1.8 + + + + + org.springframework.boot + spring-boot-starter-batch + + + mysql + mysql-connector-java + + + org.springframework.boot + spring-boot-starter-jdbc + + + org.springframework.boot + spring-boot-starter-validation + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/SpringBatchItemprocessorApplication.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/SpringBatchItemprocessorApplication.java new file mode 100644 index 00000000..e035a83a --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/SpringBatchItemprocessorApplication.java @@ -0,0 +1,15 @@ +package cc.mrbird.batch; + +import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +@EnableBatchProcessing +public class SpringBatchItemprocessorApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBatchItemprocessorApplication.class, args); + } + +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/TestData.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/TestData.java new file mode 100644 index 00000000..9da53d46 --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/TestData.java @@ -0,0 +1,57 @@ +package cc.mrbird.batch.entity; + +import javax.validation.constraints.NotBlank; + +/** + * @author MrBird + */ +public class TestData { + + private int id; + private String field1; + private String field2; + @NotBlank + private String field3; + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public String getField1() { + return field1; + } + + public void setField1(String field1) { + this.field1 = field1; + } + + public String getField2() { + return field2; + } + + public void setField2(String field2) { + this.field2 = field2; + } + + public String getField3() { + return field3; + } + + public void setField3(String field3) { + this.field3 = field3; + } + + @Override + public String toString() { + return "TestData{" + + "id=" + id + + ", field1='" + field1 + '\'' + + ", field2='" + field2 + '\'' + + ", field3='" + field3 + '\'' + + '}'; + } +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/BeanValidatingItemProcessorDemo.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/BeanValidatingItemProcessorDemo.java new file mode 100644 index 00000000..bad2e937 --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/BeanValidatingItemProcessorDemo.java @@ -0,0 +1,50 @@ +package cc.mrbird.batch.entity.job; + +import cc.mrbird.batch.entity.TestData; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.batch.item.validator.BeanValidatingItemProcessor; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class BeanValidatingItemProcessorDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + @Autowired + private ListItemReader simpleReader; + + @Bean + public Job beanValidatingItemProcessorJob() throws Exception { + return jobBuilderFactory.get("beanValidatingItemProcessorJob") + .start(step()) + .build(); + } + + private Step step() throws Exception { + return stepBuilderFactory.get("step") + .chunk(2) + .reader(simpleReader) + .processor(beanValidatingItemProcessor()) + .writer(list -> list.forEach(System.out::println)) + .build(); + } + + private BeanValidatingItemProcessor beanValidatingItemProcessor() throws Exception { + BeanValidatingItemProcessor beanValidatingItemProcessor = new BeanValidatingItemProcessor<>(); + // 开启过滤,不符合规则的数据被过滤掉; + beanValidatingItemProcessor.setFilter(true); + beanValidatingItemProcessor.afterPropertiesSet(); + return beanValidatingItemProcessor; + } +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/CompositeItemProcessorDemo.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/CompositeItemProcessorDemo.java new file mode 100644 index 00000000..b3483b2d --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/CompositeItemProcessorDemo.java @@ -0,0 +1,61 @@ +package cc.mrbird.batch.entity.job; + +import cc.mrbird.batch.entity.TestData; +import cc.mrbird.batch.processor.TestDataFilterItemProcessor; +import cc.mrbird.batch.processor.TestDataTransformItemPorcessor; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.batch.item.support.CompositeItemProcessor; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +import java.util.Arrays; +import java.util.List; + +/** + * @author MrBird + */ +@Component +public class CompositeItemProcessorDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + @Autowired + private ListItemReader simpleReader; + @Autowired + private TestDataFilterItemProcessor testDataFilterItemProcessor; + @Autowired + private TestDataTransformItemPorcessor testDataTransformItemPorcessor; + + @Bean + public Job compositeItemProcessorJob() { + return jobBuilderFactory.get("compositeItemProcessorJob") + .start(step()) + .build(); + } + + private Step step() { + return stepBuilderFactory.get("step") + .chunk(2) + .reader(simpleReader) + .processor(compositeItemProcessor()) + .writer(list -> list.forEach(System.out::println)) + .build(); + } + + // CompositeItemProcessor组合多种中间处理器 + private CompositeItemProcessor compositeItemProcessor() { + CompositeItemProcessor processor = new CompositeItemProcessor<>(); + List> processors = Arrays.asList(testDataFilterItemProcessor, testDataTransformItemPorcessor); + // 代理两个processor + processor.setDelegates(processors); + return processor; + } +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/TestDataFilterItemProcessorDemo.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/TestDataFilterItemProcessorDemo.java new file mode 100644 index 00000000..47da7e17 --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/TestDataFilterItemProcessorDemo.java @@ -0,0 +1,44 @@ +package cc.mrbird.batch.entity.job; + +import cc.mrbird.batch.entity.TestData; +import cc.mrbird.batch.processor.TestDataFilterItemProcessor; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class TestDataFilterItemProcessorDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + @Autowired + private ListItemReader simpleReader; + @Autowired + private TestDataFilterItemProcessor testDataFilterItemProcessor; + + @Bean + public Job testDataFilterItemProcessorJob() { + return jobBuilderFactory.get("testDataFilterItemProcessorJob") + .start(step()) + .build(); + } + + private Step step() { + return stepBuilderFactory.get("step") + .chunk(2) + .reader(simpleReader) + .processor(testDataFilterItemProcessor) + .writer(list -> list.forEach(System.out::println)) + .build(); + } +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/TestDataTransformItemPorcessorDemo.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/TestDataTransformItemPorcessorDemo.java new file mode 100644 index 00000000..69ec0e76 --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/TestDataTransformItemPorcessorDemo.java @@ -0,0 +1,45 @@ +package cc.mrbird.batch.entity.job; + +import cc.mrbird.batch.entity.TestData; +import cc.mrbird.batch.processor.TestDataFilterItemProcessor; +import cc.mrbird.batch.processor.TestDataTransformItemPorcessor; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class TestDataTransformItemPorcessorDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + @Autowired + private ListItemReader simpleReader; + @Autowired + private TestDataTransformItemPorcessor testDataTransformItemPorcessor; + + @Bean + public Job testDataTransformItemPorcessorJob() { + return jobBuilderFactory.get("testDataTransformItemPorcessorJob") + .start(step()) + .build(); + } + + private Step step() { + return stepBuilderFactory.get("step") + .chunk(2) + .reader(simpleReader) + .processor(testDataTransformItemPorcessor) + .writer(list -> list.forEach(System.out::println)) + .build(); + } +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/ValidatingItemProcessorDemo.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/ValidatingItemProcessorDemo.java new file mode 100644 index 00000000..53bbf7a9 --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/job/ValidatingItemProcessorDemo.java @@ -0,0 +1,55 @@ +package cc.mrbird.batch.entity.job; + +import cc.mrbird.batch.entity.TestData; +import org.springframework.batch.core.Job; +import org.springframework.batch.core.Step; +import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; +import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.batch.item.validator.ValidatingItemProcessor; +import org.springframework.batch.item.validator.ValidationException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class ValidatingItemProcessorDemo { + + @Autowired + private JobBuilderFactory jobBuilderFactory; + @Autowired + private StepBuilderFactory stepBuilderFactory; + @Autowired + private ListItemReader simpleReader; + + @Bean + public Job validatingItemProcessorJob() { + return jobBuilderFactory.get("validatingItemProcessorJob") + .start(step()) + .build(); + } + + private Step step() { + return stepBuilderFactory.get("step") + .chunk(2) + .reader(simpleReader) + .processor(validatingItemProcessor()) + .writer(list -> list.forEach(System.out::println)) + .build(); + } + + private ValidatingItemProcessor validatingItemProcessor() { + ValidatingItemProcessor processor = new ValidatingItemProcessor<>(); + processor.setValidator(value -> { + // 对每一条数据进行校验 + if ("".equals(value.getField3())) { + // 如果field3的值为空串,则抛异常 + throw new ValidationException("field3的值不合法"); + } + }); + return processor; + } +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/reader/ItemReaderConfigure.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/reader/ItemReaderConfigure.java new file mode 100644 index 00000000..51d8dbbd --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/entity/reader/ItemReaderConfigure.java @@ -0,0 +1,40 @@ +package cc.mrbird.batch.entity.reader; + +import cc.mrbird.batch.entity.TestData; +import org.springframework.batch.item.support.ListItemReader; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author MrBird + */ +@Configuration +public class ItemReaderConfigure { + + @Bean + public ListItemReader simpleReader() { + List data = new ArrayList<>(); + TestData testData1 = new TestData(); + testData1.setId(1); + testData1.setField1("11"); + testData1.setField2("12"); + testData1.setField3("13"); + data.add(testData1); + TestData testData2 = new TestData(); + testData2.setId(2); + testData2.setField1("21"); + testData2.setField2("22"); + testData2.setField3("23"); + data.add(testData2); + TestData testData3 = new TestData(); + testData3.setId(3); + testData3.setField1("31"); + testData3.setField2("32"); + testData3.setField3(""); + data.add(testData3); + return new ListItemReader<>(data); + } +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/processor/TestDataFilterItemProcessor.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/processor/TestDataFilterItemProcessor.java new file mode 100644 index 00000000..2eea8fdf --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/processor/TestDataFilterItemProcessor.java @@ -0,0 +1,17 @@ +package cc.mrbird.batch.processor; + +import cc.mrbird.batch.entity.TestData; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class TestDataFilterItemProcessor implements ItemProcessor { + @Override + public TestData process(TestData item) { + // 返回null,会过滤掉这条数据 + return "".equals(item.getField3()) ? null : item; + } +} diff --git a/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/processor/TestDataTransformItemPorcessor.java b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/processor/TestDataTransformItemPorcessor.java new file mode 100644 index 00000000..cd20028f --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/java/cc/mrbird/batch/processor/TestDataTransformItemPorcessor.java @@ -0,0 +1,19 @@ +package cc.mrbird.batch.processor; + +import cc.mrbird.batch.entity.TestData; +import org.springframework.batch.item.ItemProcessor; +import org.springframework.beans.BeanUtils; +import org.springframework.stereotype.Component; + +/** + * @author MrBird + */ +@Component +public class TestDataTransformItemPorcessor implements ItemProcessor { + @Override + public TestData process(TestData item) { + // field1值拼接 hello + item.setField1(item.getField1() + " hello"); + return item; + } +} diff --git a/70.spring-batch-itemprocessor/src/main/resources/application.yml b/70.spring-batch-itemprocessor/src/main/resources/application.yml new file mode 100644 index 00000000..4c962f7b --- /dev/null +++ b/70.spring-batch-itemprocessor/src/main/resources/application.yml @@ -0,0 +1,6 @@ +spring: + datasource: + driver-class-name: com.mysql.cj.jdbc.Driver + url: jdbc:mysql://127.0.0.1:3306/springbatch + username: root + password: 123456 \ No newline at end of file