问题-01

Mybatis比较失效问题

1、问题复现

whetherPromoterNull是字符串类型,0使用单引号包裹,进行比较时发现不起作用

java 复制代码
    <if test="whetherPromoterNull != null and whetherPromoterNull.trim() == '0'"> and sui.share_user_id is not null</if>

2、原因

在MyBatis的条件判断中,使用单引号或双引号来括起字符串值都是可以的。但是在比较字符串类型的数字时,它们会产生不同的效果

  • 不加引号:比较数值时正常。若whetherPromoterNull是数值类型,比较数值时正常
  • 单引号:比较字符串时正常,比较数值时不正常。若whetherPromoterNull是数值类型,比较数值时不正常
  • 双引号:比较字符串时正常,比较数值时正常。若whetherPromoterNull是数值类型,比较数值时正常

3、解决方法

java 复制代码
//将数值类型通过toString方法变成字符串类型
<if test="whetherPromoterNull != null and whetherPromoterNull.trim() == '0'.toString()"> and sui.share_user_id is not null</if>

//使用双引号将数值类型变成字符串类型
<if test='whetherPromoterNull != null and whetherPromoterNull.trim() == "0"'> and sui.share_user_id is not null</if>

//不加引号
<if test="whetherPromoterNull != null and whetherPromoterNull.trim() == 0"> and sui.share_user_id is not null</if>
相关推荐
Pniubi16 小时前
怎么排查慢SQL,怎么解决?
java·数据库·sql
薛之谦_17 小时前
Java 全栈知识体系:从 IDEA 高效开发到 JVM、并发与 Stream 实战
java·jvm·intellij-idea
文丑颜不良啊21 小时前
LeetCode2761~2780题目解析
java·leetcode
IT枫斗者枫哥21 小时前
子线程断言失败,JUnit 为什么还显示通过?
java·后端
考虑考虑1 天前
JDK27中的Set.ofLazy
java·后端·java ee
文丑颜不良啊1 天前
LeetCode2841~2860题目解析
java·leetcode
小南家的青蛙1 天前
Tomcat与Jetty深度对比
java·tomcat·jetty
乌暮1 天前
Spring AOP 与 Spring 事务:用大白话彻底理解原理与实践
java·数据库·spring boot·后端·sql·学习·spring
YDS8291 天前
Small Spring IOC篇:容器事件及其监听器
java·spring
金銀銅鐵1 天前
带您看懂字节码指令(三):一行加法里有什么
java·jvm