ANGADJAVA Code Samples Bytes How to modify employee details using Spring Batch and an H2 database.

How to modify employee details using Spring Batch and an H2 database.

0 Comments 8:49 pm

computer, portable, office-4860524.jpg

Assuming you have an Employee entity class with properties such as id, firstName, lastName, and salary, you can follow these steps:

  1. Set up your project dependencies as mentioned earlier, including the H2 database driver.
  2. Create an Employee entity class:
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Employee {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    private String firstName;

    private String lastName;

    private double salary;

    // Constructors, getters, and setters
}

Define a custom ItemProcessor to modify employee details:

import org.springframework.batch.item.ItemProcessor;

public class EmployeeProcessor implements ItemProcessor<Employee, Employee> {

    @Override
    public Employee process(Employee employee) {
        // Modify employee details
        String modifiedFirstName = employee.getFirstName().toUpperCase();
        double modifiedSalary = employee.getSalary() * 1.1; // Increase salary by 10%
        
        // Update the employee object
        employee.setFirstName(modifiedFirstName);
        employee.setSalary(modifiedSalary);
        
        return employee;
    }
}

Update the BatchConfiguration class:

import javax.sql.DataSource;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecutionListener;
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.item.ItemProcessor;
import org.springframework.batch.item.ItemReader;
import org.springframework.batch.item.ItemWriter;
import org.springframework.batch.item.database.JdbcBatchItemWriter;
import org.springframework.batch.item.database.builder.JdbcBatchItemWriterBuilder;
import org.springframework.batch.item.database.builder.JdbcCursorItemReaderBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

@Configuration
@EnableBatchProcessing
public class BatchConfiguration {

    private final JobBuilderFactory jobBuilderFactory;
    private final StepBuilderFactory stepBuilderFactory;
    private final DataSource dataSource;

    public BatchConfiguration(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory, DataSource dataSource) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
        this.dataSource = dataSource;
    }

    @Bean
    public ItemReader<Employee> reader() {
        return new JdbcCursorItemReaderBuilder<Employee>()
                .dataSource(dataSource)
                .sql("SELECT id, first_name, last_name, salary FROM employee")
                .rowMapper(new BeanPropertyRowMapper<>(Employee.class))
                .name("employeeItemReader")
                .build();
    }

    @Bean
    public ItemProcessor<Employee, Employee> processor() {
        return new EmployeeProcessor();
    }

    @Bean
    public ItemWriter<Employee> writer() {
        return new JdbcBatchItemWriterBuilder<Employee>()
                .itemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<>())
                .sql("UPDATE employee SET first_name=:firstName, salary=:salary WHERE id=:id")
                .dataSource(dataSource)
                .build();
    }

    @Bean
    public Step step1(ItemReader<Employee> reader, ItemProcessor<Employee, Employee> processor, ItemWriter<Employee> writer) {
        return stepBuilderFactory.get("step1")
                .<Employee, Employee>chunk(10)
                .reader(reader)
                .processor(processor)
                .writer(writer)
                .build();
    }

    @Bean
    public Job updateEmployeeJob(JobCompletionNotificationListener listener, Step step1) {
        return jobBuilderFactory.get("updateEmployeeJob")
                .listener(listener)
                .flow(step1)
                .end()
                .build();
    }

    @Bean
    public NamedParameterJdbcTemplate jdbcTemplate() {
        return new NamedParameterJdbcTemplate(dataSource);
    }
}

In this updated configuration, we’ve used a JdbcCursorItemReader to read employee details from the database, and a JdbcBatchItemWriter to update the modified employee details back to the database. The SQL statement in the writer specifies how to update the employee records.

Ensure you have an H2 database running and properly configured, with an “employee” table containing columns for id, first_name, last_name, and salary.

That’s it! With this setup, when you run the Spring Batch application, it will read the employee details, process them using the EmployeeProcessor, and update the modified details back to the database.

Leave a Reply

Your email address will not be published. Required fields are marked *