实体类的映射xml文件
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//OTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.dao.EmployeeDao"> <delete id="delete"> delete from employee where employee_id in <foreach collection="collection" open="(" close=")" index="index" item="id" separator=","> #{id} </foreach> </delete> <insert id="insert"> insert into employee(employee_id,employee_name,employee_gender,employee_salary,dept_id) values <foreach collection="list" item="emp" separator=","> (#{emp.employeeId}, #{emp.employeeName}, #{emp.employeeGender}, #{emp.employeeSalary}, #{emp.deptId}) </foreach> </insert> </mapper>
EmployeeDao.class
public interface EmployeeDao { int delete(List<Integer> employeeIds); int insert(List<Employee> employees); }
测试代码
public class EmployeeDaoTest { @Test public void testDelete() { List<Integer> list = new ArrayList<>(); list.add(7); list.add(9); EmployeeDao employeeDao = MapperFactory.generateMapper(EmployeeDao.class); int delele = employeeDao.delete(list); System.out.println("成功删除" + delele + "行"); } @Test public void testInsert() { List<Employee> employees = new ArrayList<>(); employees.add(new Employee(100,"哦哈",1,new BigDecimal(3000),4)); employees.add(new Employee(101,"哦哈",1,new BigDecimal(3000),4)); employees.add(new Employee(102,"哦哈",1,new BigDecimal(3000),4)); EmployeeDao employeeDao = MapperFactory.generateMapper(EmployeeDao.class); int insert = employeeDao.insert(employees); System.out.println("成功添加" + insert + "行"); } }