mybatis if标签判断不生效 mybatis if标签判断不生效的解决办法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

mybatis if标签判断不生效 mybatis if标签判断不生效的解决办法

暮霭层层楚天阔   2021-02-07 我要评论
想了解mybatis if标签判断不生效的解决办法的相关内容吗,暮霭层层楚天阔在本文为您仔细讲解mybatis if标签判断不生效的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:mybatis,if标签不生效,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>

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们