1.student类
package com.test.hou.po; public class Student { private int sid; private String sname; private int sage; public int getSid() { return sid; } public void setSid(int sid) { this.sid = sid; } public String getSname() { return sname; } public void setSname(String sname) { this.sname = sname; } public int getSage() { return sage; } public void setSage(int sage) { this.sage = sage; } }
2.StudentAction
public String batchInsertStudent(){ List<Student> studentslist=new ArrayList<Student>(); for (int i = 0; i < 10; i++) { Student student=new Student(); student.setSid(i); student.setSname("aa"+i); student.setSage(i+10); studentslist.add(student); } studentService.batchInsertStudent(studentslist); return "batchinsertstudent"; }
3.studentMapper.xml
<insert id="batchInsertStudent" parameterType="java.util.List"> insert into student <foreach collection="list" separator="union all" item="item" index="index"> (select #{item.sid},#{item.sname},#{item.sage} from dual ) </foreach> </insert>
注意:此处sql没有values, separator 为union all和select ----from dual
1.传递单参数且为list集合,collection 为list
2.传单参数且为数组,collection为array
3.传多参数且为map,collectin 为map的key
本文出自 “大话程序” 博客,请务必保留此出处http://houqida.blog.51cto.com/8877896/1562410