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


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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM