mybatis insert foreach循環(huán)插入方式
mybatis insert foreach循環(huán)插入
@Insert("<script>" + "insert into driver_account_appeal_photo (appeal_id,appeal_photo_path) values\n" + "<foreach collection=\"photoList\" item=\"item\" index=\"index\" separator=\",\">\n" + "\t(#{appealId},#{item})\n" + "</foreach>" + "</script>") //@Insert("insert into driver_account_appeal_photo (appeal_id,appeal_photo_path) values(#{appealId},#{appealPhotoPath})") void addAppealPhoto(AppealPhoto appealPhoto);
foreach語句批量插入數(shù)據(jù)
本例技術(shù):Spring+SpringMVC+MyBatis+Oracle
問題描述:
需要將程序里的一個集合保存到數(shù)據(jù)庫里,集合的類型對應(yīng)數(shù)據(jù)庫的一個實體,若在程序里遍歷集合再一條條保存到數(shù)據(jù)庫表中有點麻煩,這里可以利用MyBatis 的 foreach語句實現(xiàn)批量插入數(shù)據(jù)。
核心代碼清單:
Item(實體類):
public class Item { private String itemCode;//項目代碼 private String itemName;//項目名稱 private String itemValue;//項目值(多個值用逗號隔開) private String itemCategory;//項目所屬類別 public String getItemCode() { return itemCode; } public void setItemCode(String itemCode) { this.itemCode = itemCode; } public String getItemName() { return itemName; } public void setItemName(String itemName) { this.itemName = itemName; } public String getItemValue() { return itemValue; } public void setItemValue(String itemValue) { this.itemValue = itemValue; } public String getItemCategory() { return itemCategory; } public void setItemCategory(String itemCategory) { this.itemCategory = itemCategory; } }
Service實現(xiàn)層方法:
public Integer submitItem(List<Item> list ){ return researchMapper.submitItem(list); }
MyBatis的mapper配置文件的語句
在Oracle數(shù)據(jù)中,多條數(shù)據(jù)之間用union all 連接,MySQL數(shù)據(jù)庫用:
<insert id="submitItem" parameterType="java.util.List"> insert into ITEM ( ITEM_CODE, ITEM_NAME, ITEM_VALUE, ITEM_CATAGORY ) select item.* from ( <foreach collection="list" item="item" index="index" separator="UNION ALL" > select #{item.itemCode,jdbcType=VARCHAR}, #{item.itemName,jdbcType=VARCHAR}, #{item.itemValue,jdbcType=VARCHAR}, #{item.itemCategory,jdbcType=VARCHAR} from dual </foreach> ) item </insert>
<!--MySql寫法--> <insert id="submitItem" parameterType="java.util.List"> insert into ITEM ( ITEM_CODE, ITEM_NAME, ITEM_VALUE, ITEM_CATAGORY ) values <foreach collection="list" item="item" index="index" separator="," > ( #{item.itemCode,jdbcType=VARCHAR}, #{item.itemName,jdbcType=VARCHAR}, #{item.itemValue,jdbcType=VARCHAR}, #{item.itemCategory,jdbcType=VARCHAR} ) </foreach> </insert>
foreach元素解析:
foreach
元素是一個遍歷集合的循環(huán)語句,它支持遍歷數(shù)組,List和Set接口的集合。
foreach
元素中,collection是傳進來的參數(shù)名稱,可以是一個數(shù)組或者List、Set等集合;
item
是循環(huán)中當前的元素(配置的item的名字隨意取,類似于iterator);
index
是當前元素在集合中的位置下標;
seperator
是各個元素的間隔符;
()分別是open和close元素,表示用什么符號將這些集合元素包裝起來。
注意:由于一些數(shù)據(jù)庫的SQL對執(zhí)行的SQL長度有限制,所以使用foreach元素的時候需要預(yù)估collection對象的長度;foreach除了用于本示例的循環(huán)插入,亦可用于構(gòu)建in條件中(可自行嘗試)。
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Mybatis-Plus Wrapper條件構(gòu)造器超詳細使用教程
接口方法的參數(shù)中,會出現(xiàn)各種 Wrapper,比如 queryWrapper、updateWrapper 等。Wrapper 的作用就是用于定義各種各樣的條件(where)。所以不管是查詢、更新、刪除都會用到Wrapper2022-03-03為什么不建議使用Java自定義Object作為HashMap的key
這篇文章主要介紹了為什么不建議使用Java自定義Object作為HashMap的key,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06Java高性能新一代構(gòu)建工具Maven-mvnd(實踐可行版)
這篇文章主要介紹了Java高性能新一代構(gòu)建工具Maven-mvnd(實踐可行版),本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-06-06Spring boot 默認靜態(tài)資源路徑與手動配置訪問路徑的方法
這篇文章主要介紹了Spring boot 默認靜態(tài)資源路徑與手動配置訪問路徑的方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-05-05java書店系統(tǒng)畢業(yè)設(shè)計 總體設(shè)計(1)
這篇文章主要介紹了java書店系統(tǒng)畢業(yè)設(shè)計,第一步系統(tǒng)總體設(shè)計,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10