Mybatis开启批量执行多个SQL语句
概述
mybatis默认不支持多条语句拼接插入或更新的,需要在数据库配置中配置相关参数,以允许执行分号相隔的多个SQL语句。
开启方法:
properties或yml配置文件中jdbc链接后追加&allowMultiQueries=true
开启批量执行配置示例:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driverClassName: com.mysql.cj.jdbc.Driver
druid:
master:
url: >-
jdbc:mysql://localhost:3306/mytest?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&allowMultiQueries=true
username: test
password: 123456
批量更新示例(多个update语句):
<update id="updateBatch" parameterType="java.util.List">
<foreach item="item" index="index" collection="list" separator=";">
update tb_user
set status = #{item.status},
update_by = #{item.updateBy},
update_time = #{item.updateTime}
where id = #{item.id}
</foreach>
</update>