mybatis的if标签不生效的问题原因

实际需求

<if test="computationRule == '1'">
  FROM app_sz_bbb a
</if>
<if test="computationRule == '2'">
  FROM app_ccc a
</if>

这种情况不生效,

原因:mybatis是用OGNL表达式来解析的,在OGNL的表达式中,'0'会被解析成字符,java是强类型的,char 和 一个string 会导致不等,所以if标签中的sql不会被解析。

先说怎么解决

三种:

加 .toString()

<if test="computationRule == '1'.toString()">
  FROM app_sz_bbb a
</if>
<if test="computationRule == '2'.toString()">
  FROM app_ccc a
</if>

choose when 标签代替

<choose>
   <when test="computationRule == '1'">
   FROM app_sz_bbb a
   </when>
   <otherwise>
     FROM app_sz_bbb a
   </otherwise>
 </choose>

单引号 换成双引号

<if test='computationRule == "1"'>
  FROM app_sz_bbb a
</if>
<if test='computationRule == "2"'>
  FROM app_ccc a
</if>

MyBatis 中if 标签 判断字符串不生效

异常sql 的mapper 文件:

<if test="isBound != null and isBound !='' and isBound == '1'">
  and box_sid is not null 
</if>
<if test="isBound != null and isBound !='' and isBound == '2'">
  and box_sid is null 
</if>

正确sql 的mapper 文件

<if test="isBound != null and isBound !='' and isBound == '1'.toString()">
  and box_sid is not null 
</if>
<if test="isBound != null and isBound !='' and isBound == '2'.toString()">
  and box_sid is null 
 </if>
  • 3
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值