mybatis if标签判断不生效的解决方法
更新时间:2021年02月07日 14:18:37 作者:暮霭层层楚天阔
这篇文章主要介绍了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>
到此这篇关于mybatis if标签判断不生效的解决方法的文章就介绍到这了,更多相关mybatis if标签判断不生效内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Spring Boot循环依赖原理、解决方案与最佳实践(全解析)
循环依赖指两个或多个Bean相互直接或间接引用,形成闭环依赖关系,这篇文章主要介绍了Spring Boot循环依赖原理、解决方案与最佳实践(全解析),需要的朋友可以参考下2025-04-04
Spring Boot中Elasticsearch的连接配置原理与使用详解
在Spring Boot中,我们可以通过Elasticsearch实现对数据的搜索和分析,本文将介绍Spring Boot中Elasticsearch的连接配置、原理和使用方法,感兴趣的可以了解一下2023-09-09
Linux下用java -jar运行可执行jar包的方法教程
这篇文章主要给大家介绍了在Linux下用java -jar运行可执行jar包的方法教程,文中介绍的非常详细,相信对大家的工作或者学习具有一定的参考学习价值,需要的朋友们下面来一起看看吧。2017-05-05


最新评论