Mybatis在Mysql批量更新中不支持BEGIN与END关键字,所以如下配置是错误的:
<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="begin" close="end;" separator=";"> update test <set> test=${item.test}+1 </set> where id = ${item.id} </foreach> </update>
正确做法如下:
方法一:执行多条SQL语句
<update id="batchUpdate" parameterType="java.util.List"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update test <set> test=${item.test}+1 </set> where id = ${item.id} </foreach> </update>
方法二:执行一条SQL语句(建议) 写道
<update id="batchUpdate" parameterType="java.util.List"> update test set test=${item.test}+1 where id in <foreach collection="list" item="item" index="index" open="(" close=")" separator=","> ${item.id} </foreach> </update>