(1)、先建立表Student
CREATE TABLE Student(
id INT NOT NULL PRIMARY KEY,
name VARCHAR(50) NOT NULL,
class VARCHAR(32) NOT NULL
)
(2)、StudentDao类接口
void insertStu(List<Student> stuList);
(3)、StudentDao.xml文件
<select id="insertStu" parameterType="java.util.ArrayList">
insert into Student(id,name,class)
values
<foreach collection="list" index="index" item="item" open="(" seperator="," close=")">
(#{item.id},#{item.name},#{item.class})
</foreach>
</select>
(4)foreach属性解释
foreach元素的属性主要有 item,index,collection,open,separator,close。
item表示集合中每一个元素进行迭代时的别名,
index指 定一个名字,用于表示在迭代过程中,每次迭代到的位置,
open表示该语句以什么开始,
separator表示在每次进行迭代之间以什么符号作为分隔 符,
close表示以什么结束。
(5)collection参数解释:
1. 如果传入的是单参数且参数类型是一个List的时候,collection属性值为list
2. 如果传入的是单参数且参数类型是一个array数组的时候,collection的属性值为array
3. 如果传入的参数是多个的时候,我们就需要把它们封装成一个Map了,当然单参数也可以