經常在使用foreach是忘記其使用方式,做個筆記。
foreach元素屬性
foreach元素的屬性主要有 item,index,open,close ,separator,collection。
- item表示集合中每一個元素進行迭代時的別名
- index指 定一個名字,用于表示在迭代過程中,每次迭代到的位置
- open表示該語句以什么開始
- close表示以什么結束
- separator表示在每次進行迭代之間以什么符號作為分隔符
- collection傳入的是參數
collection用法
- 傳入的是單參數且參數類型是一個List的時候,collection屬性值為list
<foreach item="item" index="index" collection="list" open="(" separator="," close=")">
#{item}
</foreach>
- 傳入的是單參數且參數類型是一個array數組的時候,collection的屬性值為array
<foreach item="item" index="index" collection="array" open="(" separator="," close=")">
#{item}
</foreach>
- 傳入的多個參數時,需要把參數封裝成一個Map
<foreach item="item" index="index" collection= " xxx " open="(" separator="," close=")">
#{item}
</foreach>