• mybatis批量添加和删除


    实体类的映射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 + "行");
        }
    }
    

      

  • 相关阅读:
    泛型的内部原理:类型擦除以及类型擦除带来的问题
    内部类访问局部变量的时候,为什么变量必须加上final修饰
    Web 测试经验总结
    故障测试
    智能硬件产品的测试经验总结
    web端测试和移动端测试的区别小记
    MVC模式介绍
    重定向与转发
    MySQL权限管理分配
    pt-heartbeat工具监控MySQL复制延迟
  • 原文地址:https://www.cnblogs.com/woyujiezhen/p/11648390.html
Copyright © 2020-2023  润新知