MyBatis之OGNL的应用

MyBatis中使用的OGNL表达式可参考:这里

MyBatis中可以使用OGNL的地方有两处:

  • 动态SQL表达式中
  • ${param}参数中

上面这两处地方在MyBatis中处理的时候都是使用OGNL处理的。

下面通过举例来说明这两种情况的用法。

1.动态SQL表达式中

例一,MySql like 查询:

<select id="xxx" ...>
    select id,name,... from country
    <where>
        <if test="name != null and name != ''">
            name like concat('%', #{name}, '%')
        </if>
    </where>
</select>

上面代码中test的值会使用OGNL计算结果。

例二,通用 like 查询:

<select id="xxx" ...>
    select id,name,... from country
    <bind name="nameLike" value="'%' + name + '%'"/>
    <where>
        <if test="name != null and name != ''">
            name like #{nameLike}
        </if>
    </where>
</select>

这里<bind>value值会使用OGNL计算。

注:对<bind参数的调用可以通过#{}或 ${} 方式获取,#{}可以防止注入。

在通用Mapper中支持一种UUID的主键,在通用Mapper中的实现就是使用了<bind>标签,这个标签调用了一个静态方法,大概方法如下:

<bind name="username_bind" 
      value='@java.util.UUID@randomUUID().toString().replace("-", "")' />
  • 1
  • 2

这种方式虽然能自动调用静态方法,但是没法回写对应的属性值,因此使用时需要注意。

2.${param}参数中

上面like的例子中使用下面这种方式最简单

<select id="xxx" ...>
    select id,name,... from country
    <where>
        <if test="name != null and name != ''">
            name like '${'%' + name + '%'}'
        </if>
    </where>
</select>

这里注意写的是${'%' + name + '%'},而不是%${name}%,这两种方式的结果一样,但是处理过程不一样。

在MyBatis中处理${}的时候,只是使用OGNL计算这个结果值,然后替换SQL中对应的${xxx},OGNL处理的只是${这里的表达式}

这里表达式可以是OGNL支持的所有表达式,可以写的很复杂,可以调用静态方法返回值,也可以调用静态的属性值。

mybatis中使用ognl的扩展,实现判断传入的字段:

xxx.map.xml里面的用法:

	<select id="getAll" parameterType="java.util.Map" resultMap="TaskEntity">
		SELECT task.*,run.subject subject,run.processName processName
		FROM ACT_RU_TASK task left join BPM_PRO_RUN run
		on task.PROC_INST_ID_=run.actInstId
		where 1=1
		<if test="@Ognl@isNotEmpty(name)"> AND task.name_ LIKE #{name} </if>
		<if test="@Ognl@isNotEmpty(subject)"> AND run.subject LIKE #{subject} </if>
		<if test="@Ognl@isNotEmpty(processName)"> AND run.processName LIKE #{processName} </if>
		<if test="@Ognl@isEmpty(orderField)">
			order by task.CREATE_TIME_ desc
		</if>
		<if test="@Ognl@isNotEmpty(orderField)">
			order by ${orderField} ${orderSeq}
		</if>		
	</select>

OGNL.java (Ognl.java 必须放在class目录,也就是没有包名)

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

import com.hotent.core.util.BeanUtils;

public class Ognl {
    public Ognl() {
    }

    public static boolean isEmpty(Object o) throws IllegalArgumentException {
        return BeanUtils.isEmpty(o);
    }

    public static boolean isNotEmpty(Object o) {
        return !isEmpty(o);
    }

    public static boolean isNotEmpty(Long o) {
        return !isEmpty(o);
    }

    public static boolean isNumber(Object o) {
        return BeanUtils.isNumber(o);
    }
}

参考文档:MyBatis中的OGNL教程

深入了解MyBatis参数


  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值