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。

相关推荐
MacroZheng11 小时前
Claude Code官方桌面端正式发布,夯爆了!
java·人工智能·后端
虚无境11 小时前
如何编写一个SpringBoot项目告警推送的Starter
java·prometheus·webhook
NE_STOP1 天前
Vide Coding--AI编程工具的选择
java
码云数智-园园1 天前
C++20 Modules 模块详解
java·开发语言·spring
程序员黑豆1 天前
JDK 下载安装与配置详细教程
java·前端·ai编程
小宇宙Zz1 天前
Maven依赖冲突
java·服务器·maven
swordbob1 天前
NIO的channel中什么是 fd(File Descriptor,文件描述符)
java·开发语言·nio
咖啡八杯1 天前
GoF设计模式——享元模式
java·spring·设计模式·享元模式
十五喵源码网1 天前
基于springboot2+vue2的租房管理系统
java·毕业设计·springboot·论文笔记