Reading from a database
This recipe shows you how to read data from a database as part of a read/process/write step.
Getting ready
Each user will be read from the database. Make sure that the user database table exists with some data in it:
CREATE TABLE user (
id BIGINT NOT NULL PRIMARY KEY AUTO_INCREMENT,
first_name TEXT,
age INT
);For each user row in the database, we'll create a User object. Make sure that the User class exists:
public class User {
private String firstName;
private int age;Make sure that the Datasource bean is defined with the database connection information.
How to do it…
Add a reader() method returning JdbcCursorItemReader-a class provided by Spring Batch:
@Bean
@StepScope
public JdbcCursorItemReader<User> reader() {
JdbcCursorItemReader<User> reader = new JdbcCursorItemReader<User>();
reader.setDataSource(dataSource());
reader.setSql("SELECT first_name, age FROM user");
reader.setRowMapper(new BeanPropertyRowMapper<User>(User...