服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - mybatis if标签判断不生效的解决方法

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

2021-08-05 11:41暮霭层层楚天阔 Java教程

这篇文章主要介绍了mybatis if标签判断不生效的解决方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

实际需求

?
1
2
3
4
5
6
<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()

?
1
2
3
4
5
6
<if test="computationRule == '1'.toString()">
  FROM app_sz_bbb a
</if>
<if test="computationRule == '2'.toString()">
  FROM app_ccc a
</if>

choose when 标签代替

?
1
2
3
4
5
6
7
8
<choose>
   <when test="computationRule == '1'">
   FROM app_sz_bbb a
   </when>
   <otherwise>
     FROM app_sz_bbb a
   </otherwise>
 </choose>

单引号 换成双引号

?
1
2
3
4
5
6
<if test='computationRule == "1"'>
  FROM app_sz_bbb a
</if>
<if test='computationRule == "2"'>
  FROM app_ccc a
</if>

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

 

异常sql 的mapper 文件:

?
1
2
3
4
5
6
<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 文件

?
1
2
3
4
5
6
<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标签判断不生效内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/qq_43167632/article/details/110189333

延伸 · 阅读

精彩推荐