zoukankan      html  css  js  c++  java
  • 使用foreach进行批量更新

    public void addEmps(@Param("emps")List<Employee> emps);

    2映射文件配置

    <!-- 批量保存 -->
    <!--public void addEmps(@Param("emps")List<Employee> emps); -->
    <!--MySQL下批量保存:可以foreach遍历 mysql支持values(),(),()语法-->
    <insert id="addEmps">
    insert into tbl_employee(last_name,email,gender,did)
    values
    <foreach collection="emps" item="emp" separator=",">
    (#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
    </foreach>
    </insert><!-- -->

    <!-- 这种方式需要数据库连接属性allowMultiQueries=true;
    这种分号分隔多个sql可以用于其他的批量操作(删除,修改) -->
    <!-- <insert id="addEmps">
    <foreach collection="emps" item="emp" separator=";">
    insert into tbl_employee(last_name,email,gender,d_id)
    values(#{emp.lastName},#{emp.email},#{emp.gender},#{emp.dept.id})
    </foreach>
    </insert> -->

    3进行测试

    @Test
    public void testBatchSave() throws IOException{
    SqlSessionFactory sqlSessionFactory = getSqlSessionFactory();
    SqlSession openSession = sqlSessionFactory.openSession();
    try{
    EmployeeMapperDynamicSQL mapper = openSession.getMapper(EmployeeMapperDynamicSQL.class);
    List<Employee> emps = new ArrayList<>();
    emps.add(new Employee(null, "smith0x1", "smith0x1@atguigu.com", "1",new Department(1)));
    emps.add(new Employee(null, "allen0x1", "allen0x1@atguigu.com", "0",new Department(1)));
    mapper.addEmps(emps);
    openSession.commit();
    }finally{
    openSession.close();
    }
    }

  • 相关阅读:
    About Face 摘录
    断言的使用
    C#中值传递和引用传递
    C++技巧之断言Assert
    About Face 一 目标导向设计
    About Face 二 设计行为与形态
    C++中引用传递与指针传递区别
    一个新的时代SoLoMo
    离散数学笔记算法部分
    汪教授的离散数学20110308 谓词与量词2
  • 原文地址:https://www.cnblogs.com/zhangzhiqin/p/8567452.html
Copyright © 2011-2022 走看看