使用mybatis批量更新sql
int batchUpdateLogStatus(@Param("idList") List<String> idList);
<update id="batchUpdateLogStatus" parameterType="java.util.List">
<foreach item="item" index="index" collection="idList" open="" separator=";" close="">
UPDATE tab_sy_log
set updatedTime = GETDATE()
<if test="item != null">
status = 0
</if>
where id = ${item}
</foreach>
</update>
结果
运行结果发现,传入的idList集合,如果是1个,更新成功,如果是多个,更新失败。
原因
Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾,也就是不支持多条sql语句的执行。需要在连接mysql的url上加 &allowMultiQueries=true
运行批量操作才可以执行。
解决办法
在连接mysql的url上加 &allowMultiQueries=true
# 数据库配置
spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver # mysql 8 需要添加时区
username: root
password: 123456
# &allowMultiQueries=true Mybatis映射文件中的sql语句默认是不支持以" ; " 结尾,也就是不支持多条sql语句的执行,加上这个才可以执行
url: jdbc:mysql://localhost:3306/onlinemall?useUnicode=true&characterEncoding=utf8&serverTimezone=GMT%2B8&useSSL=false&allowMultiQueries=true