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

posted @ 2022-09-30 11:32  海逸庭  阅读(5496)  评论(0编辑  收藏  举报