亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

mybatis insert foreach循環(huán)插入方式

 更新時間:2021年07月13日 10:27:45   作者:學(xué)亮編程手記  
這篇文章主要介紹了mybatis insert foreach循環(huán)插入方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

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í)體,若在程序里遍歷集合再一條條保存到數(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)目值(多個值用逗號隔開)
    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 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是傳進(jìn)來的參數(shù)名稱,可以是一個數(shù)組或者List、Set等集合;

item是循環(huán)中當(dāng)前的元素(配置的item的名字隨意取,類似于iterator);

index是當(dāng)前元素在集合中的位置下標(biāo);

seperator是各個元素的間隔符;

()分別是open和close元素,表示用什么符號將這些集合元素包裝起來。

注意:由于一些數(shù)據(jù)庫的SQL對執(zhí)行的SQL長度有限制,所以使用foreach元素的時候需要預(yù)估collection對象的長度;foreach除了用于本示例的循環(huán)插入,亦可用于構(gòu)建in條件中(可自行嘗試)。

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論