1. 程式人生 > >mybatis傳入List實現批量更新的坑

mybatis傳入List實現批量更新的坑

一行 bsp info com lose 學生 正常的 arr 個數

原文: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>

mybatis傳入List實現批量更新的坑