This topic describes how to use the SpringBatch framework and OceanBase Cloud to build an application for basic database operations, such as table creation, data insertion, and data query.
Download the java-oceanbase-springbatch sample project Prerequisites
- You have registered an OceanBase Cloud account, and created a cluster instance and an Oracle-compatible tenant in OceanBase Cloud. For more information, see Create a cluster instance and Create a tenant.
- You have obtained the connection string of the Oracle-compatible tenant. For more information, see Obtain the connection string.
- You have installed Java Development Kit (JDK) 1.8 and Maven.
- You have installed IntelliJ IDEA.
Note
This topic uses IntelliJ IDEA Community Edition 2021.3.2 to run the sample code. You can also choose a suitable tool as needed.
Procedure
Note
The following procedure applies to Windows. If you use another operating system or compiler, the procedure can be slightly different.
Step 1: Import the java-oceanbase-springbatch project to IntelliJ IDEA
Start IntelliJ IDEA and choose File > Open....

In the Open File or Project window, select the project files and click OK to import the files.
IntelliJ IDEA automatically identifies various files in the project. You can view project information such as the directory structure, file list, module list, and dependencies in the Project window. Generally, the Project window is at the leftmost of the UI of IntelliJ IDEA and is opened by default. If the Project window is closed, you can choose View > Tool Windows > Project in the menu bar or press Alt + 1 to open it.
Note
When you use IntelliJ IDEA to import a project, IntelliJ IDEA automatically detects the `pom.xml` file in the project, downloads the required dependency libraries based on the dependencies described in the file, and adds them to the project.
View the project.

Step 2: Modify the database connection information in the java-oceanbase-springbatch project
Modify the database connection information in the application.properties file based on the obtained connection string mentioned in the "Prerequisites" section.
Here is an example:
spring.datasource.driver-class-name=com.oceanbase.jdbc.Driver
spring.datasource.url=jdbc:oceanbase://t5******.********.oceanbase.cloud:3306/sys?characterEncoding=utf-8
spring.datasource.username=test_user
spring.datasource.password=******
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.batch.job.enabled=false
logging.level.org.springframework=INFO
logging.level.com.example=DEBUG
- The name of the database driver is
com.oceanbase.jdbc.Driver. - The endpoint is
t5******.********.oceanbase.cloud. - The access port is
3306. - The name of the database to be accessed is
sys. - The tenant account is
test_user. - The password is
******.
Step 3: Run the java-oceanbase-springbatch project
Run the
AddDescPeopleWriterTest.javafile.- Find the
AddDescPeopleWriterTest.javafile under src > test > java in the project package. - Choose Run > Run... > AddDescPeopleWriterTest.testWrite in the menu bar or click the green triangle in the upper-right corner to run the file.
- View the logs and output of the project in the Console window of IDEA.
Data in the people_desc table: PeopleDESC [name=John, age=25, desc=This is John with age 25] PeopleDESC [name=Alice, age=30, desc=This is Alice with age 30] Batch Job execution completed.- Find the
Run the
AddPeopleWriterTest.javafile.- Find the
AddDescPeopleWriterTest.javafile under src > test > java in the project package. - Choose Run > Run... > AddPeopleWriterTest.testWrite in the menu bar or click the green triangle in the upper-right corner to run the file.
- View the logs and output of the project in the Console window of IDEA.
Data in the people table: People [name=zhangsan, age=27] People [name=lisi, age=35] Batch Job execution completed.- Find the
Project code
Click here to download the project code, which is a package named java-oceanbase-springbatch.
Decompress the package to obtain a folder named java-oceanbase-springbatch. The directory structure is as follows:
│ pom.xml
│
├─.idea
│
├─src
│ ├─main
│ │ ├─java
│ │ │ └─com
│ │ │ └─oceanbase
│ │ │ └─example
│ │ │ └─batch
│ │ │ │──BatchApplication.java
│ │ │ │
│ │ │ ├─config
│ │ │ │ └─BatchConfig.java
│ │ │ │
│ │ │ ├─model
│ │ │ │ ├─People.java
│ │ │ │ └─PeopleDESC.java
│ │ │ │
│ │ │ ├─processor
│ │ │ │ └─AddPeopleDescProcessor.java
│ │ │ │
│ │ │ └─writer
│ │ │ ├─AddDescPeopleWriter.java
│ │ │ └─AddPeopleWriter.java
│ │ │
│ │ └─resources
│ │ └─application.properties
│ │
│ └─test
│ └─java
│ └─com
│ └─oceanbase
│ └─example
│ └─batch
│ ├─config
│ │ └─BatchConfigTest.java
│ │
│ ├─processor
│ │ └─AddPeopleDescProcessorTest.java
│ │
│ └─writer
│ ├─AddDescPeopleWriterTest.java
│ └─AddPeopleWriterTest.java
│
└─target
The files and directories are described as follows:
pom.xml: the configuration file of the Maven project, which contains the dependencies, plug-ins, and build rules of the project..idea: a directory used in an Integrated Development Environment (IDE) to store configuration information related to the project.src: a directory that stores the source code in the project.main: a directory that stores the main source code and resource files.java: a directory that stores the Java source code.com: the root directory of the Java package.oceanbase: the root directory of the project.example: the root directory of the project.batch: the main package of the project.BatchApplication.java: the entry class to the application, which contains the main methods of the application.config: the configuration class folder that contains the configuration classes of the application.BatchConfig.java: the configuration class of the application, which is used to configure some properties and behavior of the application.model: the model class folder that contains the data model classes of the application.People.java: the personnel data model class.PeopleDESC.java: the personnel DESC data model class.processor: the processor class folder that contains the processor classes of the application.AddPeopleDescProcessor.java: the processor class that adds personnel DESC information.writer: the writer class folder that contains the writer classes of the application.AddDescPeopleWriter.java: the writer class that writes personnel DESC information.AddPeopleWriter.java: the writer class that writes personnel information.resources: the resource folder that contains the configuration file and other static resource files of the application.application.properties: the configuration file of the application, which is used to configure the properties of the application.test: a directory that stores the test code and resource files.BatchConfigTest.java: the test class for the configuration class of the application.AddPeopleDescProcessorTest.java: the test class for the processor class that adds personnel DESC information.AddDescPeopleWriterTest.java: the test class for the writer class that writes personnel DESC information.AddPeopleWriterTest.java: the test class for the writer class that writes personnel information.target: a directory that stores compiled class files and .jar packages.
Code in pom.xml
Note
You can retain the default code in this file for verification purposes or modify the code in the file as needed.
Perform the following steps to configure the pom.xml file:
Declare the file.
Declare the file to be an XML file that uses XML standard 1.0 and the character encoding UTF-8.
The sample code is as follows:
<?xml version="1.0" encoding="UTF-8"?>Configure namespaces and the POM model version.
xmlns: the default XML namespace for the POM, which is set tohttp://maven.apache.org/POM/4.0.0.xmlns:xsi: the XML namespace for XML elements prefixed withxsi, which is set tohttp://www.w3.org/2001/XMLSchema-instance.xsi:schemaLocation: the location of an XML schema definition (XSD) file. The value consists of two parts: the default XML namespace(http://maven.apache.org/POM/4.0.0)and the URI of the XSD file (http://maven.apache.org/xsd/maven-4.0.0.xsd).<modelVersion>: the POM model version used by the POM file, which is set to4.0.0.
The sample code is as follows:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> </project>Configure parent project information.
<groupId>: the ID of the parent project group, which is set toorg.springframework.boot.<artifactId>: the parent project, which is set tospring-boot-starter-parent.- the version of the parent project, which is set to
2.7.11. <relativePath>: an empty path for the parent project.
The sample code is as follows:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.7.11</version> <relativePath/> </parent>Configure basic information.
<groupId>: the ID of the project group, which is set tocom.oceanbase.<artifactId>: the name of the project, which is set tojava-oceanbase-springboot.<version>: the version of the project, which is set to0.0.1-SNAPSHOT.<description>: the project information, which is set toDemo project for Spring Batch.
The sample code is as follows:
<groupId>com.oceanbase</groupId> <artifactId>java-oceanbase-springboot</artifactId> <version>0.0.1-SNAPSHOT</version> <name>java-oceanbase-springbatch</name> <description>Demo project for Spring Batch</description>Configure the Java version.
Specify to use Java 1.8 for the project.
The sample code is as follows:
<properties> <java.version>1.8</java.version> </properties>Configure core dependencies.
Define a dependency named
spring-boot-starterthat belongs to theorg.springframework.bootgroup. This dependency contains default features provided by Spring Boot, such as web, data processing, security, and testing.The sample code is as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>Define a dependency named
spring-boot-starter-jdbcthat belongs to theorg.springframework.bootgroup. This dependency contains Java Database Connectivity (JDBC) features provided by Spring Boot, such as connection pool and data source configuration.The sample code is as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency>Define a dependency named
spring-boot-starter-testthat belongs to theorg.springframework.bootgroup. This dependency takes effect in thetestscope and provides test frameworks and tools of Spring Boot, such as JUnit, Mockito, and Hamcrest.The sample code is as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>Define a dependency named
oceanbase-clientthat belongs to thecom.oceanbasegroup and whose version is2.4.3. With this dependency, you can use the features of OBClient, such as connections, queries, and transactions.The sample code is as follows:
<dependency> <groupId>com.oceanbase</groupId> <artifactId>oceanbase-client</artifactId> <version>2.4.3</version> </dependency>Define a dependency named
spring-boot-starter-batchthat belongs to theorg.springframework.bootgroup. This dependency contains the batch processing feature provided by Spring Boot.The sample code is as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-batch</artifactId> </dependency>Define a dependency named
spring-boot-starter-data-jpathat belongs to theorg.springframework.bootgroup.as the ID of the group that the dependency belongs to, andspring-boot-starter-data-jpaas the dependency ID. This dependency contains necessary dependencies and configurations for JPA-based database accesses, and is a Spring Boot starter.The sample code is as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency>Define a dependency named
tomcat-jdbcthat belongs to theorg.apache.tomcatgroup. This dependency allows the application to use JDBC connection pool features provided by Tomcat, including connection pool configuration, connection acquisition and release, and connection management.The sample code is as follows:
<dependency> <groupId>org.apache.tomcat</groupId> <artifactId>tomcat-jdbc</artifactId> </dependency>Define a dependency named
junitthat belongs to thejunitgroup and whose version is4.10and effective scope istest. This dependency allows the application to use JUnit.The sample code is as follows:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.10</version> <scope>test</scope> </dependency>Define a dependency named
javax.activation-apithat belongs to thejavax.activationgroup and whose version is1.2.0. This dependency provides the Java Activation Framework (JAF) API.The sample code is as follows:
<dependency> <groupId>javax.activation</groupId> <artifactId>javax.activation-api</artifactId> <version>1.2.0</version> </dependency>Define a dependency named
jakarta.persistence-apithat belongs to thejakarta.persistencegroup and whose version is2.2.3. This dependency provides the Jakarta Persistence API. The sample code is as follows:<dependency> <groupId>jakarta.persistence</groupId> <artifactId>jakarta.persistence-api</artifactId> <version>2.2.3</version> </dependency>
Configure the Maven plug-in.
Define a plug-in named
spring-boot-maven-pluginthat belongs to theorg.springframework.bootgroup. This plug-in can be used to package Spring Boot applications as executable JAR packages or WAR packages, or directly run Spring Boot applications.The sample code is as follows:
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
Code in application.properties
The application.properties file contains database connection configurations, such as the database driver, URL, username, and password. It also contains configurations related to the Java Persistence API (JPA), Spring Batch, and log level.
Configure the database connection.
spring.datasource.driver: the database driver used to establish a connection with OceanBase Cloud, which is set tocom.oceanbase.jdbc.Driver.spring.datasource.url: the URL for connecting to the database.spring.datasource.username: the username for connecting to the database.spring.datasource.password: the password for connecting to the database.
The sample code is as follows:
spring.datasource.driver-class-name=com.oceanbase.jdbc.Driver spring.datasource.url=jdbc:oceanbase://host:port/schema_name?characterEncoding=utf-8 spring.datasource.username=user_name spring.datasource.password=******Configure the JPA.
spring.jpa.show-sql: specifies whether to display SQL statements in logs. The valuetruehere indicates that SQL statements are displayed in logs.spring.jpa.hibernate.ddl-autothe DDL operation performed by Hibernate. The valueupdatehere indicates that Hibernate automatically updates the database schema when the application starts.
The sample code is as follows:
spring.jpa.show-sql=true spring.jpa.hibernate.ddl-auto=updateConfigure Spring Batch.
spring.batch.job.enable: specifies whether to enable Spring Batch jobs. The valuefalsehere indicates that Spring Batch jobs are disabled.The sample code is as follows:
spring.batch.job.enabled=falseConfigure the log level.
logging.level.org.springframework: the log level of the Spring framework, which is set toINFO`.logging.level.com.example: the log level for the custom code of the application, which is set toDEBUG.
The sample code is as follows:
logging.level.org.springframework=INFO logging.level.com.example=DEBUG
Code in BatchApplication.java
The BatchApplication.java file is the entry file to the Spring Boot application.
Perform the following steps to configure the BatchApplication.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
SpringApplicationclass: launches the Spring Boot application.SpringBootApplicationannotation: marks the class as the entry to the Spring Boot application.
The sample code is as follows:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;Define the
BatchApplicationclass.Use the
@SpringBootApplicationannotation to mark theBatchApplicationclass as the entry to the Spring Boot application. Use theBatchApplicationclass to define a staticmainmethod as the entry to the application. In themainmethod, use theSpringApplication.runmethod to launch the Spring Boot application. Define a method namedrunBatchJobto run batch jobs.The sample code is as follows:
@SpringBootApplication public class BatchApplication { public static void main(String[] args) { SpringApplication.run(BatchApplication.class, args); } public void runBatchJob() { } }
Code in BatchConfig.java
The BatchConfig.java file configures components such as the steps, reader, processor, and writer for batch jobs.
Perform the following steps to configure the BatchConfig.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
Peopleclass: stores personnel information read from the database.PeopleDESCclass: stores the description converted or processed from personnel information.AddPeopleDescProcessorclass: converts aPeopleobject to aPeopleDESCobject. This class implements theItemProcessorinterface.AddDescPeopleWriterclass: writes aPeopleDESCobject to a specified destination. This class implements theItemWriterinterface.Jobinterface: indicates a batch job.Stepinterface: indicates a step in a job.EnableBatchProcessingannotation: enables and configures Spring Batch features.JobBuilderFactoryclass: creates and configures jobs.StepBuilderFactoryclass: creates and configures steps.RunIdIncrementerclass: the run ID incrementer of Spring Batch, which is used to increment the run ID each time a job is run.ItemProcessorinterface: processes or converts the read items.ItemReaderinterface: reads items from the data source.ItemWriterinterface: writes processed or converted items to a specified destination.JdbcCursorItemReaderclass: reads data from the database and returns a cursor result set.Autowiredannotation: injects dependencies.Beanannotation: creates and configures beans.ComponentScanannotation: specifies the package or class to scan for components.Configurationannotation: marks a class as a configuration class.EnableAutoConfigurationannotation: enables automatic configuration of Spring Boot.SpringBootApplicationannotation: marks the class as the entry to the Spring Boot application.DataSourceinterface: obtains database connections.
The sample code is as follows:
import com.oceanbase.example.batch.model.People; import com.oceanbase.example.batch.model.PeopleDESC; import com.oceanbase.example.batch.processor.AddPeopleDescProcessor; import com.oceanbase.example.batch.writer.AddDescPeopleWriter; import org.springframework.batch.core.Job; import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing; 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.ItemProcessor; import org.springframework.batch.item.ItemReader; import org.springframework.batch.item.ItemWriter; import org.springframework.batch.item.database.JdbcCursorItemReader; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.jdbc.core.BeanPropertyRowMapper; import javax.sql.DataSource;Define the
BatchConfigclass.It is a simple batch job of Spring Batch. In the class, define how data is read, processed, and written, and encapsulate these steps as a simple Spring Batch job. Use the annotations and automatic configuration feature of Spring Batch to create corresponding component instances by calling various
@Beanmethods, and use these components to read, process, and write data instep1.- Use
@Configurationto mark this class as a configuration class. - Use
@EnableBatchProcessingto enable Spring Batch. The annotation automatically creates necessary beans, such asJobRepositoryandJobLauncher. - Use
@SpringBootApplicationto mark the main class of the Spring Boot application. The Spring Boot application is launched from the main class. - Use
@ComponentScanto specify the package to scan for components. Spring Boot scans and registers all components in the package and its sub-packages. - Use
@EnableAutoConfigurationto automatically configure the infrastructure of the Spring Boot application.
The sample code is as follows:
@Configuration @EnableBatchProcessing @SpringBootApplication @ComponentScan("com.oceanbase.example.batch.writer") @EnableAutoConfiguration public class BatchConfig { }Define the
@Autowiredannotation.Use the
@Autowiredannotation to injectJobBuilderFactory,StepBuilderFactory, andDataSourceas member variables in theBatchConfigclass.JobBuilderFactoryis the factory class used to create and configure jobs.StepBuilderFactoryis the factory class used to create and configure steps.DataSourceis the interface used to obtain database connections.The sample code is as follows:
@Autowired private JobBuilderFactory jobBuilderFactory; @Autowired private StepBuilderFactory stepBuilderFactory; @Autowired private DataSource dataSource;Define the
@Beanannotation.Use the
@Beanannotation to define methods for creating readers, processors, writers, steps, and jobs.Call the
peopleReadermethod to create an instance of theItemReadercomponent. The component usesJdbcCursorItemReaderto readPeopleobjects from the database. Set the data source indataSource, setRowMapperto map database rows toPeopleobjects, and set the SQL query statement toSELECT * FROM people.Call the
addPeopleDescProcessormethod to create an instance of theItemProcessorcomponent. The component usesAddPeopleDescProcessorto processPeopleobjects and returnPeopleDESCobjects.Call the
addDescPeopleWritermethod to create an instance of theItemWritercomponent. The component usesAddDescPeopleWriterto writePeopleDESCobjects to the destination.Call the
step1method to create an instance of theStepcomponent. Name the instance asstep1. CallstepBuilderFactory.getto get the step builder. Set the reader to theItemReadercomponent, the processor to theItemProcessorcomponent, the writer to theItemWritercomponent, and the chunk size to 10. Callbuildto build and return the configuredStepinstance.Call the
importJobmethod to create an instance of theJobcomponent. Name the job asimportJob. CalljobBuilderFactory.getto get the job builder. Set the incrementer toRunIdIncrementerand the initial step inflowtoStep. Callbuildto build and return the configuredJobinstance.The sample code is as follows:
@Bean public ItemReader<People> peopleReader() { JdbcCursorItemReader<People> reader = new JdbcCursorItemReader<>(); reader.setDataSource((javax.sql.DataSource) dataSource); reader.setRowMapper(new BeanPropertyRowMapper<>(People.class)); reader.setSql("SELECT * FROM people"); return reader; } @Bean public ItemProcessor<People, PeopleDESC> addPeopleDescProcessor() { return new AddPeopleDescProcessor(); } @Bean public ItemWriter<PeopleDESC> addDescPeopleWriter() { return new AddDescPeopleWriter(); } @Bean public Step step1(ItemReader<People> reader, ItemProcessor<People, PeopleDESC> processor, ItemWriter<PeopleDESC> writer) { return stepBuilderFactory.get("step1") . <People, PeopleDESC>chunk(10) .reader(reader) .processor(processor) .writer(writer) .build(); } @Bean public Job importJob(Step step1) { return jobBuilderFactory.get("importJob") .incrementer(new RunIdIncrementer()) .flow(step1) .end() .build(); }
- Use
Code in People.java
The People.java file defines a data model class named People to represent personnel information. The class contains two private member variables: name and age, and corresponding getter and setter methods. The toString method is overridden to print the object information. name indicates the name of a person, and age indicates the age of a person. The getter and setter methods respectively get and set the values of these attributes.
The class provides data storage and transfer means for the input and output of a batch program. In batch reads and writes, People objects store data, setter methods set data, and getter methods get data.
The sample code is as follows:
public class People {
private String name;
private int age;
// Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + "]";
}
// Getters and setters
}
Code in PeopleDESC.java
The PeopleDESC.java file defines a data model class named PeopleDESC to represent the description of personnel information. The PeopleDESC class contains four attributes: name, age, desc, and id, which respectively represent the name, age, description, and identifier of a person. The class also contains corresponding getter and setter methods for getting and setting the attribute values. The toString method is overridden to return a string representation of the class, including the name, age, and description.
Similar to the People class, the PeopleDESC class provides data storage and transfer means for the input and output of a batch program.
The sample code is as follows:
public class PeopleDESC {
private String name;
private int age;
private String desc;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "PeopleDESC [name=" + name + ", age=" + age + ", desc=" + desc + "]";
}
}
Code in AddPeopleDescProcessor.java
The AddPeopleDescProcessor.java file defines a class named AddPeopleDescProcessor that implements the ItemProcessor interface for converting People objects to PeopleDESC objects.
Perform the following steps to configure the AddPeopleDescProcessor.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
Peopleclass: stores personnel information read from the database.PeopleDESCclass: stores the description converted or processed from personnel information.ItemProcessorinterface: processes or converts the read items.
The sample code is as follows:
import com.oceanbase.example.batch.model.People; import com.oceanbase.example.batch.model.PeopleDESC; import org.springframework.batch.item.ItemProcessor;Define the
AddPeopleDescProcessorclass.The
AddPeopleDescProcessorclass of theItemProcessorinterface convertsPeopleobjects toPeopleDESCobjects, thus implementing the processing logic for the input data during batch processing.In the
processmethod of this class, create aPeopleDESCobject nameddesc, and then use theitemparameter to obtain the attributes (nameandage) of thePeopleobject and populate these attributes to thedescobject. At the same time, assign a value to thedescattribute of thedescobject. The value assignment logic is to generate the description of thePeopleobject based on its attributes. Finally, return the processedPeopleDESCobject.The sample code is as follows:
public class AddPeopleDescProcessor implements ItemProcessor<People, PeopleDESC> { @Override public PeopleDESC process(People item) throws Exception { PeopleDESC desc = new PeopleDESC(); desc.setName(item.getName()); desc.setAge(item.getAge()); desc.setDesc("This is " + item.getName() + " with age " + item.getAge()); return desc; } }
Code in AddDescPeopleWriter.java
The AddDescPeopleWriter.java file implements the AddDescPeopleWriter class of the ItemWriter interface to write PeopleDESC objects to the database.
Perform the following steps to configure the AddDescPeopleWriter.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
PeopleDESCclass: stores the description converted or processed from personnel information.ItemWriterinterface: writes processed or converted items to a specified destination.Autowiredannotation: injects dependencies.JdbcTemplateclass: provides methods for executing SQL statements.Listinterface: operates the query result set.
The sample code is as follows:
import com.oceanbase.example.batch.model.PeopleDESC; import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import java.util.List;Define the
AddDescPeopleWriterclass.Use the
@Autowiredannotation to automatically inject theJdbcTemplateinstance, which will be used to perform database operations for data writes.The sample code is as follows:
@Autowired private JdbcTemplate jdbcTemplate;In the
writemethod, traverseList<? extends PeopleDESC>that is passed in to fetch allPeopleDESCobjects in sequence. Execute the SQL statementDROP TABLE people_descto drop the table namedpeople_descthat may already exist. Execute the SQL statementCREATE TABLE people_desc (id INT PRIMARY KEY, name VARCHAR2(255), age INT, description VARCHAR2(255))to create a table namedpeople_descwith four columns:id,name,age, anddescription. Execute the SQL statementINSERT INTO people_desc (id, name, age, description) VALUES (?, ?, ?, ?)to insert the attribute values of eachPeopleDESCobject into thepeople_desctable.The sample code is as follows:
@Override public void write(List<? extends PeopleDESC> items) throws Exception { // Drop the table that may already exist. jdbcTemplate.execute("DROP TABLE people_desc"); // Create the table. String createTableSql = "CREATE TABLE people_desc (id INT PRIMARY KEY, name VARCHAR2(255), age INT, description VARCHAR2(255))"; jdbcTemplate.execute(createTableSql); for (PeopleDESC item : items) { String sql = "INSERT INTO people_desc (id, name, age, description) VALUES (?, ?, ?, ?) "; jdbcTemplate.update(sql, item.getId(), item.getName(), item.getAge(), item.getDesc()); } }
Code in AddPeopleWriter.java
The AddPeopleWriter.java file implements the AddPeopleWriter class of the ItemWriter interface to write People objects to the database.
Perform the following steps to configure the AddPeopleWriter.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
Peopleclass: stores personnel information read from the database.ItemWriterinterface: writes processed or converted items to a specified destination.Autowiredannotation: injects dependencies.JdbcTemplateclass: provides methods for executing SQL statements.Componentannotation: marks the class as a Spring component.Listinterface: operates the query result set.
The sample code is as follows:
import com.oceanbase.example.batch.model.People; import org.springframework.batch.item.ItemWriter; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Component; import java.util.List;Define the
AddPeopleWriterclass.Use the
@Autowiredannotation to automatically inject theJdbcTemplateinstance, which will be used to perform database operations for data writes.The sample code is as follows:
@Autowired private JdbcTemplate jdbcTemplate;In the
writemethod, traverseList<? extends People>that is passed in to fetch allPeopleobjects in sequence. Execute the SQL statementDROP TABLE peopleto drop the table namedpeoplethat may already exist. Execute the SQL statementCREATE TABLE people (name VARCHAR2(255), age INT)to create a table namedpeoplewith two columns:nameandage. Execute the SQL statementINSERT INTO people (name, age) VALUES (?, ?)to insert the attribute values of eachPeopleobject into thepeopletable.The sample code is as follows:
@Override public void write(List<? extends People> items) throws Exception { // Drop the table that may already exist. jdbcTemplate.execute("DROP TABLE people"); // Create the table. String createTableSql = "CREATE TABLE people (name VARCHAR2(255), age INT)"; jdbcTemplate.execute(createTableSql); for (People item : items) { String sql = "INSERT INTO people (name, age) VALUES (?, ?) "; jdbcTemplate.update(sql, item.getName(), item.getAge()); } }
Code in BatchConfigTest.java
The BatchConfigTest.java file defines a class that uses JUnit for testing and is used to test the job configuration of Spring Batch.
Perform the following steps to configure the BatchConfigTest.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
Assertclass: asserts test results.Testannotation: marks a test method.RunWithannotation: specifies the test runner.Jobinterface: indicates a batch job.JobExecutionclass: indicates the execution of a batch job.JobParametersclass: indicates the parameters of a batch job.JobParametersBuilderclass: builds parameters of a batch job.JobLauncherinterface: launches a batch job.Autowiredannotation: injects dependencies.SpringBootTestannotation: marks the test class as a Spring Boot test.SpringRunnerclass: specifies SpringRunner as the test runner.
The sample code is as follows:
import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.batch.core.Job; import org.springframework.batch.core.JobExecution; import org.springframework.batch.core.JobParameters; import org.springframework.batch.core.JobParametersBuilder; import org.springframework.batch.core.launch.JobLauncher; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import javax.batch.runtime.BatchStatus;Define the
BatchConfigTestclass.With the
SpringBootTestannotation and theSpringRunnerrunner, this class can perform Spring Boot integration tests. In thetestJobmethod, use theJobLauncherTestUtilshelper class to launch a batch job and use an assertion to verify the execution status of the job.Use the
@Autowiredannotation to automatically inject theJobLauncherTestUtilsinstance.The sample code is as follows:
@Autowired private JobLauncherTestUtils jobLauncherTestUtils;Use the
@Testannotation to mark thetestJobmethod as a test method. In this method, create aJobParametersobject, call thejobLauncherTestUtils.launchJobmethod to launch the batch job, and then call theAssert.assertEqualsmethod to assert the execution status of the job asCOMPLETED.The sample code is as follows:
@Test public void testJob() throws Exception { JobParameters jobParameters = new JobParametersBuilder() .addString("jobParam", "paramValue") .toJobParameters(); JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters); Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus()); }Use the
@Autowiredannotation to automatically inject theJobLauncherinstance.The sample code is as follows:
@Autowired private JobLauncher jobLauncher;Use the
@Autowiredannotation to automatically inject theJobinstance.The sample code is as follows:
@Autowired private Job job;Define a private class named
JobLauncherTestUtilsto assist in launching a batch job. In the class, define thelaunchJobmethod for launching a batch job. In this method, call thejobLauncher.runmethod to launch a job and return the execution result of the job.The sample code is as follows:
private class JobLauncherTestUtils { public JobExecution launchJob(JobParameters jobParameters) throws Exception { return jobLauncher.run(job, jobParameters); } }
Code in AddPeopleDescProcessorTest.java
The AddPeopleDescProcessorTest.java file defines a class that uses JUnit for testing and is used to test the job configuration of Spring Batch.
Perform the following steps to configure the AddPeopleDescProcessorTest.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
Peopleclass: stores personnel information read from the database.PeopleDESCclass: stores the description converted or processed from personnel information.Testannotation: marks a test method.RunWithannotation: specifies the test runner.Autowiredannotation: injects dependencies.SpringBootTestannotation: marks the test class as a Spring Boot test.SpringRunnerclass: specifies SpringRunner as the test runner.
The sample code is as follows:
import com.oceanbase.example.batch.model.People; import com.oceanbase.example.batch.model.PeopleDESC; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner;Define the
AddPeopleDescProcessorTestclass.With the
SpringBootTestannotation and theSpringRunnerrunner, this class can perform Spring Boot integration tests.Use the
@Autowiredannotation to automatically inject theAddPeopleDescProcessorinstance.The sample code is as follows:
@Autowired private AddPeopleDescProcessor processor;Use the
@Testannotation to mark thetestProcessmethod as a test method. In this method, create aPeopleobject, call theprocessor.processmethod to process the object, and then assign the result to aPeopleDESCobject.The sample code is as follows:
@Test public void testProcess() throws Exception { People people = new People(); PeopleDESC desc = processor.process(people); }
Code in AddDescPeopleWriterTest.java
The AddDescPeopleWriterTest.java file is a class that uses JUnit for testing and is used to test the write logic of AddDescPeopleWriter.
Perform the following steps to configure the AddDescPeopleWriterTest.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
PeopleDESCclass: stores the description converted or processed from personnel information.Assertclass: asserts test results.Testannotation: marks a test method.RunWithannotation: specifies the test runner.Autowiredannotation: injects dependencies.SpringBootTestannotation: marks the test class as a Spring Boot test.JdbcTemplateclass: provides methods for executing SQL statements.SpringRunnerclass: specifies SpringRunner as the test runner.ArrayListclass: creates an empty list.Listinterface: operates the query result set.
The sample code is as follows:
import com.oceanbase.example.batch.model.PeopleDESC; import org.junit.Assert; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List;Define the
AddDescPeopleWriterTestclass.With the
SpringBootTestannotation and theSpringRunnerrunner, this class can perform Spring Boot integration tests.Use
@Autowiredto inject instances. Use the@Autowiredannotation to automatically inject theAddDescPeopleWriterandJdbcTemplateinstances.The sample code is as follows:
@Autowired private AddDescPeopleWriter writer; @Autowired private JdbcTemplate jdbcTemplate;Use the method marked with
@Testto test data insertion and output. Use the@Testannotation to mark thetestWritemethod as a test method. In this method, create an emptypeopleDescListlist and then add twoPeopleDESCobjects to the list. Call thewriter.writemethod to write data in the list to the database. UsejdbcTemplateto execute the query statement that obtains data from thepeople_desctable. Execute the assertion statement to verify the correctness of the data. Output the query results to the console and output a message to indicate that job execution is complete.Insert data into the
people_desctable. Create an emptyPeopleDESCobject list namedpeopleDescList. Create twoPeopleDESCobjects nameddesc1anddesc2and set their attribute values. Adddesc1anddesc2to thepeopleDescListlist. Call thewritemethod ofwriterto write the objects in thepeopleDescListlist to thepeople_desctable in the database. CallJdbcTemplateto execute the query statementSELECT COUNT(*) FROM people_descthat obtains the number of records in thepeople_desctable. Assign the result to thecountvariable. Call theAssert.assertEqualsmethod to assert whether the value ofcountis2.The sample code is as follows:
List<PeopleDESC> peopleDescList = new ArrayList<>(); PeopleDESC desc1 = new PeopleDESC(); desc1.setId(1); desc1.setName("John"); desc1.setAge(25); desc1.setDesc("This is John with age 25"); peopleDescList.add(desc1); PeopleDESC desc2 = new PeopleDESC(); desc2.setId(2); desc2.setName("Alice"); desc2.setAge(30); desc2.setDesc("This is Alice with age 30"); peopleDescList.add(desc2); writer.write(peopleDescList); String selectSql = "SELECT COUNT(*) FROM people_desc"; int count = jdbcTemplate.queryForObject(selectSql, Integer.class); Assert.assertEquals(2, count);Output data in the
people_desctable. UseJdbcTemplateto execute the query statementSELECT * FROM people_desc, and use thelambdaexpression to process the query results. In thelambdaexpression, use methods such asrs.getIntandrs.getStringto obtain field values in the query result set and populate the field values to the newly createdPeopleDESCobjects. Add all the newly createdPeopleDESCobjects to the result listresultDesc. Print the prompt lineData in the people_desc table:. Then, use aFORloop to traverse theresultDesclist and useSystem.out.printlnto print thePeopleDESCobjects in the list one by one. Finally, print a message to indicate that job execution is complete.The sample code is as follows:
List<PeopleDESC> resultDesc = jdbcTemplate.query("SELECT * FROM people_desc", (rs, rowNum) -> { PeopleDESC desc = new PeopleDESC(); desc.setId(rs.getInt("id")); desc.setName(rs.getString("name")); desc.setAge(rs.getInt("age")); desc.setDesc(rs.getString("description")); return desc; }); System.out.println("Data in the people_desc table:"); for (PeopleDESC desc : resultDesc) { System.out.println(desc); } // Output a message to indicate that job execution is complete. System.out.println("Batch Job execution completed.");
Code in AddPeopleWriterTest.java
The AddPeopleWriterTest.java file is a class that uses JUnit for testing and is used to test the write logic of AddPeopleWriterTest.
Perform the following steps to configure the AddPeopleWriterTest.java file:
Reference other classes and APIs.
Declare this file to contain the following APIs and classes:
Peopleclass: stores personnel information read from the database.Testannotation: marks a test method.RunWithannotation: specifies the test runner.Autowiredannotation: injects dependencies.SpringBootApplicationannotation: marks the class as the entry to the Spring Boot application.SpringBootTestannotation: marks the test class as a Spring Boot test.ComponentScanannotation: specifies the package or class to scan for components.JdbcTemplateclass: provides methods for executing SQL statements.SpringRunnerclass: specifiesSpringRunneras the test runner.ArrayListclass: creates an empty list.Listinterface: operates the query result set.
The sample code is as follows:
import com.oceanbase.example.batch.model.People; import org.junit.jupiter.api.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.context.annotation.ComponentScan; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List;Define the
AddPeopleWriterTestclass.Use the
SpringBootTestannotation and theSpringRunnerrunner for Spring Boot integration testing, and use the@ComponentScanannotation to specify the package to scan.Use
@Autowiredto inject instances. Use the@Autowiredannotation to automatically inject theaddPeopleWriterandJdbcTemplateinstances.The sample code is as follows:
@Autowired private AddPeopleWriter addPeopleWriter; @Autowired private JdbcTemplate jdbcTemplate;Use the method marked with
@Testto test data insertion and output.Insert data into the
peopletable. First, create an emptyPeopleobject list namedpeopleList. Then, create twoPeopleobjects:person1andperson2, and set their name and age attributes. Add the twoPeopleobjects to thepeopleListlist. Call thewritemethod ofaddPeopleWriterand passpeopleListto the method as an argument, so as to write thePeopleobjects to the database.The sample code is as follows:
List<People> peopleList = new ArrayList<>(); People person1 = new People(); person1.setName("zhangsan"); person1.setAge(27); peopleList.add(person1); People person2 = new People(); person2.setName("lisi"); person2.setAge(35); peopleList.add(person2); addPeopleWriter.write(peopleList);Output data in the
peopletable. UseJdbcTemplateto execute the query statementSELECT * FROM people, and use thelambdaexpression to process the query results. In thelambdaexpression, use thers.getStringandrs.getIntmethods to obtain field values in the query result set and populate the field values to a newPeopleobject. Add all the newly createdPeopleobjects to the result listresult. Print the prompt lineData in the people table:. Then, use aFORloop to traverse the result list and useSystem.out.printlnto print thePeopleobjects in the list one by one. Finally, print a message to indicate that job execution is complete.The sample code is as follows:
List<People> result = jdbcTemplate.query("SELECT * FROM people", (rs, rowNum) -> { People person = new People(); person.setName(rs.getString("name")); person.setAge(rs.getInt("age")); return person; }); System.out.println("Data in the people table:"); for (People person : result) { System.out.println(person); } // Output a message to indicate that job execution is complete. System.out.println("Batch Job execution completed.");
Complete code
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.oceanbase</groupId>
<artifactId>java-oceanbase-springboot</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>java-oceanbase-springbatch</name>
<description>Demo project for Spring Batch</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>oceanbase-client</artifactId>
<version>2.4.3</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-batch</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jdbc</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.10</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>javax.activation-api</artifactId>
<version>1.2.0</version>
</dependency>
<dependency>
<groupId>jakarta.persistence</groupId>
<artifactId>jakarta.persistence-api</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
#configuration database
spring.datasource.driver-class-name=com.oceanbase.jdbc.Driver
spring.datasource.url=jdbc:oceanbase://host:port/schema_name?characterEncoding=utf-8
spring.datasource.username=user_name
spring.datasource.password=
# JPA
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
# Spring Batch
spring.batch.job.enabled=false
#
logging.level.org.springframework=INFO
logging.level.com.example=DEBUG
package com.oceanbase.example.batch;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class BatchApplication {
public static void main(String[] args) {
SpringApplication.run(BatchApplication.class, args);
}
public void runBatchJob() {
}
}
package com.oceanbase.example.batch.config;
import com.oceanbase.example.batch.model.People;
import com.oceanbase.example.batch.model.PeopleDESC;
import com.oceanbase.example.batch.processor.AddPeopleDescProcessor;
import com.oceanbase.example.batch.writer.AddDescPeopleWriter;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
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.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcCursorItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import javax.sql.DataSource;
//import javax.activation.DataSource;
@Configuration
@EnableBatchProcessing
@SpringBootApplication
@ComponentScan("com.oceanbase.example.batch.writer")
@EnableAutoConfiguration
public class BatchConfig {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Autowired
private DataSource dataSource;// Use the default dataSource provided by automatic Spring Boot configuration
@Bean
public ItemReader<People> peopleReader() {
JdbcCursorItemReader<People> reader = new JdbcCursorItemReader<>();
reader.setDataSource((javax.sql.DataSource) dataSource);
reader.setRowMapper(new BeanPropertyRowMapper<>(People.class));
reader.setSql("SELECT * FROM people");
return reader;
}
@Bean
public ItemProcessor<People, PeopleDESC> addPeopleDescProcessor() {
return new AddPeopleDescProcessor();
}
@Bean
public ItemWriter<PeopleDESC> addDescPeopleWriter() {
return new AddDescPeopleWriter();
}
@Bean
public Step step1(ItemReader<People> reader, ItemProcessor<People, PeopleDESC> processor,
ItemWriter<PeopleDESC> writer) {
return stepBuilderFactory.get("step1")
. <People, PeopleDESC>chunk(10)
.reader(reader)
.processor(processor)
.writer(writer)
.build();
}
@Bean
public Job importJob(Step step1) {
return jobBuilderFactory.get("importJob")
.incrementer(new RunIdIncrementer())
.flow(step1)
.end()
.build();
}
}
package com.oceanbase.example.batch.model;
public class People {
private String name;
private int age;
// Getters and setters
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "People [name=" + name + ", age=" + age + "]";
}
// Getters and setters
}
package com.oceanbase.example.batch.model;
public class PeopleDESC {
private String name;
private int age;
private String desc;
private int id;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public String toString() {
return "PeopleDESC [name=" + name + ", age=" + age + ", desc=" + desc + "]";
}
}
package com.oceanbase.example.batch.processor;
import com.oceanbase.example.batch.model.People;
import com.oceanbase.example.batch.model.PeopleDESC;
import org.springframework.batch.item.ItemProcessor;
public class AddPeopleDescProcessor implements ItemProcessor<People, PeopleDESC> {
@Override
public PeopleDESC process(People item) throws Exception {
PeopleDESC desc = new PeopleDESC();
desc.setName(item.getName());
desc.setAge(item.getAge());
desc.setDesc("This is " + item.getName() + " with age " + item.getAge());
return desc;
}
}
package com.oceanbase.example.batch.writer;
import com.oceanbase.example.batch.model.PeopleDESC;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import java.util.List;
public class AddDescPeopleWriter implements ItemWriter<PeopleDESC> {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void write(List<? extends PeopleDESC> items) throws Exception {
// Drop the table that may already exist.
jdbcTemplate.execute("DROP TABLE people_desc");
// Create the table.
String createTableSql = "CREATE TABLE people_desc (id INT PRIMARY KEY, name VARCHAR2(255), age INT, description VARCHAR2(255))";
jdbcTemplate.execute(createTableSql);
for (PeopleDESC item : items) {
String sql = "INSERT INTO people_desc (id, name, age, description) VALUES (?, ?, ?, ?) ";
jdbcTemplate.update(sql, item.getId(), item.getName(), item.getAge(), item.getDesc());
}
}
}
package com.oceanbase.example.batch.writer;
import com.oceanbase.example.batch.model.People;
import org.springframework.batch.item.ItemWriter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import java.util.List;
@Component
public class AddPeopleWriter implements ItemWriter<People> {
@Autowired
private JdbcTemplate jdbcTemplate;
@Override
public void write(List<? extends People> items) throws Exception {
// Drop the table that may already exist.
jdbcTemplate.execute("DROP TABLE people");
// Create the table.
String createTableSql = "CREATE TABLE people (name VARCHAR2(255), age INT)";
jdbcTemplate.execute(createTableSql);
for (People item : items) {
String sql = "INSERT INTO people (name, age) VALUES (?, ?) ";
jdbcTemplate.update(sql, item.getName(), item.getAge());
}
}
}
package com.oceanbase.example.batch.config;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.batch.runtime.BatchStatus;
@RunWith(SpringRunner.class)
@SpringBootTest
public class BatchConfigTest {
@Autowired
private JobLauncherTestUtils jobLauncherTestUtils;
@Test
public void testJob() throws Exception {
JobParameters jobParameters = new JobParametersBuilder()
.addString("jobParam", "paramValue")
.toJobParameters();
JobExecution jobExecution = jobLauncherTestUtils.launchJob(jobParameters);
Assert.assertEquals(BatchStatus.COMPLETED, jobExecution.getStatus());
}
@Autowired
private JobLauncher jobLauncher;
@Autowired
private Job job;
private class JobLauncherTestUtils {
public JobExecution launchJob(JobParameters jobParameters) throws Exception {
return jobLauncher.run(job, jobParameters);
}
}
}
package com.oceanbase.example.batch.processor;
import com.oceanbase.example.batch.model.People;
import com.oceanbase.example.batch.model.PeopleDESC;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AddPeopleDescProcessorTest {
@Autowired
private AddPeopleDescProcessor processor;
@Test
public void testProcess() throws Exception {
People people = new People();
// people.setName("John");
// people.setAge(25);
PeopleDESC desc = processor.process(people);
// Assert.assertEquals("John", desc.getName());
// Assert.assertEquals(25, desc.getAge());
// Assert.assertEquals("This is John with age 25", desc.getDesc());
}
}
package com.oceanbase.example.batch.writer;
import com.oceanbase.example.batch.model.PeopleDESC;
import org.junit.Assert;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
public class AddDescPeopleWriterTest {
@Autowired
private AddDescPeopleWriter writer;
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testWrite() throws Exception {
// Insert data into the people_desc table.
List<PeopleDESC> peopleDescList = new ArrayList<>();
PeopleDESC desc1 = new PeopleDESC();
desc1.setId(1);
desc1.setName("John");
desc1.setAge(25);
desc1.setDesc("This is John with age 25");
peopleDescList.add(desc1);
PeopleDESC desc2 = new PeopleDESC();
desc2.setId(2);
desc2.setName("Alice");
desc2.setAge(30);
desc2.setDesc("This is Alice with age 30");
peopleDescList.add(desc2);
writer.write(peopleDescList);
String selectSql = "SELECT COUNT(*) FROM people_desc";
int count = jdbcTemplate.queryForObject(selectSql, Integer.class);
Assert.assertEquals(2, count);
// Output data in the people_desc table.
List<PeopleDESC> resultDesc = jdbcTemplate.query("SELECT * FROM people_desc", (rs, rowNum) -> {
PeopleDESC desc = new PeopleDESC();
desc.setId(rs.getInt("id"));
desc.setName(rs.getString("name"));
desc.setAge(rs.getInt("age"));
desc.setDesc(rs.getString("description"));
return desc;
});
System.out.println("Data in the people_desc table:");
for (PeopleDESC desc : resultDesc) {
System.out.println(desc);
}
// Output a message to indicate that job execution is complete.
System.out.println("Batch Job execution completed.");
}
}
package com.oceanbase.example.batch.writer;
import com.oceanbase.example.batch.model.People;
import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringRunner.class)
@SpringBootTest
@SpringBootApplication
@ComponentScan("com.oceanbase.example.batch.writer")
public class AddPeopleWriterTest {
@Autowired
private AddPeopleWriter addPeopleWriter;
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
public void testWrite() throws Exception {
// Insert data into the people table.
List<People> peopleList = new ArrayList<>();
People person1 = new People();
person1.setName("zhangsan");
person1.setAge(27);
peopleList.add(person1);
People person2 = new People();
person2.setName("lisi");
person2.setAge(35);
peopleList.add(person2);
addPeopleWriter.write(peopleList);
// Query and output the result.
List<People> result = jdbcTemplate.query("SELECT * FROM people", (rs, rowNum) -> {
People person = new People();
person.setName(rs.getString("name"));
person.setAge(rs.getInt("age"));
return person;
});
System.out.println("Data in the people table:");
for (People person : result) {
System.out.println(person);
}
// Output a message to indicate that job execution is complete.
System.out.println("Batch Job execution completed.");
}
}
References
For more information about OceanBase Connector/J, see OceanBase Connector/J.