MyBatis之foreach

MyBatis之foreach

1.EmpMapper文件

  public void batchSave(List<Student> empList);

2.EmpMapper.xml文件

 <insert id="batchSave">
  insert into student(number,name,type,password) VALUES
    <foreach collection="list" item="emp" separator=",">
      (#{emp.number}, #{emp.name}, #{emp.type},#{emp.password})
    </foreach>
  </insert>

3.编写测试类

 @Test
    public void testBatchSave(){

        List<Student> list = new ArrayList<>();
        list.add(new Student("1","老坛","1","123"));
        list.add(new Student("2","酸菜","2","123"));
        list.add(new Student("3","红烧","2","123"));
        list.add(new Student("4","牛肉","1","123"));


        studentMapper.batchSave(list);

    }

4.效果图
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_42370891/article/details/88624676
今日推荐