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

posted @   FynnWang  阅读(143)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 单元测试从入门到精通
· 上周热点回顾(3.3-3.9)
· Vue3状态管理终极指南:Pinia保姆级教程
more_horiz
keyboard_arrow_up dark_mode palette
选择主题
menu
点击右上角即可分享
微信分享提示