SpringBoot中@Insert、@Update實現(xiàn)批量新增更新的使用示例
一、使用@Insert批量新增
數(shù)據(jù)庫原始表數(shù)據(jù)

數(shù)據(jù)層接口
// 批量新增
@Insert("<script>" +
"INSERT INTO userInfo" +
" (id,name,money)" +
" VALUES" +
" <foreach collection ='list' item='userInfo' separator =','>" +
" (#{userInfo.id}, #{userInfo.name}, #{userInfo.money})" +
" </foreach >" +
"</script>")
void insertUsers(@Param("list") List<userInfo> userInfos);
注意:@Param(“list”) 引號中和foreach 中 collection 屬性值必須寫list,否則報錯。item屬性值寫實體類的類名,首字母必須小寫
接口測試:
由于在Apifox中是沒有對應(yīng)List的數(shù)據(jù)類型因此需要我們自己手寫body形式 – json例如:
[
{ "id":4,
"money":"102.3",
"name":"XU"
},
{ "id":5,
"money":"289.64",
"name":"RC"
}
]

除了使用接口測試工具驗證,我們還可以編寫業(yè)務(wù)測試類進(jìn)行測試:
package com.gy;
import com.gy.domain.userInfo;
import com.gy.service.UserService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import java.util.ArrayList;
import java.util.List;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
public class test {
@Autowired
UserService userService;
// 批量新增
@Test
public void test01(){
List<userInfo> userInfos =new ArrayList<>();
userInfos.add(new userInfo(4,"XU",102.3));
userInfos.add(new userInfo(5,"RC",289.64));
userService.insertUsers(userInfos);
}
}

使用以上兩種方式均可進(jìn)行測試驗證,此時數(shù)據(jù)均被批量新增成功?。?!

二、使用@Update批量更新
數(shù)據(jù)庫原始表數(shù)據(jù)見上圖
數(shù)據(jù)層接口:
// 批量更新
@Update("<script>" +
" <foreach collection ='list' item='userInfo' separator =';'>" +
"update userInfo set name=#{userInfo.name},money=#{userInfo.money} where id=#{userInfo.id}" +
"</foreach>" +
"</script>")
void updateList(@Param("list") List<userInfo> userInfos);
測試類:
// 批量修改
@Test
public void test04(){
List<userInfo> userInfos =new ArrayList<>();
userInfos.add(new userInfo(3,"NPL",1251.2));
userInfos.add(new userInfo(4,"LL",37.64));
userService.updateList(userInfos);
List<userInfo> infos = userService.getAll();
System.out.println(infos);
}
此處需注意踩坑(本人在此處解決問題時耗費較長時間)
測試運行后出現(xiàn)報錯信息,提示大概為sql語法出現(xiàn)異常,然而多次排查各種(數(shù)據(jù)庫、字段、表名等等…)問題均無果

發(fā)現(xiàn)sql能夠在navicat上面正常的運行,于是就只有看配置。后來了解到了批量必然要執(zhí)行多行sql。但是mybatis默認(rèn)是不開啟多行sql執(zhí)行的,于是修改配置,開啟MySQL多行sql執(zhí)行。
url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=UTC&allowMultiQueries = true
開啟多行sql執(zhí)行:在數(shù)據(jù)庫連接信息配置中 url后面加上:`allowMultiQueries = true
可以看到上訴問題立刻得到了解決?


mysql的url參數(shù)詳解:

到此這篇關(guān)于SpringBoot中@Insert、@Update實現(xiàn)批量新增更新的使用示例的文章就介紹到這了,更多相關(guān)SpringBoot @Insert、@Update批量新增更新內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
spring啟動錯誤Singleton bean creation not al
本文主要介紹了spring啟動錯誤Singleton bean creation not allowed while the singletons of this factory are indestruction,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
java(swing)+ mysql實現(xiàn)學(xué)生信息管理系統(tǒng)源碼
這篇文章主要分享了java mysql實現(xiàn)學(xué)生信息管理系統(tǒng)的源碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-11-11
關(guān)于swagger配置及踩坑@Api參數(shù)postion無效解決接口排序問題
這篇文章主要介紹了關(guān)于swagger配置及踩坑@Api參數(shù)postion無效解決接口排序問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-06-06
java利用udp實現(xiàn)發(fā)送數(shù)據(jù)
這篇文章主要為大家詳細(xì)介紹了java利用udp實現(xiàn)發(fā)送數(shù)據(jù),文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2020-07-07
Spring使用@Filter注解創(chuàng)建自定義過濾器
Spring 中鮮為人知但非常有用的注解之一是 @Filter,它支持自定義過濾器,下面我們就來深入研究一下如何使用 Spring 的 @Filter 注解來創(chuàng)建自定義過濾器吧2023-11-11

