Mysql批量更新的一个坑-&allowMultiQueries=true允许批量更新

前言

        实际上,我们经常会遇到这样的需求,那就是利用Mybatis批量更新或者批量插入,但是,实际上即使Mybatis完美支持你的sql,你也得看看你说操作的数据库是否支持,最近就遇到这样的一个坑。

 <foreach collection="batchList" item="detail" separator=";">
            update batch_detail
            <trim prefix="SET" suffixOverrides=",">
                <if test="detail.status != null and detail.status != ''">detail_status = #{detail.status},</if>
                <if test="detail.reason != null and detail.reason != ''">reason = #{detail.reason},</if>
                <if test="detail.updateTime != null">update_time = #{detail.updateTime}</if>
            </trim>
            <where>
                id = #{detail.Id}
            </where>
        </foreach>

看似似乎没有一点问题,这里用到了Mybatis的动态sql,实际上说白了也就是拼sql,不过这个繁杂的工作交给Mybatis帮我们去做了。可是,只要一执行就要报语法错误。调试了好久,发现只要传一个值进去就没有问题,就是list的成员只有一个。这引起了我的警觉。

解决方案

        后来发现,原来mysql的批量更新是要我们主动去设置的, 就是在数据库的连接url上设置一下,加上* &allowMultiQueries=true *即可。

愿你眼中有光芒,活成你想要的模样
原文地址:https://www.cnblogs.com/SmallStrange/p/15305499.html