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

作者:暮霭层层楚天阔 时间:2023-11-28 12:30:49 

实际需求


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

来源:https://blog.csdn.net/qq_43167632/article/details/110189333

标签:mybatis,if标签
0
投稿

猜你喜欢

  • dubbo入门指南及demo实例详解

    2023-08-24 04:49:07
  • C#图片压缩的实现方法

    2022-07-01 21:26:12
  • Kotlin原理详析之拓展函数

    2023-04-10 19:54:08
  • Springboot集成RabbitMQ死信队列的实现

    2022-08-24 13:10:54
  • 新手学习微服务SpringCloud项目架构搭建方法

    2022-01-29 04:00:17
  • maven springboot如何将jar包打包到指定目录

    2022-12-09 00:40:25
  • Spring Task定时任务每天零点执行一次的操作

    2021-09-12 10:59:44
  • 介绍C# 泛型类在使用中约束

    2023-06-23 21:39:40
  • Spring异常捕获且回滚事务解决方案

    2023-04-25 18:32:41
  • Java中线程安全问题

    2021-12-02 05:51:59
  • 教你如何编写简单的网络爬虫

    2023-06-01 08:58:30
  • Android nativePollOnce函数解析

    2022-05-29 01:51:26
  • Android中Notification用法实例总结

    2023-03-16 23:05:38
  • Spring Cloud Gateway去掉url前缀

    2023-06-05 00:54:38
  • Spring Boot日志技术logback原理及配置解析

    2022-09-14 10:51:57
  • 深入探讨JAVA中的异常与错误处理

    2023-06-11 00:30:24
  • 获取wince mac地址与IP地址解决方案

    2022-01-21 02:04:19
  • 浅谈图片上传利用request.getInputStream()获取文件流时遇到的问题

    2023-10-18 10:36:43
  • Android以对话框形式制作数字软键盘示例

    2022-12-07 07:37:03
  • C#数据结构之堆栈(Stack)实例详解

    2022-12-17 21:25:37
  • asp之家 软件编程 m.aspxhome.com