【MyBatis】Mybatis中EL表定式总结

【MyBatis】Mybatis中EL表定式总结

Mybatis是使用OGNL表达式来解析的,关于OGNL详细信息请查看官网:https://commons.apache.org/proper/commons-ognl/language-guide.html

一、字符串相关

1)null 判断

<if test="bookname == null">
	and book_name is null
</if>
<!-- 或者 -->
<if test="bookname != null">
	and book_name is not null
</if>

2)判断字符串不等于空串

<if test="bookname == '' ">
	and book_name = ''
</if>
<!-- 或者 -->
<if test="bookname != '' ">
	and book_name = ''
</if>

3)单字符问题

<!-- 错误代码 -->
<if test="bookname == '1' ">
	and book_name = '1'
</if>

上面代码,在 bookname等于1,不成立,这是由于OGNL的表达式中,'1' 会被解析成字符,java是强类型的,char 和 一个string 不是同一种类型。

正确写法如下:

<!-- 第一种-->
<if test="bookname == '1'.toString() ">
	and book_name = '1'
</if>
<!-- 第二种-->
<if test='bookname == "1" ' >
	and book_name = '1'
</if>

4)自身方法调用

EL表示式可以直接调用自身方法

<if test="bookname != null and bookname.indexOf('三') > -1 ">
	and book_name = #{bookname}
</if>
<if test="bookname != null and bookname.endsWith('记') ">
	and book_name = #{bookname}
</if>
<if test="bookname != null and bookname.startsWith('水') ">
	and book_name = #{bookname}
</if>

5)字符串转数组

EL字符串,调用split方法,转换成数组。

<if test="booknames != null and booknames.length() gt 0 ">
    <foreach item="item" index="index" collection="booknames.split(',')" open=" AND book_name in (" separator="," close=")">
        #{item}
    </foreach>
</if>
<if test="booknames != null and booknames.length() lt 10 ">
    <foreach item="item" index="index" collection="booknames.split(',')" open=" AND book_type in (" separator="," close=")">
        #{item}
    </foreach>
</if>

注:

在字符串比较中:

eq 等价于 ==
neq 等价于 !=

二、数字类型

1)普通判断

<if test = "id !=null and id gt 0">
	and id = #{id}
</if>
<if test = "id !=null and id lt 0">
	and id = #{id}
</if>

2)0值问题

<!-- 错误-->
<if test = "id !=null and id != ''">
	and id = 0
</if>

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 as true, zero as false;
  • If the object is a Character, its boolean value is true if and only if its char value is non-zero;
  • Otherwise, its boolean value is true if and only if it is non-null.

在数字类型与非数字类型比较时,数值0或者浮点数字0.00被解析成false,同理,空串''也被解析成false。

具体用例如下:id等于0的情况下:

<!--id =0 时: true -->
<if test = "id !=null and id lt 1">
	and id = 0
</if>
<!-- id =0 时:false -->
<if test = "id !=null and id == true">
    and id = 2
</if>
<!-- id =0 时:true -->
<if test = "id !=null and id == false">
    and id = 3
</if>
<!-- id =0 时:true -->
<if test = "id !=null and id == ''">
    and id = 4
</if>
<!--id =0 时: true -->
<if test = "id !=null and false == ''">
    and id = 5
</if>
表达式 表达式
gt 等价于 >
gte 等价于 >=
lt 等价于 <
lte 等价于 <=

三、调用静态函数

EL表达式可以直接调用Java中静态类,表达式:@class@method

	<select id ="callingStaticMethodsTest" resultType ="Book">
		select * from book
		<where>
			<if test = "@java.lang.Math@max(id1,id2) lt 10">
				and 1 = 1
			</if>
			<if test = "@java.lang.Math@min(id1,id2) gt 3">
				and 1 = 2
			</if>
		</where>
	</select>

代码地址:https://gitee.com/firefish/mybatis_study

posted @   二月无雨  阅读(574)  评论(0编辑  收藏  举报
(评论功能已被禁用)
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示