Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Spring batch getting started exampl

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

1 scenario description

Read the CVS file, after processing, save to the database.

2 Project structure

Application program

Start the main program

DemoApplication.java

Read file (input file)

UserItemReader.java

Processing data

UserItemProcess.java

Output file

UserItemWriter.java

Schedule batch jobs

Timing processing configuration

QuartzConfiguration.java

Timing scheduling

QuartzJobLauncher.java

Auxiliary file

Data file

User.txt

Object entity (transfer object)

User.java

Meaven profile

Pom.xml

2.1 Pom.xml

4.0.0

Com.zy

SpringBatchDemo1

0.0.1-SNAPSHOT

Jar

SpringBatchDemo1

Demo project for Spring Boot

Org.springframework.boot

Spring-boot-starter-parent

1.5.10.RELEASE

UTF-8

UTF-8

1.8

Org.springframework

Spring-context-support

Org.springframework.boot

Spring-boot-starter-batch

Org.springframework

Spring-oxm

Org.projectlombok

Lombok

Mysql

Mysql-connector-java

Runtime

Org.springframework.boot

Spring-boot-starter-test

Test

Org.springframework.batch

Spring-batch-test

Test

Org.projectlombok

Lombok

Org.quartz-scheduler

Quartz

2.3.0

Com.h3database

H3

Runtime

Org.springframework.boot

Spring-boot-maven-plugin

2.2 User.java

Package com.zy.model

Public class User {

Private String id

Private String name

Private String age

Public User (String id, String name, String age) {

This.id = id

This.name = name

This.age = age

}

Public String getId () {

Return id

}

Public void setId (String id) {

This.id = id

}

Public String getName () {

Return name

}

Public void setName (String name) {

This.name = name

}

Public String getAge () {

Return age

}

Public void setAge (String age) {

This.age = age

}

@ Override

Public String toString () {

Return "User [id=" + id + ", name=" + name + ", age=" + age + "]"

}

}

2.3 UserItemReader.java

Package com.zy.reader

Import org.springframework.batch.item.file.FlatFileItemReader

Import org.springframework.batch.item.file.LineMapper

Import org.springframework.batch.item.file.mapping.DefaultLineMapper

Import org.springframework.batch.item.file.mapping.FieldSetMapper

Import org.springframework.batch.item.file.transform.DelimitedLineTokenizer

Import org.springframework.batch.item.file.transform.FieldSet

Import org.springframework.batch.item.file.transform.LineTokenizer

Import org.springframework.core.io.ClassPathResource

Import org.springframework.validation.BindException

Import com.zy.model.User

/ / read information from user.txt file to User

Public class UserItemReader extends FlatFileItemReader {

Public UserItemReader () {

CreateReader ()

}

Private void createReader () {

This.setResource (new ClassPathResource ("data/User.txt"))

This.setLinesToSkip (1)

This.setLineMapper (userLineMapper ())

}

Private LineMapper userLineMapper () {

DefaultLineMapper lineMapper = new DefaultLineMapper ()

LineMapper.setLineTokenizer (userLineTokenizer ())

LineMapper.setFieldSetMapper (new UserFieldStepMapper ())

LineMapper.afterPropertiesSet ()

Return lineMapper

}

Private LineTokenizer userLineTokenizer () {

DelimitedLineTokenizer tokenizer = new DelimitedLineTokenizer ()

Tokenizer.setNames (new String [] {"ID", "NAME", "AGE"})

Return tokenizer

}

Private static class UserFieldStepMapper implements FieldSetMapper {

@ Override

Public User mapFieldSet (FieldSet fieldSet) throws BindException {

Return new User (fieldSet.readString ("ID")

FieldSet.readString ("NAME")

FieldSet.readString ("AGE"))

}

}

}

2.4 User.txt

ID,NAME,AGE

1,zy,28

2,tom,20

3,terry,30

4,lerry,18

5,bob,25

6,linda,27

7,marry,39

8,long,22

9,kin,33

10, Wang Wu, 40

2.5 UserItemProcessor.java

Package com.zy.processor

Import org.springframework.batch.item.ItemProcessor

Import com.zy.model.User

Public class UserItemProcessor implements ItemProcessor {

@ Override

Public User process (User item) throws Exception {

If (Integer.parseInt (item.getAge ()) > 20) {

Return item

}

Return null

}

}

2.6 UserItemWriter.java

Package com.zy.writer

Import java.util.List

Import org.springframework.batch.item.ItemWriter

Import com.zy.model.User

Public class UserItemWriter implements ItemWriter {

@ Override

Public void write (List

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report