<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          mybatis 中 foreach collection 的用法

          共 1713字,需瀏覽 4分鐘

           ·

          2021-12-19 13:46

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

          屬性說(shuō)明
          item表示集合中每一個(gè)元素進(jìn)行迭代時(shí)的別名,
          index指 定一個(gè)名字,用于表示在迭代過(guò)程中,每次迭代到的位置,
          open表示該語(yǔ)句以什么開(kāi)始,
          separator表示在每次進(jìn)行迭代之間以什么符號(hào)作為分隔符,
          close表示以什么結(jié)束。

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

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

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

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

          上例子

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

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

          ?,Article 是我自己定義的實(shí)體類


          二、批量插入數(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>

          三、對(duì)一個(gè)字段進(jìn)行多次模糊匹配

          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?,我下面寫(xiě)map 是因?yàn)榕渲昧藙e名
          Java 代碼是這樣的:

          Map map = new HashMap<>();
          String[] ids = {"1","2","3"};
          map.put("content","修改的內(nèi)容");
          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>

          多個(gè)

          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
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  91尤物视频 | 成人网站十八禁 | 欧美日韩无码视频 | 国产大黄片久久久久久 | 夜夜撸性视频 |