ibatis ResultMap ParameterMap

來源:互聯網
上載者:User
在執行查詢 Mapped Statement 時,resultMap 負責將結果集的列值對應成 Java Bean的屬性值。resultMap 的結構如下:

<resultMap id="resultMapName" clss="some.domain.Class" [extends="parent-resultMap"]>
<result property="ropertyName" clumn="COLUMN_NAME"
[columnIndex=""] [jaaType="int"]jdbcType="NUMERIC"
[nullValue="-999999"] [elect="smeOtherStatement"]
/>
<result ....../>
<result ....../>
<result ....../>
</resultMap>

// extends 是可選的屬性,可設定成另外一個 resultMap 的名字,並以它為基礎。和在 Java 中繼承一個類相似,父 resultMap 的屬性將作為子 resutlMap 的一部分。父 resultMap 的屬性總是加到子 resultMap 屬性的前面,並且父 resultMap必須要在子 resultMap 之前定義。父 resultMap 和子 resultMap 的 class 屬性不一定要一致,它們可以沒有任何關係。
// columnIndex 是可選的,用於改善效能。屬性 columnIndex的值是 ResultSet 中用於賦值 Java Bean 屬性的欄位序號。在 99%的應用中,不太可能需要犧牲可讀性來換取效能。使用 columnIndex,某些 JDBC Driver可以大幅提高效能,某些則沒有任何效果。
// nullValue 指定資料庫中 NULL 的替代值。因此,如果從 ResultSet 中讀出 NULL
值,Java Bean 屬性將被賦值屬性 null指定的替代值


parameterMap 負責將 Java Bean 的屬性對應成 statement 的參數

<parameterMap id="parameterMapName" class="com.omain.Product"]>
    <parameter property ="proprtyName" [jdbcType="VARCHAR"] [jvaType="tring"]
        [nullValue="NUMERIC"] [ull="-999999"] />
    <parameter ...... />
    <parameter ...... />
</parameterMap>

// 括弧[]是可選的屬性。parameterMap 元素只要求屬性 id 作為唯一標識。屬性 class 是可選的但強烈推薦使用。和 parameterClass 類似,class 屬性可以架構檢查輸入參數的類型並最佳化效能。

//以下是一個完整的例子

<parameterMap id="insert-product-param" class"cm.domain.Product">
    <parameter property="id" jdbcType="NUMERIC" jvaType="int" nullValue="-9999999"/>
    <parameter property="dscription" dbcType="VARCHAR" nullVlue="NO_ENTRY"/>
</parameterMap>

<statement id="insertroduct" prameterMap="insert-product-param">
    insert into PRODUCT (PRD_ID, PRD_DESCRIPTION) values (?,?);
</statement>

//上面的例子中,Java Bean 的屬性“id”和“description”將按順序地替換 Mapped Statement
“insertProduct”的值符號(?)

注意:parameterMap 並不自動地綁定到特定的 Java 類。因此在上面的例子中,任何擁有“id”和“description”屬性的 Java Bean 對象,都可以作為 parameterMap 的輸入。如果需要將輸入綁定到特定的 Java 類,可以使用 mapped-statement 的 resultClass 屬性。

注意:Parameter Map 的名稱(id)局部的,只在定義它的 SQL Map XML檔案中有效。不過,加上 SQL Map 的名稱(即在<sqlMap>根項目中的名稱)作為首碼,您可以在另一個 SQL Map XML檔案中引用它。例如,要在另一個檔案中引用以上的 parameterMap,可以使用名稱“Product.insert-product-param”。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.