Mybatis常用的OGNL表达式

本文深入解析了Mybatis中OGNL表达式的使用方法,包括逻辑运算、比较运算、算术运算、对象属性访问、集合操作及静态方法调用等,为动态SQL的灵活应用提供了详实的指导。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在Mybatis的动态SQL和${}形式的参数中都用到了OGNL表达式。Mybatis常用的OGNL表达式如下

1、e1 or e2:或

<if test="userEmail != null or userEmail == '1'">
</if>

2、e1 and e2:且

<if test="userEmail != null and userEmail != ''">
</if>

3、e1 == e2 或e1 eq e2:相等

<if test="userEmail == null and userEmail == ''">
</if>

4、e1 != e2 或 e1 neq e2:不等

<if test="userEmail != null and userEmail != ''">
</if>

5、e1 lt e2:小于

<if test="age lt 10">
		#{userEmail,jdbcType=VARCHAR},
</if>

6、e1 lte e2:小于等于
7、e1 gt e2:大于
8、e1 gte e2:大于等于
9、 e1 + e2(加),e1 - e2(减),e1 * e2(乘),e1/e2(除),e1%e2(余)
10、!e或not e:非,取反
11、e.method(args):调用对象方法

<if test="list != null and list.size() > 0 ">
		#{userEmail,jdbcType=VARCHAR},
</if>

12、e.property:对象属性值

<!-- 多接口参数的查询方法(@Param + javaBean方式) -->
  <select id="selectByUserIdAndEnabledUseBean" resultMap="BaseResultMap">
    select r.id, r.role_name, r.enabled, r.create_by, r.create_time, 
    u.user_name as "user.userName", u.user_email as "user.userEmail"
    from sys_user u 
    inner join sys_user_role ur on u.id = ur.user_id 
    inner join sys_role r on ur.role_id = r.id 
    where u.id = #{user.id} and r.enabled = #{role.enabled}
</select>

13、e1[e2]:按索引取值(List、数组和map)
14、@class@method(args):调用类的静态方法

<bind name="name" value="@ex.mybatis.rbac.mapper.UserMaperTest@setName()"/>

15、@class@field:调用类的静态字段值

<bind name="name" value="@ex.mybatis.rbac.mapper.UserMaperTest@NAME"/>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值