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>

 

posted @ 2019-01-31 11:37  这个名字想了很久~  阅读(1969)  评论(0编辑  收藏  举报