zoukankan      html  css  js  c++  java
  • MyBatis中的OGNL教程

     OGNL -- Object Graph Navigation Library

    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 * e2e1/e2e1 - e2,   e1%e2
    • !e, not e:非,求反
    • e.method(args) 调用对象方法
    • e.property 对象属性值
    • e1[ e2 ] 按索引取值,List,数组和Map
    • @class@method(args) 调用类的静态方法
    • @class@field  调用类的静态字段值

    上述内容是在MyBatis中使用的OGNL表达式,完整的表达式点击这里

    MyBatis中什么地方可以使用OGNL?

    深入了解MyBatis参数,这篇博客中提到了OGNL和一些特殊用法。

    MyBatis中可以使用OGNL的地方有两处:

    • 动态SQL表达式中
    • ${param}参数中

    上面这两处地方在MyBatis中处理的时候都是使用OGNL处理的。

    下面通过举例来说明这两种情况的用法。

    1.动态SQL表达式中

    例一,MySQL like 查询:

    复制代码
    <select id="xxx" ...>
        select id,name,... from country
        <where>
            <if test="name != null and name != ''">
                name like concat('%', #{name}, '%')
            </if>
        </where>
    </select>
    复制代码

    上面代码中test的值会使用OGNL计算结果。

    例二,通用 like 查询:

    复制代码
    <select id="xxx" ...>
        select id,name,... from country
        <bind name="nameLike" value="'%' + name + '%'"/>
        <where>
            <if test="name != null and name != ''">
                name like #{nameLike}
            </if>
        </where>
    </select>
    复制代码

    这里<bind>value值会使用OGNL计算。

    注:对<bind参数的调用可以通过#{}或 ${} 方式获取,#{}可以防止注入。

    在通用Mapper中支持一种UUID的主键,在通用Mapper中的实现就是使用了<bind>标签,这个标签调用了一个静态方法,大概方法如下:

    <bind name="username_bind" 
          value='@java.util.UUID@randomUUID().toString().replace("-", "")' />

    这种方式虽然能自动调用静态方法,但是没法回写对应的属性值,因此使用时需要注意。

    2.${param}参数中

    上面like的例子中使用下面这种方式最简单

    复制代码
    <select id="xxx" ...>
        select id,name,... from country
        <where>
            <if test="name != null and name != ''">
                name like '${'%' + name + '%'}'
            </if>
        </where>
    </select>
    复制代码

    这里注意写的是${'%' + name + '%'},而不是%${name}%,这两种方式的结果一样,但是处理过程不一样。

    在MyBatis中处理${}的时候,只是使用OGNL计算这个结果值,然后替换SQL中对应的${xxx},OGNL处理的只是${这里的表达式}

    这里表达式可以是OGNL支持的所有表达式,可以写的很复杂,可以调用静态方法返回值,也可以调用静态的属性值。

    例子:使用OGNL实现单表的分表功能

    上面说的是OGNL简单的使用方法。这里举个OGNL实现数据库分表的例子。

    分表这个功能是通用Mapper中的新功能,允许在运行的时候指定一个表名,通过指定的表名对表进行操作。这个功能实现就是使用了OGNL。

    首先并不是所有的表都需要该功能,因此定义了一个接口,当参数(接口方法只有实体类一个参数)对象继承该接口的时候,就允许使用动态表名。

    1
    2
    3
    4
    5
    6
    7
    8
    9
    public interface IDynamicTableName {
     
        /**
         * 获取动态表名 - 只要有返回值,不是null和'',就会用返回值作为表名
         *
         * @return
         */
        String getDynamicTableName();
    }

    然后在XML中写表名的时候使用:

    复制代码
    <if test="@tk.mybatis.mapper.util.OGNL@isDynamicParameter(_parameter) 
                and dynamicTableName != null 
                and dynamicTableName != ''">
        ${dynamicTableName}
    </if>
    <if test="@tk.mybatis.mapper.util.OGNL@isNotDynamicParameter(_parameter) 
                or dynamicTableName == null 
                or dynamicTableName == ''">
        defaultTableName
    </if>
    复制代码

    由于需要判断_parameter是否继承了IDynamicTableName接口,简单的写法已经无法实现,所以使用了静态方法,这两个方法如下:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    /**
     * 判断参数是否支持动态表名
     *
     * @param parameter
     * @return true支持,false不支持
     */
    public static boolean isDynamicParameter(Object parameter) {
        if (parameter != null && parameter instanceof IDynamicTableName) {
            return true;
        }
        return false;
    }
     
    /**
     * 判断参数是否b支持动态表名
     *
     * @param parameter
     * @return true不支持,false支持
     */
    public static boolean isNotDynamicParameter(Object parameter) {
        return !isDynamicParameter(parameter);
    }

    根据<if>判断的结果来选择使用那个表名。

    另外注意XML判断中有一个dynamicTableName,这个参数是根据getDynamicTableName方法得到的,MyBatis使用属性对应的getter方法来获取值,不是根据field来获取值。

    https://www.cnblogs.com/lxl57610/p/7436648.html

  • 相关阅读:
    poj 1015 Jury Compromise(背包+方案输出)
    最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)
    轮廓线DP:poj 2279 Mr. Young's Picture Permutations
    LCS的几种求法
    POJ 1737 Connected Graph(高精度+DP递推)
    Cats transport(codeforces311B)(斜率优化)
    高精度(压位+判负数+加减乘+读写)
    洛谷 P2257 YY的GCD
    [POI2007]ZAP-Queries (莫比乌斯反演+整除分块)
    [SDOI2010]古代猪文 (欧拉,卢卡斯,中国剩余)
  • 原文地址:https://www.cnblogs.com/emanlee/p/15341602.html
Copyright © 2011-2022 走看看