MyBatis支持的OGNL语法
文章目录
Mybatis支持OGNL的语法
OgnlCache
用于解析静态方法
org.apache.ibatis.scripting.xmltags.TextSqlNode.BindingTokenParser#handleToken
org.apache.ibatis.scripting.xmltags.OgnlCache#getValue
org.apache.ibatis.scripting.xmltags.OgnlCache#parseExpression解析表达式
org.apache.ibatis.ognl.Ognl#parseExpression
org.apache.ibatis.ognl.OgnlParser#staticReference
org.apache.ibatis.ognl.OgnlParser#staticMethodCall
org.apache.ibatis.ognl.OgnlRuntime#callStaticMethod
在sql映射语句中可以支持引入以下几种方式
示例SQL
<select id="getUserById" resultMap="BaseResultMap">
select * from user
<if test="id != null">
<where>
name = #{name}
and id =${id}
and id = ${user.id}
and id = ${@@abs(-12345678)}
and id = ${@Integer@parseInt("654")}
and id='${@cn.followtry.mybatis.bean.User@name()}'
and id='${@org.apache.commons.lang3.math.NumberUtils@isNumber("sdfghjk")}'
and id='${new cn.followtry.mybatis.bean.User()}'
and id=${@cn.followtry.mybatis.bean.User@haha}
and id='${@cn.followtry.mybatis.bean.User@arr[1]}'
and id='${@cn.followtry.mybatis.bean.User@list[1]}'
and id='${@cn.followtry.mybatis.bean.User@map.get("123")}'
and id='${@cn.followtry.mybatis.bean.CodeTypeEnum@THREE.ordinal()}'
and id='${@cn.followtry.mybatis.bean.User@sayHello(_parameter)}'
</where>
</if>
limit 100
</select>- 变量
id =${id} - 属性
id = ${user.id} - 静态方法(public)- 无参数
id='${@cn.followtry.mybatis.bean.User@name()}' - 待参数
id='${@org.apache.commons.lang3.math.NumberUtils@isNumber("abcd")}' - 静态属性(public)
id=${@cn.followtry.mybatis.bean.User@aaa} - 数组索引
id='${@cn.followtry.mybatis.bean.User@arr[1]}' - 集合
'${@cn.followtry.mybatis.bean.User@list[1]}' - Map
id='${@cn.followtry.mybatis.bean.User@map.get("123")}'
id='${@cn.followtry.mybatis.bean.User@map}' Enum
id=${@cn.followtry.mybatis.bean.CodeTypeEnum@THREE.ordinal()}- 构造方法
id='${new cn.followtry.mybatis.bean.User()}' - java.lang.Math的静态方法
id = ${@@abs(-12345678)}可以省略class的编写,方法的默认class是java.lang.Math - java.lang包下的静态方法
id = ${@Integer@parseInt("654")}可以省略package的编写,类默认的package是java.lang - 解析所有参数
id=’${@cn.followtry.mybatis.bean.User@sayHello(_parameter)}’,其中的参数_parameter是Mybatis内置的所有参数的标识,Map类型
${}语法中通过两个@字符,前者定位到Java类,后者定位到类中的方法或属性
这里只列出的其中一部分,对于Mybatis支持的${}语法,可以参见OGNL语法手册。

评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论