JdbcTemplate的基本配置参考我的上一篇文章: JdbcTemplate的基本使用-新增

1、修改和删除数据(jdbcTemplate.update)

修改和删除跟上面的新增操作一样,只是SQL语句不同而已。

UserServiceImpl 增加修改和删除方法:

package service;
import dao.UserDao;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Override
    public void updateUser(User user) {
        userDao.updateUser(user);
    }
    @Override
    public void deleteUser(int userId) {
        userDao.deleteUser(userId);
    }
}

UserDaoImpl 增加修改删除方法:

package dao;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
@Repository
public class UserDaoImpl implements UserDao{
    //注入JdbcTemplate
     @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void updateUser(User user) {
        String sql = "update user set name=?, password=? where id=?";
        int updateRow = jdbcTemplate.update(sql, user.getName(), user.getPassword(), user.getId());
        System.out.println(updateRow);
    }
    @Override
    public void deleteUser(int userId) {
        String sql = "delete from user where id=?";
        int updateRow = jdbcTemplate.update(sql, userId);
        System.out.println(updateRow);
    }
}

编写调用方法

package test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;import entity.User;
import service.UserService;
import service.UserServiceImpl;public class Test01 {
    ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean01.xml");
    //修改操作
    @Test
    public void test1() {
        User user = new User();
        user.setId(2);
        user.setName("AA");
        user.setPassword("112233");
        UserService userService = applicationContext.getBean(UserServiceImpl.class);
        userService.updateUser(user);
    }
    //删除操作
    @Test
    public void test2() {
        UserService userService = applicationContext.getBean(UserServiceImpl.class);
        userService.deleteUser(5);
    }
}

2、批量修改和删除(batchUpdate)

批量修改和批量删除都可以使用 jdbcTemplate.batchUpdate() 方法,该用于执行批处理相关语句,batchUpdate() 方法第二参数是一个元素为 Object[] 数组类型的 List 集合。

示例如下。UserServiceImpl 增加批量修改和删除方法:

package service;
import dao.UserDao;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    //批量修改
    @Override
    public void updateBatch(List<Object[]> listArg) {
        userDao.updateBatch(listArg);
    }
    //批量删除
    @Override
    public void deleteBath(List<Object[]> listArg) {
        userDao.deleteBath(listArg);
    }
}

UserDaoImpl 增加批量修改和删除方法:

package dao;
import entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import java.util.Arrays;
import java.util.List;
@Repository
public class UserDaoImpl implements UserDao {
    //注入JdbcTemplate
    @Autowired
    private JdbcTemplate jdbcTemplate;
    //批量修改
    @Override
    public void updateBatch(List<Object[]> listArg) {
        String sql = "update user set name=?, password=? where id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, listArg);
        System.out.println(Arrays.toString(ints));
    }
    //批量删除
    @Override
    public void deleteBath(List<Object[]> listArg) {
        String sql = "delete from user where id=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, listArg);
        System.out.println(Arrays.toString(ints));
    }
}

编写调用方法

package test;
import entity.User;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import service.UserService;
import service.UserServiceImpl;
import java.util.ArrayList;
import java.util.List;
public class TestMain {
    ApplicationContext ioc = new ClassPathXmlApplicationContext("bean01.xml");
    //批量修改
    @Test
    public void test3() {
        List<Object[]> userList = new ArrayList<>();
        Object[] arr1 = {"name1changed", "password1", 1};
        Object[] arr2 = {"name2changed", "password2", 2};
        Object[] arr3 = {"name3changed", "password3", 3};
        userList.add(arr1);
        userList.add(arr2);
        userList.add(arr3);
        UserService userService = ioc.getBean(UserServiceImpl.class);
        userService.updateBatch(userList);
    }
    //批量删除
    @Test
    public void test4() {
        List<Object[]> userList = new ArrayList<>();
        Object[] arr1 = {6};
        Object[] arr2 = {7};
        userList.add(arr1);
        userList.add(arr2);
        UserService userService = ioc.getBean(UserServiceImpl.class);
        userService.deleteBath(userList);
    }
}