Mybatis中if标签为何判断条件不生效?

2026-04-19 20:021阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计393个文字,预计阅读时间需要2分钟。

Mybatis中if标签为何判断条件不生效?

实际需求中,如果测试条件为computationRule='1'则从app_sz_bbb获取数据,如果测试条件为computationRule='2'则从app_ccc获取数据。这种情况下,不会生效,原因是Mybatis使用OGNL表达式解析,在OGNL表达式中,'0'会被解析为字符。

实际需求

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标签判断不生效内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!

本文共计393个文字,预计阅读时间需要2分钟。

Mybatis中if标签为何判断条件不生效?

实际需求中,如果测试条件为computationRule='1'则从app_sz_bbb获取数据,如果测试条件为computationRule='2'则从app_ccc获取数据。这种情况下,不会生效,原因是Mybatis使用OGNL表达式解析,在OGNL表达式中,'0'会被解析为字符。

实际需求

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标签判断不生效内容请搜索易盾网络以前的文章或继续浏览下面的相关文章希望大家以后多多支持易盾网络!