Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Performing Batch Database Operations with Spring JdbcTemplate

Tech 1

Batch database opreations execute mlutiple table modifications over a single database connection, reducing overhead compared to separate single-statement cals.

package com.example.entity;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import java.io.Serializable;
@NoArgsConstructor
@AllArgsConstructor
@Data
public class Department implements Serializable {
    private Integer id;
    private String deptName;
    private String location;
}
package com.example.service;
import com.example.entity.Department;
import java.util.List;
public interface DepartmentService {
    int[] batchInsertDepartments(List<Department> deptList);
    int[] batchModifyDepartments(List<Department> deptList);
    int[] batchRemoveDepartmentsById(List<Integer> idList);
}
package com.example.service.impl;
import com.example.dao.DepartmentDao;
import com.example.entity.Department;
import com.example.service.DepartmentService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
@Service
public class DepartmentServiceImpl implements DepartmentService {
    @Autowired
    private DepartmentDao departmentDao;
    @Override
    public int[] batchInsertDepartments(List<Department> deptList) {
        return departmentDao.batchInsert(deptList);
    }
    @Override
    public int[] batchModifyDepartments(List<Department> deptList) {
        return departmentDao.batchUpdate(deptList);
    }
    @Override
    public int[] batchRemoveDepartmentsById(List<Integer> idList) {
        return departmentDao.batchDelete(idList);
    }
}
package com.example.dao;
import com.example.entity.Department;
import java.util.List;
public interface DepartmentDao {
    int[] batchInsert(List<Department> deptList);
    int[] batchUpdate(List<Department> deptList);
    int[] batchDelete(List<Integer> idList);
}
package com.example.dao.impl;
import com.example.dao.DepartmentDao;
import com.example.entity.Department;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.ArrayList;
import java.util.List;
@Repository
public class DepartmentDaoImpl implements DepartmentDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public int[] batchInsert(List<Department> deptList) {
        String insertSql = "INSERT INTO department (dept_name, location) VALUES (?, ?)";
        List<Object[]> paramList = new ArrayList<>();
        deptList.forEach(dept -> {
            Object[] params = {dept.getDeptName(), dept.getLocation()};
            paramList.add(params);
        });
        return jdbcTemplate.batchUpdate(insertSql, paramList);
    }
    @Override
    public int[] batchUpdate(List<Department> deptList) {
        String updateSql = "UPDATE department SET dept_name = ?, location = ? WHERE id = ?";
        List<Object[]> paramList = new ArrayList<>();
        deptList.forEach(dept -> {
            Object[] params = {dept.getDeptName(), dept.getLocation(), dept.getId()};
            paramList.add(params);
        });
        return jdbcTemplate.batchUpdate(updateSql, paramList);
    }
    @Override
    public int[] batchDelete(List<Integer> idList) {
        String deleteSql = "DELETE FROM department WHERE id = ?";
        List<Object[]> paramList = new ArrayList<>();
        idList.forEach(id -> {
            Object[] params = {id};
            paramList.add(params);
        });
        return jdbcTemplate.batchUpdate(deleteSql, paramList);
    }
}
package com.example.test;
import com.example.entity.Department;
import com.example.service.DepartmentService;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import java.util.LinkedList;
import java.util.List;
import java.util.Arrays;
public class BatchTest {
    @Test
    public void testBatchInsert() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        DepartmentService service = ctx.getBean(DepartmentService.class);
        List<Department> depts = new LinkedList<>();
        for (int j = 0; j < 8; j++) {
            depts.add(new Department(null, "TechTeam-" + j, "Office-" + j));
        }
        int[] results = service.batchInsertDepartments(depts);
        System.out.println(Arrays.toString(results));
    }
    @Test
    public void testBatchUpdate() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        DepartmentService service = ctx.getBean(DepartmentService.class);
        List<Department> depts = new LinkedList<>();
        for (int j = 40; j <= 47; j++) {
            depts.add(new Department(j, "Updated-Name", "Updated-Loc"));
        }
        int[] results = service.batchModifyDepartments(depts);
        System.out.println(Arrays.toString(results));
    }
    @Test
    public void testBatchDelete() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("spring-dao.xml");
        DepartmentService service = ctx.getBean(DepartmentService.class);
        List<Integer> ids = new LinkedList<>();
        for (int j = 40; j <= 55; j++) {
            ids.add(j);
        }
        int[] results = service.batchRemoveDepartmentsById(ids);
        System.out.println(Arrays.toString(results));
    }
}

Related Articles

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

SBUS Signal Analysis and Communication Implementation Using STM32 with Fus Remote Controller

Overview In a recent project, I utilized the SBUS protocol with the Fus remote controller to control a vehicle's basic operations, including movement, lights, and mode switching. This article is aimed...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.