1.MyBatis常用的OGNL
e1 or e2
e1 and e2
e1 == e2,e1 eq e2
e1 != e2,e1 neq e2
e1 lt e2:小于
e1 lte e2:小于等于,其他gt(大于),gte(大于等于)
e1 in e2
e1 not in e2
e1 + e2,e1 * e2,e1/e2,e1 - e2,e1%e2
!e,not e:非,求反
e.method(args)调用对象方法
e.property对象属性值
e1[ e2 ]按索引取值,List,数组和Map
@class@method(args)调用类的静态方法
@class@field调用类的静态字段值
2.模糊查询标签<bind>
<select id="getUser" resultType="User">
<bind name="name" value="'_'+ob+'%'"/>
select *
from user
where name like #{name}
</select>
3.<bind>标签的特殊使用
(代码中bind标签的value值@com.syg.gamemanage.util.MbValid@instance()
采用OGNL表达式@class@method(args)调用类的静态方法这里的目的是采用单例模式(懒汉模式)创建对象)
MbValid.noZero(example.id)调用对象的方法返回值为boolean类型
<select id="queryList" resultType="Operation"> select * from operation <where> <bind name="MbValid" value="@com.syg.gamemanage.util.MbValid@instance()"/> <if test="MbValid.noZero(example.id)">and id = #{example.id}</if> <if test="MbValid.str(example.operationName)">and operation_name = #{example.operationName}</if> </where> order by create_ts </select>
public class MbValid { private static MbValid instance; //懒汉模式 public static MbValid instance() { if (instance == null) { instance = new MbValid(); } return instance; } public static boolean str(String str) { if (str == null || str.trim().length() <= 0) { return false; } return true; } public static boolean noZero(Integer integer) { if (integer == null || integer == 0) { return false; } return true; } }