2012-07-24 30 views
1

我試圖根據來自mybatis配置的參數而不是查詢參數創建條件查詢片段。事情是這樣的:配置參數上的Mybatis條件

<sql id="frag"> 
    <if test="col_name != null"> 
     SELECT * FROM TABLE WHERE ${col.name}=#{value} 
    </if> 
    <if test="col_name == null"> 
     SELECT * FROM TABLE WHERE SAMPLECOL=#{value} 
    </if> 
</sql> 

其中col_name值是一個全球性的參數,在裏面的.properties文件指定由MyBatis的配置讀取。

顯然這是行不通的;看着源代碼,看起來OGNL表達式求值器並不知道配置屬性(當我通過SQL內部的${...}進行參數替換時,該配置屬性正在工作)。有沒有人找到辦法做到這一點?

回答

1

我發現這是目前不可能的; OGNL實際上無法訪問配置屬性。

作爲mybatis郵件列表中的this post建議的解決方法,我編寫了一個簡單的攔截器,它讀取配置參數並將它們添加到查詢參數映射中。不完全乾淨,但它的工作原理。

攔截代碼:

@Intercepts({ 
    @Signature(type = Executor.class, 
    method = "query", 
    args = {MappedStatement.class, Object.class, RowBounds.class, ResultHandler.class})}) 
public class ConfigPropInterceptor implements Interceptor { 

    private final Map<String, Object> properties = new HashMap<String, Object>(); 

    @Override 
    public Object intercept(Invocation invocation) throws Throwable { 
     Object param = invocation.getArgs()[1]; 
     if (param instanceof Map) { 
      ((Map<String, Object>)param).putAll(properties); 
     } 
     return invocation.proceed(); 
    } 

    @Override 
    public Object plugin(Object target) { 
     return Plugin.wrap(target, this); 
    } 

    @Override 
    public void setProperties(Properties properties) { 
     for (String p : properties.stringPropertyNames()) { 
      this.properties.put(p, properties.getProperty(p)); 
     } 
    } 
} 

實施例的使用在配置上的.xml:

<plugins> 
    <plugin interceptor="...ConfigPropInterceptor"> 
     <property name="_issuerLocation" value="${issuer.location}"/> 
    </plugin> 
</plugins> 

通過此設置,我能夠測試_issuerLocation變量在OGNL表達式等一切。