Mybatis遇到的坑
Mybatis遇到的坑
批量update\insert 失败,如果确定代码没问题,则看下jdbc配置,在application.properties配置文中的数据源url后面添加一个参数
&allowMultiQueries=true 【允许sql语句中有多个insert或者update语句 == 支持sql批量操作】
原来,Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾的,也就是不支持多条sql语句的执行
但是在SQL编辑器中执行多条sql语句的时候是可以以分号结尾的,如:
或者改变写法:
update book
<trim prefix="set" suffixOverrides=",">
<trim prefix="total_price1 =case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.totalPrice1}
</foreach>
</trim>
<trim prefix="total_price2 =case" suffix="end,">
<foreach collection="list" item="item" index="index">
when id=#{item.id} then #{item.totalPrice2}
</foreach>
</trim>
</trim>
where id in
<foreach collection="list" index="index" item="item" separator="," open="(" close=")">
#{item.id}
</foreach>
对应sql:
update book
set total_price =
case id
when 149201 then 5844
when 149200 then 49977
end,
book_name =
case id
end
where id in (149201,149200)