mybatis 动态sql的算术运算问题

mybatis的动态sql在xml文件中如下:

<if test="eachBook==0">
    count( loan.ITEM_ID ) AS borrowNum-- 借阅次数(册)
</if>
<if test="eachBook==1">
    count( DISTINCT item.RECORD_ID ) AS borrowNum -- 借阅次数(种)
</if>

如上,我要判断 eachBook 字段时 0还是 1,请问该怎么写呢,看了mybatis官方文档也没说.

希望知道的兄弟告知一下,谢谢了.

阅读 4.3k
2 个回答

MyBatis是使用OGNL表达式进行解析 所以变量要判断等于某个值使用如下:

<if test='optionType == "1" '>

</if>

或者

<if test="optionType == '1'.toString() ">

</if>

或者

<if test="optionType == &quot;1&quot; ">

</if>

为什么不用 case when 语句块


select sno,sname,age,saddress,
(case sex 
    when '0' then '女' 
    when '1' then '男' 
 else '未知' end) as 性别 

from stud;
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题