報錯sql如下:
<update id="updateBatch"> <foreach collection="list" item="item" index="index" separator=";"> update ICS_TASK_DETAILS set EXE_PEOPLE_ID = #{item.exePeopleId}, STATUS = #{item.status}, EXE_TIME = #{item.exeTime} where CAL_TASK_ID = #{item.calTaskId} and TASK_ID = #{item.taskId} </foreach> </update>
修改后sql如下,注意新增begin和;end;
<update id="updateBatch"> begin <foreach collection="list" item="item" index="index" separator=";"> update ICS_TASK_DETAILS <set> EXE_PEOPLE_ID = #{item.exePeopleId}, STATUS = #{item.status}, EXE_TIME = #{item.exeTime} </set> where CAL_TASK_ID = #{item.calTaskId} and TASK_ID = #{item.taskId} </foreach> ;end; </update>
批量更新時,foreach對傳入的數據迭代更新操作,foreach中主要存在collection、item、index、open、separate、close幾個參數:
collection為指定,指代Dao層接口傳遞的數據類型,分別是list、數組array和map。
item:別名,表示集合中每一個元素迭代時的別名,獲取數據時必須指定用別名來指定,如例子中所示,迭代循環時,使用item來獲取屬性值。
index:迭代下標,即迭代過程中的位置。
open:表示語句以什么開始。
separate:表示每次迭代之間以什么符號作為分割。
close:表示語句以什么結束。
歡迎批評指正。