1. <strong id="7actg"></strong>
    2. <table id="7actg"></table>

    3. <address id="7actg"></address>
      <address id="7actg"></address>
      1. <object id="7actg"><tt id="7actg"></tt></object>

        mybatis 中 foreach collection 的用法

        共 1713字,需瀏覽 4分鐘

         ·

        2021-12-19 13:46

        foreach元素的屬性主要有 item,index,collection,open,separator,close。

        屬性說明
        item表示集合中每一個元素進行迭代時的別名,
        index指 定一個名字,用于表示在迭代過程中,每次迭代到的位置,
        open表示該語句以什么開始,
        separator表示在每次進行迭代之間以什么符號作為分隔符,
        close表示以什么結束。

        在使用foreach的時候最關鍵的也是最容易出錯的就是collection屬性,該屬性是必須指定的,但是在不同情況 下,該屬性的值是不一樣的,主要有一下3種情況:

        1. 如果傳入的是單參數(shù)且參數(shù)類型是一個List的時候,collection屬性值為list

        2. 如果傳入的是單參數(shù)且參數(shù)類型是一個array數(shù)組的時候,collection的屬性值為array

        3. 如果傳入的參數(shù)是多個的時候,我們就需要把它們封裝成一個Map了,當然單參數(shù)也可

        上例子

        一、通過id獲取多條數(shù)據(jù)

        List 類型的我都配置了別名list,參數(shù)是?List

        ?,Article 是我自己定義的實體類


        二、批量插入數(shù)據(jù)



        insert into blog_article_label( article_id, label_id ) values
        "list" item="item" index="index" separator="," >

        ( #{item.article_id}, #{item.label_id} )

        insert>

        三、對一個字段進行多次模糊匹配

        select * from table
        <where>
        "list" item="item" index="index" separator="or">

        name like '%${item}%'

        where>

        上面的參數(shù)都是?List,如果是?String[]?這種的就是把collection 的值改為array,如下demo

        四、批量刪除


        DEKETE from blog_article a
        where a.article_id in

        #{item}


        五、批量修改

        參數(shù)是?Map?,我下面寫map 是因為配置了別名
        Java 代碼是這樣的:

        Map map = new HashMap<>();
        String[] ids = {"1","2","3"};
        map.put("content","修改的內容");
        map.put("ids",ids);

        mapper 文件


        UPDATE table
        SET content="#{content}"
        WHERE id in
        "ids" index="index" item="item" open="(" separator="," close=")">

        #{item}

        update>

        還有一種


        UPDATE usr_relation_umbrella
        SET child_number = CASE user_id
        "list" item="item">

        WHEN #{item.userId} THEN #{item.childNumber}

        END
        WHERE user_id IN
        "item" collection="list" index="index" open="(" separator="," close=")">
        #{item.userId}

        update>

        多個

        UPDATE categories 
        SET display_order = CASE id
        WHEN 1 THEN 3
        WHEN 2 THEN 4
        WHEN 3 THEN 5
        END,
        title = CASE id
        WHEN 1 THEN 'New Title 1'
        WHEN 2 THEN 'New Title 2'
        WHEN 3 THEN 'New Title 3'
        END
        WHERE id IN (1,2,3)

        喜歡,在看

        瀏覽 95
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        評論
        圖片
        表情
        推薦
        點贊
        評論
        收藏
        分享

        手機掃一掃分享

        分享
        舉報
        1. <strong id="7actg"></strong>
        2. <table id="7actg"></table>

        3. <address id="7actg"></address>
          <address id="7actg"></address>
          1. <object id="7actg"><tt id="7actg"></tt></object>
            国产福利社区 | 一级a一级a爱片免费免免高潮 | 从小被开发成淫娃日本视频 | rylskyart裸体欧美极品 | 狠狠躁日日躁夜夜躁A片视频小说 | 成品片a免费入口麻豆 | 精品国产999久久久免费 | av无码在线观看 红桃精品91一区二区久 | 国产精品爽爽爽爽爽爽在线观看 | 91人妻人人澡人人爽人人精品乱 |