mybatis insert foreach循環(huán)插入方式
@Insert('<script>' + 'insert into driver_account_appeal_photo (appeal_id,appeal_photo_path) valuesn' + '<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
問題描述:需要將程序里的一個(gè)集合保存到數(shù)據(jù)庫里,集合的類型對(duì)應(yīng)數(shù)據(jù)庫的一個(gè)實(shí)體,若在程序里遍歷集合再一條條保存到數(shù)據(jù)庫表中有點(diǎn)麻煩,這里可以利用MyBatis 的 foreach語句實(shí)現(xiàn)批量插入數(shù)據(jù)。
核心代碼清單:
Item(實(shí)體類):public class Item { private String itemCode;//項(xiàng)目代碼 private String itemName;//項(xiàng)目名稱 private String itemValue;//項(xiàng)目值(多個(gè)值用逗號(hào)隔開) private String itemCategory;//項(xiàng)目所屬類別 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實(shí)現(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 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 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元素是一個(gè)遍歷集合的循環(huán)語句,它支持遍歷數(shù)組,List和Set接口的集合。
foreach元素中,collection是傳進(jìn)來的參數(shù)名稱,可以是一個(gè)數(shù)組或者List、Set等集合;
item是循環(huán)中當(dāng)前的元素(配置的item的名字隨意取,類似于iterator);
index是當(dāng)前元素在集合中的位置下標(biāo);
seperator是各個(gè)元素的間隔符;
()分別是open和close元素,表示用什么符號(hào)將這些集合元素包裝起來。
注意:由于一些數(shù)據(jù)庫的SQL對(duì)執(zhí)行的SQL長度有限制,所以使用foreach元素的時(shí)候需要預(yù)估collection對(duì)象的長度;foreach除了用于本示例的循環(huán)插入,亦可用于構(gòu)建in條件中(可自行嘗試)。
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. Access數(shù)據(jù)庫日常維護(hù)方法(優(yōu)化)2. 巧用SQL語言在ACCESS數(shù)據(jù)庫中批量替換內(nèi)容3. MySQL基本調(diào)度策略淺析4. SQL Server靜態(tài)頁面導(dǎo)出技術(shù)45. 如何實(shí)現(xiàn)MySQL數(shù)據(jù)庫的備份與恢復(fù)6. 數(shù)據(jù)庫相關(guān)的幾個(gè)技能:ACCESS轉(zhuǎn)SQL7. mybatis 為什么千萬不要使用 where 1=18. Microsoft Office Access設(shè)置小數(shù)位數(shù)的方法9. DB2 常用命令小結(jié)10. 用Oracle 9i全索引掃描快速訪問數(shù)據(jù)

網(wǎng)公網(wǎng)安備