【MyBatis】Mybatis中EL表定式总结
【MyBatis】Mybatis中EL表定式总结
Mybatis是使用OGNL表达式来解析的,关于OGNL详细信息请查看官网:https://commons.apache.org/proper/commons-ognl/language-guide.html
一、字符串相关
1)null 判断
and book_name is null
and book_name is not null
2)判断字符串不等于空串
and book_name = ''
and book_name = ''
3)单字符问题
and book_name = '1'
上面代码,在 bookname等于1,不成立,这是由于OGNL的表达式中,'1' 会被解析成字符,java是强类型的,char 和 一个string 不是同一种类型。
正确写法如下:
and book_name = '1'
and book_name = '1'
4)自身方法调用
EL表示式可以直接调用自身方法
and book_name = #{bookname}
and book_name = #{bookname}
and book_name = #{bookname}
5)字符串转数组
EL字符串,调用split方法,转换成数组。
#{item}
#{item}
注:
在字符串比较中:
| eq | 等价于 | == |
|---|---|---|
| neq | 等价于 | != |
二、数字类型
1)普通判断
and id = #{id}
and id = #{id}
2)0值问题
and id = 0
Interpreting Objects as Booleans
Any object can be used where a boolean is required. OGNL interprets objects as booleans like this:
- If the object is a
Boolean, its value is extracted and returned;- If the object is a
Number, its double-precision floating-point value is compared with zero; non-zero is treated astrue, zero asfalse;- If the object is a
Character, its boolean value istrueif and only if its char value is non-zero;- Otherwise, its boolean value is
trueif and only if it is non-null.
在数字类型与非数字类型比较时,数值0或者浮点数字0.00被解析成false,同理,空串''也被解析成false。
具体用例如下:id等于0的情况下:
and id = 0
and id = 2
and id = 3
and id = 4
and id = 5
| 表达式 | 表达式 | |
|---|---|---|
| gt | 等价于 | > |
| gte | 等价于 | >= |
| lt | 等价于 | < |
| lte | 等价于 | <= |
三、调用静态函数
EL表达式可以直接调用Java中静态类,表达式:@class@method
代码地址:https://gitee.com/firefish/mybatis_study