Mybatis批量插入和批量更新失敗問題
背景
Mybatis在執(zhí)行批量插入(方式二)和批量更新操作時,如果連接參數(shù)allowMultiQueries=false(默認),會批量操作失敗,而且sql語句copy出來運行正常
報錯如下圖:
如何解決此類問題
在設(shè)置數(shù)據(jù)庫連接時,添加allowMultiQueries=true
spring: datasource: url: jdbc:mysql://localhost:3306/db-test?allowMultiQueries=true
allowMultiQueries 參數(shù)
allowMultiQueries 是 MySQL 數(shù)據(jù)庫連接參數(shù)之一,用于指示是否允許在單個查詢中執(zhí)行多個 SQL 語句。
如果設(shè)置為 true,則允許執(zhí)行多個 SQL 語句,以分號 ; 分隔。
這在某些情況下可能會導(dǎo)致安全風(fēng)險,因為它可能會受到 SQL 注入攻擊的影響。
因此,默認情況下,allowMultiQueries 通常被設(shè)置為 false,以提高安全性。
Mybatis批量插入
方式一,單條SQL插入,allowMultiQueries=false,不會報錯
void batchInsert(@Param("list") List<UserInfo> list);
<insert id="batchInsert"> insert into t_user_info <trim prefix="(" suffix=")" suffixOverrides=","> <if test="list[0].username != null"> username, </if> <if test="list[0].pwd != null"> pwd, </if> <if test="list[0].createTime != null"> create_time, </if> </trim> values <foreach collection="list" item="item" index="index" separator=","> <trim prefix="(" suffix=")" suffixOverrides=","> <if test="item.username != null"> #{item.username}, </if> <if test="item.pwd != null"> #{item.pwd}, </if> <if test="item.createTime != null"> #{item.createTime}, </if> </trim> </foreach> </insert>
方式二,多條SQL插入,allowMultiQueries=false,會報錯
void batchInsert(@Param("list") List<UserInfo> list);
<insert id="batchInsert"> <foreach collection="list" item="item" index="index" separator=";"> insert into t_user_info <trim prefix="(" suffix=")" suffixOverrides=","> <if test="item.username != null"> username, </if> <if test="item.pwd != null"> pwd, </if> <if test="item.createTime != null"> create_time, </if> </trim> values <trim prefix="(" suffix=")" suffixOverrides=","> <if test="item.username != null"> #{item.username}, </if> <if test="item.pwd != null"> #{item.pwd}, </if> <if test="item.createTime != null"> #{item.createTime}, </if> </trim> </foreach> </insert>
Mybatis批量更新,allowMultiQueries=false,會報錯
void batchUpdate(@Param("list") List<UserInfo> list);
<update id="batchUpdate"> <foreach collection="list" item="item" index="index" open="" close="" separator=";"> update t_user_info <set> <if test="item.username != null"> username = #{item.username}, </if> <if test="item.pwd != null"> pwd = #{item.pwd}, </if> <if test="item.updateTime != null"> update_time = #{item.updateTime}, </if> </set> where id = #{item.id} </foreach> </update>
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java將List轉(zhuǎn)換為String的幾種方式
我們大家在實際開發(fā)中經(jīng)常遇到List轉(zhuǎn)為String字符串的情況,下面這篇文章主要給大家介紹了關(guān)于Java將List轉(zhuǎn)換為String的幾種方式,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2023-05-05Spring Boot 與 Kotlin 上傳文件的示例代碼
這篇文章主要介紹了Spring Boot 與 Kotlin 上傳文件的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-01-01spring boot使用thymeleaf為模板的基本步驟介紹
Spring Boot項目的默認模板引擎是Thymeleaf,這沒什么好說的,個人覺得也非常好,下面這篇文章主要給大家介紹了關(guān)于spring boot使用thymeleaf為模板的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2018-01-01SpringBoot中調(diào)用通用URL的實現(xiàn)
在 Spring Boot 應(yīng)用程序中,有時候我們需要調(diào)用一些通用的 URL 接口,本文主要介紹了SpringBoot中調(diào)用通用URL的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07Java讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法
下面小編就為大家?guī)硪黄狫ava讀取properties配置文件時,出現(xiàn)中文亂碼的解決方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-11-11解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題
這篇文章主要介紹了解決eclipse中maven引用不到已經(jīng)存在maven中jar包的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-10-10