MyBatis批量插入

一、

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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章