Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Spring-batch (ItemReader)-data reading from normal files, databases, XML, multi-file data reading

Shulou Source: shulou.com Published: 2022-06-03 06:09:54 09月24日 Update

Summary of Spring-Batch Learning (3)-- how to enter data

I. Overview of ItemReader

1.ItemReader: the interface that provides data

two。 There is only one method read () in this interface, which reads one data and moves to the next, and must return a null at the end of the reading, otherwise it indicates that the data has not finished reading.

Example:

OverViewApplication:

Package com.dhcc.batch.batchDemo.input.overview;import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication@EnableBatchProcessingpublic class OverViewApplication {public static void main (String [] args) {SpringApplication.run (OverViewApplication.class, args);}}

InputOverViewDemoJobConfiguration:

Package com.dhcc.batch.batchDemo.input.overview;import java.util.Arrays;import java.util.List;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.ItemWriter;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration @ Configurationpublic class InputOverViewDemoJobConfiguration {@ Autowired private JobBuilderFactory jobBuilderFactory; @ Autowired private StepBuilderFactory stepBuilderFactory; @ Bean public Job inputOverViewDemoJob () {return jobBuilderFactory.get ("inputOverViewDemoJob") .start (inputOverViewDemoJobStep ()). Build ();} public Step inputOverViewDemoJobStep () {return stepBuilderFactory.get ("inputOverViewDemoJobStep"). Chunk (2) .reader (inputOverViewDemoReader ()) .writer (outputOverViewDemoWriter ()). Build () } private ItemWriter 4.0.0 com.dhcc.batch batchDemo 0.0.1-SNAPSHOT jar batchDemo Demo project for Spring Boot org.springframework.boot spring-boot-starter-parent 2.0.4.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring -boot-starter-batch org.springframework.boot spring-boot-starter-test test org.springframework.batch spring-batch-test test org.springframework spring-oxm 5.0.8.RELEASE Com.thoughtworks.xstream xstream 1.4.10 org.springframework.boot spring-boot-starter-jdbc mysql mysql-connector-java runtime Org.springframework.boot spring-boot-maven-plugin

Tags: Data files databases results northeast wind interfaces projects runs two code examples sorting observation multiple entities source files status structure UTF-8 processing Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL Shulou Tech Info Redmi MariaDB