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

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.xdnf.cn/news/12471.html

相关文章:

  • 浏览器后台服务 vs 在线教育:QPS、并发模型与架构剖析
  • 如何对Video视频进行SEO优化?
  • Ubuntu Cursor升级成v1.0
  • 大数据量高实时性场景下订单生成的优化方案
  • 2025主流智能体Agent终极指南:Manus、OpenManus、MetaGPT、AutoGPT与CrewAI深度横评
  • windows上的visual studio2022的项目使用jenkins自动打包
  • Visual Studio问题记录
  • 准确--k8s cgroup问题排查
  • 【学习笔记】构造函数+重载相关
  • 生成 Git SSH 证书
  • Linux 常用命令语法总结
  • 【Go语言基础【9】】字符串格式化与输入处理
  • 神经网络-Day46
  • react 常见的闭包陷阱深入解析
  • 循环神经网络(RNN)
  • 麒麟系统使用-进行.NET开发
  • 1.3 编译程序:单个C文件和多个C文件
  • Python打卡DAY46
  • JSON解析崩溃原因及解决方案
  • linux系统dd 工具介绍
  • 理解JavaScript中map和parseInt的陷阱:一个常见的面试题解析
  • win10+TensorRT+OpenCV+Qt+YOLOV8模型部署教程
  • 2025年多层PCB技术发展与厂商实践指南
  • C++总复习
  • 嵌入式学习笔记 - FreeRTOS 信号量以及释放函数
  • cursor和windsurf使用体验对比
  • NLP学习路线图(二十八):BERT及其变体
  • AI for Science(AI4S)的现在与未来,科学智能如何赋能科学研究?
  • Python训练营打卡Day46(2025.6.6)
  • leetcode 2434. 使用机器人打印字典序最小的字符串 中等