zoukankan      html  css  js  c++  java
  • 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 + "行");
        }
    }
    

      

  • 相关阅读:
    将Excel嵌入你的.Net程序
    调用资源文件
    Socket Error# Description
    LPCTSTR 和其它
    linux send and recv详解
    stdafx.h的作用
    setsocketopt() usage
    openfire源码入门级分析
    openfire分析
    关于xmpp
  • 原文地址:https://www.cnblogs.com/woyujiezhen/p/11648390.html
Copyright © 2011-2022 走看看