spring--JdbcTemplate--03--批量增删改操作

本文介绍了如何使用Spring的JdbcTemplate进行批量插入、删除和更新操作,通过BookDaoBatchImpl中的方法展示如何避免queryForObject异常,并提供了完整的代码示例。

一、JdbcTemplate批量增删改api

int[] batchUpdate(String sql, List<Object[]> batchArgs)

batchUpdate增删改多个或0个时,不会有queryForObject抛异常问题

二、举例

(1)dao层代码

package com.fuping3.dao;

import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;

public class BookDaoBatchImpl implements BookDaoBatch{
    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int[] batchAddBook(List<Object[]> batchArgs) {
        String sql="INSERT INTO book(name,price) VALUES(?,?)";
        int[] ints=jdbcTemplate.batchUpdate(sql,batchArgs);
        return ints;
    }

    @Override
    public int[] batchDeleteBook(List<Object[]> batchArgs) {
        String sql="DELETE FROM book WHERE id=?";
        int[] ints=jdbcTemplate.batchUpdate(sql,batchArgs);
        return ints;
    }

    @Override
    public int[] batchUpdateBook(List<Object[]> batchArgs) {
        String sql="UPDATE book SET name=?,price=price-? WHERE id=?";
        int[] ints=jdbcTemplate.batchUpdate(sql,batchArgs);
        return ints;
    }


}

(2)service代码

package com.fuping3.service;

import com.fuping3.dao.BookDaoBatch;

import java.util.List;

public class BookServiceBatch {
    private BookDaoBatch bookDaoBatch;

    public void setBookDaoBatch(BookDaoBatch bookDaoBatch) {
        this.bookDaoBatch = bookDaoBatch;
    }

    public int[] batchAdd(List<Object[]> batchArgs){
        System.out.println("-------batchAdd...");
        return bookDaoBatch.batchAddBook(batchArgs);
    }
    public int[] batchDelete(List<Object[]> batchArgs){
        System.out.println("-------batchDelete...");
        return bookDaoBatch.batchDeleteBook(batchArgs);
    }

    public int[] batchUpdate(List<Object[]> batchArgs){
        System.out.println("-------batchUpdate...");
        return bookDaoBatch.batchUpdateBook(batchArgs);
    }
}

(3)测试代码

import com.fuping3.service.BookServiceBatch;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class TestBatch {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context=new ClassPathXmlApplicationContext("bean.xml");
        BookServiceBatch bookService = context.getBean("bookServiceBatch", BookServiceBatch.class);
        {
            List<Object[]> batchArgs = new ArrayList<>();
            Object[] o1={"aa",10};
            Object[] o2={"bb",11};
            Object[] o3={"cc",12};
            batchArgs.add(o1);
            batchArgs.add(o2);
            batchArgs.add(o3);
            int[] ints = bookService.batchAdd(batchArgs);
            System.out.println("批量添加结果:"+Arrays.toString(ints));
        }


        {
            List<Object[]> batchArgs = new ArrayList<>();
            Object[] o1={"aaa",5,13};
            Object[] o2={"bbb",6,14};
            Object[] o3={"ccc",10,60};
            batchArgs.add(o1);
            batchArgs.add(o2);
            batchArgs.add(o3);
            int[] ints = bookService.batchUpdate(batchArgs);
            System.out.println("批量修改结果:"+Arrays.toString(ints));
        }
        {
            List<Object[]> batchArgs = new ArrayList<>();
            Object[] o1={15};
            Object[] o2={16};
            Object[] o3={60};
            batchArgs.add(o1);
            batchArgs.add(o2);
            batchArgs.add(o3);
            int[] ints = bookService.batchDelete(batchArgs);
            System.out.println("批量删除结果:"+Arrays.toString(ints));
        }

    }
}

(4)输出

-------batchAdd...
四月 22, 2021 2:51:31 下午 com.alibaba.druid.support.logging.JakartaCommonsLoggingImpl info
信息: {dataSource-1} inited
批量添加结果:[1, 1, 1]
-------batchUpdate...
批量修改结果:[1, 1, 0]
-------batchDelete...
批量删除结果:[1, 1, 0]

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值