一、
<insert id="add" parameterType="Student">
<foreach collection="list" item="item" index="index" separator=";">
INSERT INTO TStudent(name,age) VALUES(#{item.name}, #{item.age})
</foreach>
</insert>
該方法和循環調用方法插入沒有區別,mapper接口方法返回值將是最一條INSERT語句的操作成功的記錄數目(就是0或1),而不是所有INSERT語句的操作成功的總記錄數
而且當其中一條不成功時,不會進行整體回滾。
二、
INSERT INTO Student(name,age)
<foreach collection="list" item="item" index="index" open="(" close=")" separator="union all">
SELECT #{item.name} as a, #{item.age} as b FROM DUAL
</foreach>