mybatis if标签判断不生效的解决方法
时间:2021-07-03 08:26:59|栏目:JAVA代码|点击: 次
实际需求
<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>


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




