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ù)測試類進行測試:
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); } }
使用以上兩種方式均可進行測試驗證,此時數(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默認是不開啟多行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)文章
關(guān)于maven pom文件配置加載lib下的jar包
在項目中引用放置在lib文件夾中的第三方j(luò)ar包需要通過POM文件進行特定配置,雖然將依賴放置在公司的Maven私服是更好的做法,但如果遇到部署問題,可以利用maven-jar-plugin進行配置,Spring Boot項目可以通過特定設(shè)置來實現(xiàn)2024-09-09IDEA創(chuàng)建Java Web項目不能及時刷新HTML或JSP頁面問題
這篇文章主要介紹了IDEA創(chuàng)建Java Web項目不能及時刷新HTML或JSP頁面問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-03-03java 定時器Timer和TimerTask的使用詳解(執(zhí)行和暫停)
這篇文章主要介紹了java 定時器Timer和TimerTask的使用詳解(執(zhí)行和暫停),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧2023-11-11