关于使用循环,依次执行多条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