Mybatis批量插入数据

Controller层:

Student stu=new Student();
stu.SetName("小三");
stu.SetAge(12);
stu.SetSex("男");
Student stu2=new Student();
stu.SetName("小四");
stu.SetAge(13);
stu.SetSex("男");
Student stu3=new Student();
stu.SetName("小五");
stu.SetAge(14);
stu.SetSex("女");
List list =new ArrayList();
list.add(stu);
list.add(stu2);
list.add(stu3);
StudentService.insert(list);

xml层:

<insert id="insertStu" parameterType="java.util.List">
        insert into
        tb_student
         (name,sex,age)
        VALUES
        <foreach collection="list" item="item" index= "index" separator =",">
            (#{item.name}, #{item.sex},#{item.age})
        </foreach>
    </insert>

 

posted @ 2020-05-28 18:00  乖怪丶  阅读(229)  评论(0编辑  收藏  举报
……