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> ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.