当前位置: 首页 > article >正文

mybatis的if判断==‘1‘不生效,改成‘1‘.toString()才生效的原因

mybatis的xml文件中的if判断==‘1’不生效,改成’1’.toString()才生效

Mapper接口传入的参数

    List<Table> queryList(@Param("state") String state);

xml内容

<where><if test="state != null and state == '1'">AND EXISTS(select 1 from t_space.base_outlay where outlay_card_no = t1.outlay_card_no)</if>AND EXISTS(select 2 from t_space.base_outlay where outlay_card_no = t1.outlay_card_no)
</where>

select 1的sql一直没进到,能确定state就是1,改成<if test="state != null and state == '1'">或者<if test='state != null and state == "1">问题成功解决,网上大量博客未说明原因,如:参考1|参考2
多次询问ai给出如下原因有点说服力:

总结的就是:

在 MyBatis 的 OGNL 或 SpEL 表达式引擎中,写法 == ‘1’ 中的 ‘1’ 被解析为 Character 类型(char),而不是字符串!
而你传入的参数 state是 String 类型,所以比较时会出现 类型不一致导致判断失败:
String(“1”) == Character(‘1’) ❌ 不成立

以下是灵码给的原因:

明白了,你发现:<if test="state != null and state == '1'"> ❌ 不生效
<if test="state != null and state == '1'.toString()"> ✅ 生效了
这说明你在 MyBatis 的 EL 表达式中遇到了类型匹配问题。我们来深入分析原因。🧪 问题本质:'1' 和 '1'.toString() 在 MyBatis EL 中的类型不同
在 Java 中:java
'1'        // 是 char 类型(注意是单引号)
"1"        // 是 String 类型(双引号)
"1".toString() == "1"  // 成立
但在 MyBatis 的 OGNL 或 SpEL 表达式引擎中,写法 == '1' 中的 '1' 被解析为 Character 类型(char),而不是字符串!而你传入的参数 state 是 String 类型,所以比较时会出现 类型不一致导致判断失败:java
String("1") == Character('1') ❌ 不成立
当你写成:xml
state == '1'.toString()
此时 '1'.toString() 返回的是字符串 "1",两边就都是 String 类型了,比较就能成功。✅ 正确写法推荐
为了避免这种陷阱,建议统一使用双引号表示字符串:xml
<if test="state != null and state == '1'">
应该改为:xml
<if test="state != null and state == '1'.toString()">
或者更清晰地写成:xml
<if test="state != null and state == '1'.toString().intern()">
但最推荐的是:xml
<if test="state != null and outlayAddAble.equals('1')">
⚠️ 注意:在 MyBatis 的表达式中,使用 .equals() 比 == 更安全,因为 == 判断的是引用地址,.equals() 才是值比较。✅ 最佳实践总结
写法	是否推荐	原因
state == '1'	❌ 不推荐	'1' 是 char 类型,和 String 不匹配
state == '1'.toString()	✅ 推荐	强制转成 String 类型比较
outlayAddAble.equals('1')	✅✅ 强烈推荐	使用 .equals() 进行值比较,语义正确
outlayAddAble.toString() == '1'	⚠️ 避免	== 是引用比较,容易出错
outlayAddAble.toString().equals('1')	✅ 推荐	安全通用
✅ 推荐最终写法
xml
<if test="state != null and outlayAddAble.equals('1')">AND EXISTS(select 1 from t_space.base_outlay where outlay_card_no = t1.outlay_card_no)
</if>
这样可以确保:参数是字符串;
比较是值比较;
不受底层表达式引擎对 '1' 解析为 char 的影响。
如果你还有疑问或需要进一步验证,可以把生成的 SQL 日志贴出来一起分析。
http://www.lryc.cn/news/2404933.html

相关文章:

  • 【试卷篇】Spring面试试卷题
  • 通过阿里云 DashScope API 调用通义千问
  • Go 中 map 的双值检测写法详解
  • 大故障:阿里云核心域名爆炸了
  • 解决Zotero翻译插件Zotero PDF Translate无法正常翻译
  • 【Latex】Windows/Ubuntu 绘制 eps 矢量图通用方法(drawio),支持插入 Latex 数学公式
  • Vue---vue使用AOS(滚动动画)库
  • 十(1). 强制类型转换
  • 从npm库 Vue 组件到独立SDK:打包与 CDN 引入的最佳实践
  • rknn优化教程(一)
  • uniapp Vue2 获取电量的独家方法:绕过官方插件限制
  • 【统计方法】树模型,ensemble,bagging, boosting
  • 【选配电脑】CPU核显工作机控制预算5000
  • Mysql 插入中文乱码
  • UserAgent包名识别工具
  • 96.如何使用C#实现串口发送? C#例子
  • 【工具使用】STM32CubeMX-FreeRTOS操作系统-信号标志、互斥锁、信号量篇
  • [P2P]并发模式
  • Cloudflare 免费域名邮箱 支持 Catch-all 无限别名收件
  • 大数据Spark(六十一):Spark基于Standalone提交任务流程
  • 学习记录:DAY32
  • next,react封装axios,http请求
  • 元图CAD:一键解锁PDF转CAD,OCR技术赋能高效转换
  • Android 平台RTSP/RTMP播放器SDK接入说明
  • Nodejs工程化实践:构建高性能前后端交互系统
  • STM32什么是寄存器
  • Linux 的 find 命令使用指南
  • 第六个微信小程序:教师工具集
  • 记录一个用了很久的git提交到github和gitee比较方便的方法
  • Qt Qml模块功能及功能解析