9、Spring JDBCTemplate 批量操作数据

@Data
@NoArgsConstructor
@AllArgsConstructor
public class Dept implements Serializable {
    private Integer deptno;
    private String dname;
    private String loc;
}
public interface DeptService {
    int[] addDeptBatch(List<Dept> deptList);

    int[] updateDeptBatch(List<Dept> deptList);

    int[] deleteDeptBatch(List<Integer> deptnos);
}
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptDAO deptDAO;

    public int[] addDeptBatch(List<Dept> deptList) {
        return deptDAO.addDeptBatch(deptList);
    }

    public int[] updateDeptBatch(List<Dept> deptList) {
        return deptDAO.updateDeptBatch(deptList);
    }

    @Override
    public int[] deleteDeptBatch(List<Integer> deptnos) {
        return deptDAO.deleteDeptBatch(deptnos);
    }
}
public interface DeptDAO {
    int[] addDeptBatch(List<Dept> deptList);

    int[] updateDeptBatch(List<Dept> depts);

    int[] deleteDeptBatch(List<Integer> deptnos);

}

@Repository
public class DeptDAOImpl implements DeptDAO {
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public int[] addDeptBatch(List<Dept> deptList) {
        String sql = "insert into dept values (default,?,?)";
        List<Object[]> args = new ArrayList<Object[]>();

        for (Dept dept : deptList) {
            Object[] objects = {dept.getDname(), dept.getLoc()};
            args.add(objects);
        }

        return jdbcTemplate.batchUpdate(sql, args);
    }

    public int[] updateDeptBatch(List<Dept> depts) {
        String sql = "update dept set dname=?,loc=? where deptno=?";
        List<Object[]> args = new ArrayList<Object[]>();

        for (Dept dept : depts) {
            Object[] objects = {dept.getDname(), dept.getLoc(), dept.getDeptno()};
            args.add(objects);
        }

        return jdbcTemplate.batchUpdate(sql, args);
    }

    public int[] deleteDeptBatch(List<Integer> deptnos) {
        String sql = "delete from dept where deptno=?";
        List<Object[]> args = new ArrayList<>();
        for (Integer deptno : deptnos) {
            Object[] objects = {deptno};

            args.add(objects);
        }

        return jdbcTemplate.batchUpdate(sql, args);
    }
}

public class Test02 {
    // 批量增加部门
    @Test
    public void addDeptBatch() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DeptService deptService = applicationContext.getBean(DeptService.class);
        List<Dept> depts = new ArrayList<Dept>();
        for (int i = 0; i < 10; i++) {
            depts.add(new Dept(null, "dname" + i, "loc" + i));
        }
        int[] ints = deptService.addDeptBatch(depts);
        System.out.println("Arrays.toString(ints) = " + Arrays.toString(ints));

    }

    // 批量修改部门
    @Test
    public void updateDeptBatch() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DeptService deptService = applicationContext.getBean(DeptService.class);
        List<Dept> depts = new ArrayList<Dept>();
        for (int i = 45; i < 55; i++) {
            depts.add(new Dept(i, "newdname" + i, "newloc" + i));
        }
        int[] ints = deptService.updateDeptBatch(depts);
        System.out.println("Arrays.toString(ints) = " + Arrays.toString(ints));

    }


    // 批量删除部门
    @Test
    public void deleteDeptBatch() {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        DeptService deptService = applicationContext.getBean(DeptService.class);
        List<Integer> deptnos = new ArrayList<>();
        for (int i = 45; i < 55; i++) {
            deptnos.add(i);
        }
        int[] ints = deptService.deleteDeptBatch(deptnos);
        System.out.println("Arrays.toString(ints) = " + Arrays.toString(ints));

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值