Mybatis使用OGNL绑定函数
直接调用
<sql id="headFilter">
<if test="headFilter != null and headFilter.size != 0">
<foreach collection="headFilter.entrySet()" separator=" " index="columnKey" item="val">
/*对于有非空值且非" "的情况*/
<if test="val != null and !@org.apache.commons.lang3.StringUtils@isBlank(val)">
and `${columnKey}` like concat("%", #{val}, "%")
</if>
/*对于" "或者""的情况*/
<if test="val != null and @org.apache.commons.lang3.StringUtils@isBlank(val)">
and (`${columnKey}` is null or `${columnKey}` = '' or `${columnKey}` like concat("%", ' ', "%"))
</if>
</foreach>
</if>
</sql>
bind静态方法再调用
<sql id="isBlank">
<bind name="isBlank" value=":[@org.apache.commons.lang3.StringUtils@isBlank(#this)]"/>
</sql>
<sql id="headFilter">
<include refid="com.wf.dev.mapper.userexp.UserExpCommonMapper.isBlank"/>
<if test="headFilter != null and headFilter.size != 0">
<foreach collection="headFilter.entrySet()" separator=" " index="columnKey" item="val">
/*对于有非空值且非" "的情况*/
<if test="#fn = isBlank, val != null and !#fn(val)">
and `${columnKey}` like concat("%", #{val}, "%")
</if>
/*对于" "或者""的情况*/
<if test="#fn = isBlank, val != null and #fn(val)">
and (`${columnKey}` is null or `${columnKey}` = '' or `${columnKey}` like concat("%", ' ', "%"))
</if>
</foreach>
</if>
</sql>
参考:
https://gist.github.com/jacknie84/3f51046d1a53a86a549a4994ff4f0a8c