原文:http://www.cnblogs.com/zzlback/p/9342329.html
今天用mybatis實現批量更新,一直報錯,說我的sql語句不對,然后我還到mysql下面試了,明明沒問題,但就是過不去,原來問題在這。
在連接數據庫的url中要加入?allowMultiQueries=true這段,而且要放在第一行
然后dao層就如下寫
最后mapper.xml就是正常的寫法,解釋一下,我的collection="list",為什么寫list,因為傳入的是一個list集合,這里必須寫list,
如果傳入一個數組比如Integer[],那么就要寫collection="array"
<!-- 如果不是第一次參加考試,就更新學生的答案 --> <update id="updateStudentAnswer" parameterType="java.util.List"> <if test="list!=null"> <foreach collection="list" item="studentAnswer" index= "index" open="" close="" separator =";"> update studentanswerinfo <set> SAnswer=#{studentAnswer.SAnswer}, Getpoint=#{studentAnswer.Getpoint}, other=#{studentAnswer.other} </set> <where> questionID=#{studentAnswer.questionID} </where> </foreach> </if> </update>