MyBatis批量插入

来源:互联网 发布:靠谱的淘宝代购 编辑:程序博客网 时间:2024/06/13 21:27

一、

<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>


0 0