其他分享
首页 > 其他分享> > Mybatis使用OGNL绑定函数

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>

参考:

OGNL

https://gist.github.com/jacknie84/3f51046d1a53a86a549a4994ff4f0a8c

标签:columnKey,like,val,绑定,OGNL,Mybatis,null,concat
来源: https://www.cnblogs.com/wftop1/p/16476856.html