mybatis标签获取数组或集合长度
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]}
总结
在使用这些表达式时,请确保你的参数类型与表达式中使用的类型相匹配。
例如,如果你传递的是 java.util.list,则应使用 list.size();如果你传递的是原生数组(如 string[]),则应使用 array.length。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。
发表评论