Spring基础(十三):JDBCTemplate的批操作
【摘要】
文章目录
JDBCTemplate的批操作
一、实体类
二、DeptService
三、DeptDao
四、测试
JDBCTemplate的批操作
一次连接,操作表格里的多条数据,就是批量操作。
案例操作
批量增加批量修改批量删除
一、实体类
package com.lanson.pojo;import...
文章目录
JDBCTemplate的批操作
一次连接,操作表格里的多条数据,就是批量操作。
案例操作
- 批量增加
- 批量修改
- 批量删除
一、实体类
-
package com.lanson.pojo;
-
import lombok.AllArgsConstructor;
-
import lombok.Data;
-
import lombok.NoArgsConstructor;
-
import java.io.Serializable;
-
/**
-
* @Author: Lansonli
-
* @Description: MircoMessage:Mark_7001
-
*/
-
@AllArgsConstructor
-
@NoArgsConstructor
-
@Data
-
public class Dept implements Serializable {
-
private Integer deptno;
-
private String dname;
-
private String loc;
-
}
二、DeptService
-
package com.lanson.service;
-
import com.lanson.pojo.Dept;
-
import java.util.List;
-
/**
-
* @Author: Lansonli
-
* @Description: MircoMessage:Mark_7001
-
*/
-
public interface DeptService {
-
int[] deptBatchAdd(List<Dept> depts);
-
int[] deptBatchUpdate(List<Dept> depts);
-
int[] deptBatchDelete(List<Integer> deptnos);
-
}
-
package com.lanson.service.impl;
-
import com.lanson.dao.DeptDao;
-
import com.lanson.pojo.Dept;
-
import com.lanson.service.DeptService;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Service;
-
import java.util.List;
-
/**
-
* @Author: Lansonli
-
* @Description: MircoMessage:Mark_7001
-
*/
-
@Service
-
public class DeptServiceImpl implements DeptService {
-
@Autowired
-
private DeptDao deptDao;
-
@Override
-
public int[] deptBatchAdd(List<Dept> depts) {
-
return deptDao.deptBatchAdd(depts);
-
}
-
@Override
-
public int[] deptBatchUpdate(List<Dept> depts) {
-
return deptDao.deptBatchUpdate(depts);
-
}
-
@Override
-
public int[] deptBatchDelete(List<Integer> deptnos) {
-
return deptDao.deptBatchDelete(deptnos);
-
}
-
}
三、DeptDao
-
package com.lanson.dao;
-
import com.lanson.pojo.Dept;
-
import java.util.List;
-
/**
-
* @Author: Lansonli
-
* @Description: MircoMessage:Mark_7001
-
*/
-
public interface DeptDao {
-
int[] deptBatchAdd(List<Dept> depts);
-
int[] deptBatchUpdate(List<Dept> depts);
-
int[] deptBatchDelete(List<Integer> deptnos);
-
}
-
package com.lanson.dao.impl;
-
import com.lanson.dao.DeptDao;
-
import com.lanson.pojo.Dept;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.jdbc.core.JdbcTemplate;
-
import org.springframework.stereotype.Repository;
-
import java.util.LinkedList;
-
import java.util.List;
-
/**
-
* @Author: Lansonli
-
* @Description: MircoMessage:Mark_7001
-
*/
-
@Repository
-
public class DeptDaoImpl implements DeptDao {
-
@Autowired
-
private JdbcTemplate jdbcTemplate;
-
@Override
-
public int[] deptBatchAdd(List<Dept> depts) {
-
String sql ="insert into dept values(DEFAULT,?,?)";
-
List<Object[]> args =new LinkedList<>();
-
for (Dept dept : depts) {
-
Object[] arg ={dept.getDname(),dept.getLoc()};
-
args.add(arg);
-
}
-
return jdbcTemplate.batchUpdate(sql, args);
-
}
-
@Override
-
public int[] deptBatchUpdate(List<Dept> depts) {
-
String sql ="update dept set dname =? ,loc =? where deptno=?";
-
List<Object[]> args =new LinkedList<>();
-
for (Dept dept : depts) {
-
Object[] arg ={dept.getDname(),dept.getLoc(),dept.getDeptno()};
-
args.add(arg);
-
}
-
return jdbcTemplate.batchUpdate(sql, args);
-
}
-
@Override
-
public int[] deptBatchDelete(List<Integer> deptnos) {
-
String sql ="delete from dept where deptno =?";
-
List<Object[]> args =new LinkedList<>();
-
for (Integer deptno : deptnos) {
-
Object[] arg ={deptno};
-
args.add(arg);
-
}
-
return jdbcTemplate.batchUpdate(sql, args);
-
}
-
}
四、测试
-
package com.lanson.test;
-
import com.lanson.pojo.Dept;
-
import com.lanson.service.DeptService;
-
import com.lanson.service.EmpService;
-
import org.junit.Test;
-
import org.springframework.context.ApplicationContext;
-
import org.springframework.context.support.ClassPathXmlApplicationContext;
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
import java.util.List;
-
/**
-
* @Author: Lansonli
-
* @Description: MircoMessage:Mark_7001
-
*/
-
public class Test2 {
-
@Test
-
public void testBatchAdd(){
-
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
-
DeptService deptService = context.getBean(DeptService.class);
-
List<Dept> depts =new ArrayList<>();
-
for (int i = 0; i < 10; i++) {
-
depts.add(new Dept(null,"name"+i,"loc"+i));
-
}
-
int[] ints = deptService.deptBatchAdd(depts);
-
System.out.println(Arrays.toString(ints));
-
}
-
@Test
-
public void testBatchUpdate(){
-
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
-
DeptService deptService = context.getBean(DeptService.class);
-
List<Dept> depts =new ArrayList<>();
-
for (int i = 51; i <=60; i++) {
-
depts.add(new Dept(i,"newname","newLoc"));
-
}
-
int[] ints = deptService.deptBatchUpdate(depts);
-
System.out.println(Arrays.toString(ints));
-
}
-
@Test
-
public void testBatchDelete(){
-
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
-
DeptService deptService = context.getBean(DeptService.class);
-
List<Integer> deptnos =new ArrayList<>();
-
for (int i = 51; i <=69; i++) {
-
deptnos.add(i);
-
}
-
int[] ints = deptService.deptBatchDelete(deptnos);
-
System.out.println(Arrays.toString(ints));
-
}
-
}
- 📢博客主页:https://lansonli.blog.csdn.net
- 📢欢迎点赞 👍 收藏 ⭐留言 📝 如有错误敬请指正!
- 📢本文由 Lansonli 原创,首发于 CSDN博客🙉
- 📢停下休息的时候不要忘了别人还在奔跑,希望大家抓紧时间学习,全力奔赴更美好的生活✨
文章来源: lansonli.blog.csdn.net,作者:Lansonli,版权归原作者所有,如需转载,请联系作者。
原文链接:lansonli.blog.csdn.net/article/details/126899417
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)