关于使用循环,依次执行多条update的方式
方式如下:
<update id="updateBatch">
<foreach collection="list" item="item" index="index" separator=";" open="" close="">
update history_max_num
set max_num = #{item.maxNum,jdbcType=DECIMAL}
where id = #{item.id,jdbcType=BIGINT}
</foreach>
</update>
这种是以;结尾,多条update语句拼接的方式进行批量更新的。
但是Mybatis默认是不支持以" ; " 结尾的sql语句的,也就是不支持多条sql语句的批量执行。
需要在连接mysql的url加上 allowMultiQueries 这个参数。才可以执行。
&allowMultiQueries=true
示例
jdbc:mysql://localhost:3306/dbname?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&allowMultiQueries=true
本文介绍如何在Mybatis中使用foreach遍历更新多条记录,以及遇到的MySQL不支持多条SQL的限制,解决方法是调整数据库连接参数allowMultiQueries。
的问题&spm=1001.2101.3001.5002&articleId=124244795&d=1&t=3&u=0b0251f557cf418b921aa3ac2188a95a)
1万+

被折叠的 条评论
为什么被折叠?



