mybatis 中if标签内容 判断详解

MyBatis 的 <if> 标签通过 OGNL 表达式进行条件判断,支持丰富的逻辑和对象操作。以下是常见用法和注意事项的详细总结:


一、基本判断语法

xml 复制代码
<if test="条件表达式">
    SQL片段
</if>

二、常见判断场景

1. 简单值比较

  • 数字类型(无需引号):

    xml 复制代码
    <if test="age == 18"> ... </if>       <!-- 等于 -->
    <if test="age != 20"> ... </if>      <!-- 不等于 -->
    <if test="age gt 10"> ... </if>      <!-- 大于 -->
    <if test="age lt 30"> ... </if>      <!-- 小于 -->
  • 字符串类型(需单引号包裹):

    xml 复制代码
    <if test="status == 'ACTIVE'"> ... </if>    <!-- 字符串相等 -->
    <if test="status eq 'INACTIVE'"> ... </if>  <!-- eq 等效于 == -->
    <if test="status != 'DELETED'"> ... </if>

2. 对象属性判断

xml 复制代码
<if test="user.name != null"> ... </if>        <!-- 判断对象属性非空 -->
<if test="order.amount gt 100"> ... </if>      <!-- 嵌套属性判断 -->

3. 集合/数组判断

xml 复制代码
<if test="list != null and list.size() > 0"> ... </if>  <!-- 集合非空 -->
<if test="array.length > 0"> ... </if>                <!-- 数组非空 -->

4. 逻辑运算

xml 复制代码
<if test="(a == 1) or (b == 2)"> ... </if>     <!-- 逻辑或 -->
<if test="flag and !error"> ... </if>          <!-- 逻辑与、非 -->

5. 方法调用

xml 复制代码
<if test="name.toString() == 'admin'"> ... </if>          <!-- 调用toString -->
<if test="@com.example.StringUtils@isBlank(email)"> ... </if>  <!-- 调用静态方法 -->

三、特殊场景处理

1. 枚举类型判断

xml 复制代码
<!-- 假设枚举类型 Status 有 ACTIVE, INACTIVE -->
<if test="status.name() == 'ACTIVE'"> ... </if>    <!-- 使用 name() 获取枚举名 -->
<if test="status.toString() == 'ACTIVE'"> ... </if> <!-- 或 toString() -->

2. 避免空指针(Null Safety)

xml 复制代码
<!-- 先判空再调用方法 -->
<if test="user != null and user.isAdmin()"> ... </if> 

3. 布尔值判断

xml 复制代码
<if test="isDeleted"> ... </if>        <!-- 等效于 isDeleted == true -->
<if test="!isDeleted"> ... </if>       <!-- 等效于 isDeleted == false -->

4. 空字符串与 Null

xml 复制代码
<if test="name != null and name != ''"> ... </if>  <!-- 同时排除 null 和空字符串 -->

四、易错点及解决

1. 字符串比较陷阱

  • 错误示例 (比较对象地址):

    xml 复制代码
    <if test="status == ACTIVE"> ... </if>  <!-- 缺少单引号,OGNL 会解析为变量 -->
  • 正确写法

    xml 复制代码
    <if test="status == 'ACTIVE'"> ... </if>  <!-- 用单引号包裹字符串 -->

2. 数字类型与字符串隐式转换

  • 错误示例 (类型不匹配):

    xml 复制代码
    <if test="id == '1'"> ... </if>  <!-- id 是 Long 类型,比较会失败 -->
  • 正确写法

    xml 复制代码
    <if test="id == 1"> ... </if>    <!-- 去掉引号,确保类型一致 -->

3. 使用 toString() 的场景

  • 适用情况 :将非字符串类型转换为字符串比较。

    xml 复制代码
    <if test="id.toString() == '1001'"> ... </if>  <!-- 将 Long 转为 String -->

五、高级技巧

1. 使用工具类方法

xml 复制代码
<if test="@org.apache.commons.lang3.StringUtils@isNotBlank(name)">
  AND name = #{name}
</if>

2. 多条件组合

xml 复制代码
<where>
  <if test="name != null"> AND name = #{name} </if>
  <if test="age != null"> AND age = #{age} </if>
</where>

3. 动态 SQL 函数

xml 复制代码
<if test="_parameter.containsKey('keyword')">  <!-- 判断 Map 参数是否包含 key -->
  AND title LIKE CONCAT('%', #{keyword}, '%')
</if>

六、总结

  • 核心原则:确保 OGNL 表达式中的类型与 Java 对象一致。
  • 关键注意:字符串用单引号、数字无需引号、优先判空。
  • 灵活组合 :通过逻辑运算符 (and/or)、方法调用 (toString())、工具类等实现复杂逻辑。

通过合理使用 <if> 标签,可以轻松实现动态 SQL 的灵活拼接!

相关推荐
没有bug.的程序员7 小时前
JAVA面试宝典 - 《MyBatis 进阶:插件开发与二级缓存》
java·面试·mybatis
码不停蹄的玄黓14 小时前
深入理解MyBatis延迟加载:原理、配置与实战优化
java·mybatis·延迟加载
hello早上好18 小时前
JPA、缓存、数据源与连接池、简介
java·mybatis
梁辰兴19 小时前
企业培训笔记:宠物信息管理--实现宠物信息分页查询
笔记·elementui·mybatis·vue3·springboot·宠物
鲁子狄21 小时前
[笔记] 动态 SQL 查询技术解析:构建灵活高效的企业级数据访问层
java·spring boot·笔记·sql·mysql·mybatis
拼搏@1 天前
第十六天,7月10日,八股
java·mybatis
军军君011 天前
基于Springboot+UniApp+Ai实现模拟面试小工具四:后端项目基础框架搭建下
spring boot·spring·面试·elementui·typescript·uni-app·mybatis
超级小忍2 天前
在 Spring Boot 中使用 MyBatis 的 XML 文件编写 SQL 语句详解
xml·spring boot·mybatis
程序猿小D2 天前
[附源码+数据库+毕业论文+答辩PPT+部署教程+配套软件]基于SpringBoot+MyBatis+MySQL+Maven+Vue实现的交流互动管理系统
spring boot·mysql·vue·mybatis·毕业论文·答辩ppt·交流互动
24kHT2 天前
xml映射文件的方式操作mybatis
xml·mybatis