实际需求

<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>

到此这篇关于mybatis if标签判断不生效的解决方法的文章就介绍到这了,更多相关mybatis if标签判断不生效内容请搜索www.887551.com以前的文章或继续浏览下面的相关文章希望大家以后多多支持www.887551.com!