1、判斷列表長度:
<if test="list != null and list.size() > 0">...
</if>
可結合in條件使用:SELECT * FROM users<where><if test="idList != null and idList.size() > 0">id IN<foreach item="item" index="index" collection="idList" open="(" separator="," close=")">#{item}</foreach></if></where>
2、遍歷數組:
<if test="array != null and array.length > 0">...
</if>
3、獲取數組或列表的元素(${}方式拼接字符串,注意安全):
${list[0]}
${array[0]}
4、總結:
在使用這些表達式時,請確保你的參數類型與表達式中使用的類型相匹配。例如,如果你傳遞的是 java.util.List,則應使用 list.size();如果你傳遞的是原生數組(如 String[]),則應使用 array.length。