2013-11-21 72 views
3

我有一個自定義的typeHandler一個<resultMap>的結果屬性之一:MyBatis的自定義類型處理器

<resultMap id="foo" type="hashmap"> 
    ... 
    <result property="SERVICES_XML" javaType="string" jdbcType="CLOB" typeHandler="com.foo.bar.OracleClobTypeHandler" /> 
    ... 
</resultMap> 

無論哪個屬性我我的處理程序連接到(我的意思是,這是不特定於CLOB的問題,也嘗試了VARCHAR),當我從數據庫獲取結果時,處理程序不會被調用。

我在自定義處理程序的所有方法設置斷點:執行

public class OracleClobTypeHandler implements TypeHandler<String> { 

    @Override 
    public void setParameter(PreparedStatement ps, int i, String parameter, JdbcType jdbcType) throws SQLException { 
    log.debug("setParameter called"); <================ BREAKPOINT HERE 
    } 

    @Override 
    public String getResult(ResultSet rs, String columnName) 
     throws SQLException { 
    log.debug("getResult 2 called"); <================ BREAKPOINT HERE 
    return ""; 
    } 

    @Override 
    public String getResult(ResultSet rs, int columnIndex) 
     throws SQLException { 
    log.debug("getResult 2 called"); <================ BREAKPOINT HERE 
    return ""; 
    } 

    @Override 
    public String getResult(CallableStatement cs, int columnIndex) 
     throws SQLException { 
    log.debug("getResult 3 called"); <================ BREAKPOINT HERE 
    return ""; 
    } 
} 

顯然沒有上面的方法。

我試過把<typeHandler javaType="java.lang.String" jdbcType="CLOB" handler="com.foo.bar.OracleClobTypeHandler"/>放在myBatis <configuration>中,但是這也不起作用。
也沒有做任何其他事情,包括延伸TypeHandler<Object>等。

我在做什麼錯?

回答

4

經過長時間的挖掘,終於找到了答案。

這似乎是myBatis中的一個錯誤。

爲了讓您的處理器爲<result>元素的工作,你必須指定column屬性明確即使property屬性已經匹配列名,並在豆字段名。
對我來說,它看起來像這樣:

<result property="SERVICES_XML" column="SERVICES_XML" javaType="string" jdbcType="CLOB" typeHandler="com.foo.bar.OracleClobTypeHandler" /> 

注意上面的變化也將引起<configuration>標籤定義工作處理程序,所以內聯typeHandler可能不再需要 - 這是我的情況。我結束了:

<configuration> 
    <typeHandlers> 
    <typeHandler javaType="java.lang.String" jdbcType="CLOB" handler="com.foo.bar.OracleClobTypeHandler"/>   
    </typeHandlers> 
</configuration> 

<result property="SERVICES_XML" column="SERVICES_XML" javaType="string" jdbcType="CLOB" /> 
2

雖然這個問題是現在3歲,我寧願賭的問題是關係到一個事實,即甲骨文轉換爲大寫「列名從結果元數據中檢索,而區分大小寫的java對象屬性名稱通常尊重駱駝大小寫。

然後顯式列屬性映射是必需的,除非您在SQL查詢中使用帶引號的別名。 The question has been asked here。 而不像最新的評論中所說的,我觀察到ORDER BY引用別名的作品。