mybatis中如何使用OGNL表达式

在使用mybatis时经常使用到OGNL表达式。特别是在使用动态sql查询时。
下面看下一些常用的OGNL(详情请点这里–官网介绍):
表达式e1,e2

  • e1 or e2
  • e1 and e2
  • e1 == e2, e1 eq e2
  • e1 != e2, e1 neq e2
  • e1 < e2, e1 lt e2
  • e1 <= e2, e1 lte e2
  • e1 > e2, e1 gt e2
  • e1 >= e2, e1 gte e2
  • e1 in e2
  • e1 not in e2
  • ! e, not e,e instanceof class
  • e.method(args)调用对象方法
  • e.property调用对象属性
  • e1[ e2 ] 按索引取值,list,数组和map
  • @class@method(args) 调用静态方法
  • @class@field 调用静态常量

这里举个简单例子说明下@class@method(args)和@class@field的用法:
sql如下:

  1. 静态方法@class@method(args)
<select id="select" parameterType="com.xingguo.springboot.model.User"  resultType="com.xingguo.springboot.model.User">
        select username,password from t_user 
        where 1=1
        <choose>
        <!--调用静态方法 -->
			<when test="@com.xingguo.springboot.model.TestConstant@checkStatus(state,0)">
				<!--调用静态常量 -->
				AND state = ${@com.xingguo.springboot.model.TestConstant@STATUS_0}
			</when>
			<otherwise>
				AND state = ${@com.xingguo.springboot.model.TestConstant@STATUS_1}
			</otherwise>
		</choose>
    </select>
  1. 静态常量@class@field
package com.xingguo.springboot.model;

public class TestConstant {
	//静态常量
	public static final int STATUS_0 = 0;
	public static final int STATUS_1 = 1;
	
	//静态方法
	public static Boolean checkStatus(int sourceStatus,int targetStatus){
		return sourceStatus == targetStatus;
	}
	public static final List<Integer> STATES= Lists.newArrayList(STATUS_0, 
    		STATUS_1);
}

如果是参数是List的情况:

<select id="selectCount" resultType="int">
    select  COUNT(1) from t_user 
    where state in 
    <foreach collection="@com.xingguo.springboot.model.TestConstant@STATES" 
    		item="item" open="(" close=")" separator=",">
    		#{item}
    	</foreach>
</select>

其他的相对简单就不再举例子。

在mysql中使用mybatis进行模糊查询的方式就有下面几种:

  1. 使用OGNL
<select id="select" parameterType="com.xingguo.springboot.model.User"  resultType="com.xingguo.springboot.model.User">
        select username,password from t_user 
        where 1=1
        <!-- OGNL处理${这里的表达式}的结果值 -->
		<if test="username != null and username != ''">
          and username like '${'%' + username + '%'}'
        </if>
    </select>
  1. 使用一般的方式
<select id="select" parameterType="com.xingguo.springboot.model.User"  resultType="com.xingguo.springboot.model.User">
        select username,password from t_user 
       	<where>
	       	<!-- 使用concat -->
			<if test="username != null and username != ''">
	          username like concat('%', #{username}, '%')
	        </if>
       	</where>
        
    </select>
  1. 使用bind
<select id="select" parameterType="com.xingguo.springboot.model.User"  resultType="com.xingguo.springboot.model.User">
        select username,password from t_user 
        <bind name="nameLike" value="'%' + username + '%'"/>
       	<where>
			<if test="username != null and username != ''">
	          username like #{nameLike}
	        </if>
       	</where>
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
OGNL(Object-Graph Navigation Language,对象图导航语言)是一个强大的表达式语言,可以用于Java的各种应用程序,包括Struts、JavaServer Faces、JavaServer Pages等。在MybatisOGNL表达式可以用于Mapper XML文件的各种标签,例如`<if>`、`<where>`、`<set>`、`<foreach>`等标签OGNL表达式可以用于获取Java对象的属性值、调用Java对象的方法、进行算术运算、比较运算、逻辑运算等。例如: ```xml <select id="selectByCondition" resultType="com.example.User"> select * from user where name like #{keyword} and age >= #{minAge} <if test="maxAge != null"> and age <= #{maxAge} </if> </select> ``` 在这个例子,`#{keyword}`、`#{minAge}`、`#{maxAge}`都是OGNL表达式,表示获取Java对象的属性值。例如,如果传入的参数对象是一个`User`对象,那么`#{keyword}`可以表示`user.getKeyword()`方法的返回值,`#{minAge}`可以表示`user.getMinAge()`方法的返回值。 在OGNL表达式,还可以进行算术运算、比较运算、逻辑运算等。例如,`age >= #{minAge}`表示将`age`和`#{minAge}`进行比较,判断`age`是否大于等于`#{minAge}`;`age <= #{maxAge}`表示将`age`和`#{maxAge}`进行比较,判断`age`是否小于等于`#{maxAge}`。 除了基本的运算符和表达式OGNL还提供了丰富的函数和操作符,例如`in`操作符、`not`操作符、`contains`函数、`size`函数等。这些函数和操作符可以方便地进行集合操作、字符串操作等。在使用OGNL表达式时,需要注意语法的正确性和安全性,以避免可能的安全漏洞。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值