MyBatis标签获取数组或者集合长度的方法

1、判断列表长度:

bash 复制代码
<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、遍历数组:

bash 复制代码
<if test="array != null and array.length > 0">
  ...
</if>

3、获取数组或列表的元素(${}方式拼接字符串,注意安全):

bash 复制代码
${list[0]}
${array[0]}

4、总结:

在使用这些表达式时,请确保你的参数类型与表达式中使用的类型相匹配。例如,如果你传递的是 java.util.List,则应使用 list.size();如果你传递的是原生数组(如 String[]),则应使用 array.length。

相关推荐
郑州光合科技余经理1 小时前
代码展示:PHP搭建海外版外卖系统源码解析
java·开发语言·前端·后端·系统架构·uni-app·php
大大水瓶2 小时前
Tomcat
java·tomcat
dustcell.2 小时前
haproxy七层代理
java·开发语言·前端
游离态指针2 小时前
以为发消息=下单成功?RabbitMQ从0到秒杀实战的完整踩坑笔记
java
BD_Marathon2 小时前
工厂方法模式
android·java·工厂方法模式
玹外之音3 小时前
Spring AI MCP 无状态服务器实战:构建AI智能用户管理系统
java·后端
java干货3 小时前
Redis 的 ZipList 是什么?它是怎么解决内存碎片问题的?
java
失重外太空啦3 小时前
Tomcat
java·服务器·tomcat
屎到临头想搅便3 小时前
TOMCAT
java·tomcat
Sylvia33.3 小时前
火星数据:解构斯诺克每一杆进攻背后的数字语言
java·前端·python·数据挖掘·数据分析