MyBatis中的OGNL教程


MyBatis中的OGNL教程

有些人可能不知道MyBatis中使用了OGNL,有些人知道用到了OGNL卻不知道在MyBatis中如何使用,本文就是講如何在MyBatis中使用OGNL。

如果我們搜索OGNL相關的內容,通常的結果都是和Struts有關的,你肯定搜不到和MyBatis有關的,雖然和Struts中的用法類似但是換種方式理解起來就有難度。

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調用類的靜態字段值

上述內容只是合適在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。

首先並不是所有的表都需要該功能,因此定義了一個接口,當參數(接口方法只有實體類一個參數)對象繼承該接口的時候,就允許使用動態表名。

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接口,簡單的寫法已經無法實現,所以使用了靜態方法,這兩個方法如下:

/**
 * 判斷參數是否支持動態表名
 *
 * @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來獲取值。

最后

如果你真想了解MyBatis中的OGNL用法,自己多寫幾個例子測試玩玩,動手測試是一種好的學習方式。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM