mybatis 使用if 判断字符串
mybatis 动态sql 中if判断使用的ognl表达式,现在分3中情况说明并验证。
一、情况说明:
传入的itemCode为参数传入mybatis
<if test='itemCode != null and itemCode !="" and itemCode =="xxx" '>
1、 单个字符的数字型字符串
例如:传入参数 itemCode=“1”
以下写法不符合判断
<if test='itemCode != null and itemCode !="" and itemCode =="1" '>
1
如果想让判断符合条件,可以使用一下两种写法
<if test="itemCode != null and itemCode !='' and itemCode =='1'.toString()">
或
<if test='itemCode != null and itemCode !="" and itemCode =="1" '>
2、单个字符的非数字型字符串
例如:传入参数 itemCode=“z”
<if test="itemCode != null and itemCode !='' and itemCode =='z'">
1
会报错 NumberFormatException,如果想让判断符合条件如下写法。
<if test="itemCode != null and itemCode !='' and itemCode =='z'.toString()">
或
<if test='itemCode != null and itemCode !="" and itemCode =="z" '>
3、不是单个字符的字符串
例如:传入参数 itemCode=“张三”
不用.toString()或单引号变双引号就会符合条件
<if test="itemCode != null and itemCode !='' and itemCode =='张三'">
————————————————
版权声明:本文为CSDN博主「曾令胜」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_21190847/article/details/105642356
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· winform 绘制太阳,地球,月球 运作规律
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· AI与.NET技术实操系列(五):向量存储与相似性搜索在 .NET 中的实现
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人