Spring5学习笔记(13) — “JdbcTemplate 操作数据库(批量增删改)”

Posted 一切因为是码农

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring5学习笔记(13) — “JdbcTemplate 操作数据库(批量增删改)”相关的知识,希望对你有一定的参考价值。

JdbcTemplate 操作数据库(批量增删改)

1.JdbcTemplate的 批量增加、修改、删除操作
均由JdbcTemplate 的 batchUpdate 方法实现
该方法有两个参数:
参数一:sql(操作对应的sql语句)
参数二:List集合,添加多条记录数据


一、批量添加操作

//1.编写bookDao接口
public interface bookDao {
   //批量添加操作
    void batchAddbooks(List<Object[]> batchAddlist);
}

//2.编写接口实现类bookDaoImple
@Repository
    public class bookDaoImple implements bookDao{
      //注入JdbcTemplate
      @Autowired
      private JdbcTemplate jdbcTemplate;
        
	//批量添加操作
    @Override
    public void batchAddbooks(List<Object[]> batchAddlist) {
        String sql = "insert into book values(?,?,?)";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchAddlist);
        System.out.println(ints);
    }
}

//3.编写service类
@Service
public class bookService {

    //1.注入dao
    @Autowired
    private bookDao bookdao;

    //实现批量添加
    public void batchAddbook(List<Object[]> batchAddlist){
        bookdao.batchAddbooks(batchAddlist);
    }

}

//4.编写测试类
    //批量添加
        List<Object[]> batchAddlist = new ArrayList<>();
        Object[] o1 = {"5","aaa","a"};
        Object[] o2 = {"6","bbb","b"};
        Object[] o3 = {"7","ccc","c"};
        batchAddlist.add(o1);
        batchAddlist.add(o2);
        batchAddlist.add(o3);
        
        bookservice.batchAddbook(batchAddlist);

二、批量修改操作

//1.编写bookDao接口
public interface bookDao {
   //批量修改操作
    void batchupdate(List<Object[]> batchAddlist);
}

//2.编写接口实现类bookDaoImple
@Repository
    public class bookDaoImple implements bookDao{
      //注入JdbcTemplate
      @Autowired
      private JdbcTemplate jdbcTemplate;
        
	//批量修改操作
    @Override
    public void batchupdate(List<Object[]> batchAddlist) {
        String sql = "update book set bookname=?,bookstatus=? where bookid=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchAddlist);
        System.out.println(Arrays.toString(ints));
    }
}

//3.编写service类
@Service
public class bookService {

    //1.注入dao
    @Autowired
    private bookDao bookdao;

    //实现批量修改
    public void batchupdate(List<Object[]> batchAddlist){
        bookdao.batchupdate(batchAddlist);
    }

}

//4.编写测试类
    //批量修改
        List<Object[]> batchAddlist = new ArrayList<>();
        //注意数组中值的顺序要对应sql中参数出现的顺序
        Object[] o1 = {"aaa111","a1","5"};
        Object[] o2 = {"bbb222","b2","6"};
        Object[] o3 = {"ccc333","c3","7"};
        batchAddlist.add(o1);
        batchAddlist.add(o2);
        batchAddlist.add(o3);
        
        bookservice.batchupdate(batchAddlist);

三、批量删除操作

//1.编写bookDao接口
public interface bookDao {
   //批量删除操作
    void batchDelete(List<Object[]> batchAddlist);
}

//2.编写接口实现类bookDaoImple
@Repository
    public class bookDaoImple implements bookDao{
      //注入JdbcTemplate
      @Autowired
      private JdbcTemplate jdbcTemplate;
        
	//批量删除操作
    @Override
    public void batchDelete(List<Object[]> batchAddlist) {
        String sql = "delete from book where bookid=?";
        int[] ints = jdbcTemplate.batchUpdate(sql, batchAddlist);
        System.out.println(Arrays.toString(ints));
    }
}

//3.编写service类
@Service
public class bookService {

    //1.注入dao
    @Autowired
    private bookDao bookdao;

    //实现批量删除
    public void batchdelete(List<Object[]> batchAddlist) {
        bookdao.batchDelete(batchAddlist);
    }

}

//4.编写测试类
    //批量删除
        List<Object[]> batchAddlist = new ArrayList<>();
        Object[] o1 = {"5"};
        Object[] o2 = {"6"};
        Object[] o3 = {"7"};
        batchAddlist.add(o1);
        batchAddlist.add(o2);
        batchAddlist.add(o3);
        
        bookservice.batchdelete(batchAddlist);

以上是关于Spring5学习笔记(13) — “JdbcTemplate 操作数据库(批量增删改)”的主要内容,如果未能解决你的问题,请参考以下文章

Spring5学习笔记

Spring5学习笔记

Spring5学习笔记

Spring5学习笔记

Spring5学习笔记(14) — “Spring5 声明式事务管理”

Spring5学习笔记(14) — “Spring5 声明式事务管理”