BulingBuling

mybatis中<foreach>标签的使用

标签太多,记一下不是特别常用的标签~留着脑袋瓜不机灵的时候看!

<foreach>标签  该标签的作用是遍历集合类型的条件 

<select id="countByUserList" parameterType="list">
  SELECT COUNT(*) FROM users
  WHERE user_id IN
  <foreach item="userList" collection="list" separator="," open="(" close=")" index="">
    #{item.userId}
  </foreach>
</select>

注:SELECT COUNT(*) FROM users WHERE user_id IN(1,2,3)

* collection = "array" / collection = "list" 

  是数组类型,还是集合类型(一共有三种类型,分别为List, [](array), Map三种)该参数为必选。

-----------------------------------------------------------------------------------------------------------

* item = "userList"

  循环体中的具体对象,在list和数组中是其中的对象,在map中是value。 该参数为必选。

-----------------------------------------------------------------------------------------------------------

* open = "("  separator = ","  close = ")"

  开始符号,分隔符号,结束符号(常用在in(),values()时)该参数可选。

-----------------------------------------------------------------------------------------------------------

* index =  " " 

  在list和数组中,index是元素的序号,在map中,index是元素的key,该参数可选。

-----------------------------------------------------------------------------------------------------------

* separator = ","

  元素之间的分隔符。例如在in()的时候,separator=","会自动在元素中间用“,“隔开,避免手动输入逗号导致sql错误,如in(1,2,)这样。该参数可选。

 

分类:

技术点:

相关文章:

  • 2021-09-09
  • 2021-12-05
  • 2021-09-09
  • 2021-11-07
  • 2021-05-17
  • 2021-06-03
  • 2022-12-23
  • 2021-04-18
猜你喜欢
  • 2021-10-25
  • 2021-09-09
  • 2021-09-09
  • 2021-09-09
  • 2021-07-26
  • 2021-09-09
  • 2021-09-09
相关资源
相似解决方案