常用批量操作
7.1 批量新增数据
7.1.1 映射文件定义SQL
insert into dept(dept_name,dept_address) values
(#{dept.deptName},#{dept.deptAddress})
7.1.2 实现类DeptDaoImpl.java
package com.demo.dao.impl;
import java.util.List;
import org.apache.ibatis.session.SqlSession;
import com.demo.entity.Dept;
import com.demo.util.MyBatisUtil;
public class DeptDaoImpl {
SqlSession session;
//批量添加部门信息
public int insertDeptList(List depts){
int i = 0;
try {
session = MyBatisUtil.getSession();
i = session.insert("com.demo.entity.DeptMapper.insertDeptList", depts);
session.commit();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
session.rollback();
}finally{
try {
MyBatisUtil.closeSession();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return i;
}
}
7.1.3 测试类TestDeptDaoImpl.java
package com.demo.test;
import java.util.ArrayList;
import java.util.List;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.demo.dao.impl.DeptDaoImpl;
import com.demo.entity.Dept;
public class TestDeptDaoImpl {
private static DeptDaoImpl deptDaoImpl;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
deptDaoImpl = new DeptDaoImpl();
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
deptDaoImpl = null;
}
// 批量添加部门信息
@Test
public void testInsertDeptList() {
List depts = new ArrayList();
for (int i = 0; i < 5; i++) {
Dept dept = new Dept();
dept.setDeptName("deptName"+i);
dept.setDeptAddress("deptAddress"+i);
depts.add(dept);
}
System.out.println("受影响的行数:"+deptDaoImpl.insertDeptList(depts));
}
}
4、测试效果:
7.2 批量删除部门
7.2.1 映射文件定义SQL
delete from dept where dept_id in(
#{deptId}
)
7.2.2 编写批量删除部门的方法
//根据部门编号批量删除部门信息
public int deleteDeptList(Integer[] deptIds){
int i = 0;
try {
session = MyBatisUtil.getSession();
i = session.delete("com.demo.entity.DeptMapper.deleteDeptList", deptIds);
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
try {
MyBatisUtil.closeSession();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return i;
}
7.2.3 编写测试代码
// 批量删除部门信息
@Test
public void testDeleteDeptList() {
Integer [] deptIds = {5,7,8};
System.out.println("受影响的行数:"+deptDaoImpl.deleteDeptList(deptIds));
}
7.2.4 测试效果
7.3 批量更新数据
7.3.1 修改db.properties文件
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/mybatis?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true
name=root
password=
7.3.2 映射文件定义SQL
update dept set dept_name = #{dept.deptName},dept_address = #{dept.deptAddress}
where dept_id = #{dept.deptId}
7.3.3 编写批量删除部门的方法
//批量修改部门信息
public int updateDeptList(List depts){
int i = 0;
try {
session = MyBatisUtil.getSession();
i = session.update("com.demo.entity.DeptMapper.updateDeptList", depts);
session.commit();
} catch (Exception e) {
e.printStackTrace();
session.rollback();
}finally{
try {
MyBatisUtil.closeSession();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return i;
}
7.3.4 编写测试代码
// 批量修改部门信息
@Test
public void testUpdateDeptList() {
List depts = new ArrayList();
for (int i = 1; i < 5; i++) {
Dept dept = new Dept();
dept.setDeptId(i);
dept.setDeptName("deptNameaaa"+i);
dept.setDeptAddress("deptAddressaaa"+i);
depts.add(dept);
}
System.out.println("受影响的行数:"+deptDaoImpl.updateDeptList(depts));
}
7.3.5 测试效果