Mybatis--映射器註解

來源:互聯網
上載者:User

因為最初設計時,MyBatis 是一個 XML 驅動的架構。配置資訊是基於 XML 的,而且 映射語句也是定義在 XML 中的。而到了 MyBatis 3,有新的可用的選擇了。MyBatis 3 構建 在基於全面而且強大的 Java 配置 API 之上。這個配置 API 是基於 XML 的 MyBatis 配置的 基礎,也是新的基於註解配置的基礎。註解提供了一種簡單的方式來實現簡易對應語句,而 不會引入大量的開銷。

注意 不幸的是,Java 註解限制了它們的表現和靈活。儘管很多時間都花調查,設計和 實驗上,最強大的 MyBatis 映射不能用註解來構建,那並不可笑。C#屬性(做樣本)就沒 有這些限制,因此 MyBatis.NET 將會比 XML 有更豐富的選擇。也就是說,基於 Java 註解 的配置離不開它的特性。

註解有下面這些:


註解 目標 相對應的 XML 描述
@CacheNamespace 類 <cache> 為給定的命名空間 (比如類) 配置緩衝。 屬性:implemetation,eviction, flushInterval,size 和 readWrite。
@CacheNamespaceRef 類 <cacheRef> 參照另外一個命名空間的緩衝來使用。 屬性:value,應該是一個名空姐的字 符串值(也就是類的完全限定名) 。
@ConstructorArgs Method <constructor> 收集一組結果傳遞給一個劫奪對象的 構造方法。屬性:value,是形式參數 的數組。
@Arg 方法 <arg> <idArg> 單 獨 的 構 造 方 法 參 數 , 是 ConstructorArgs 集合的一部分。屬性: id,column,javaType,typeHandler。 id 屬性是布爾值, 來標識用於比較的屬 性,和<idArg>XML 元素相似。
@TypeDiscriminator 方法 <discriminator> 一組執行個體值被用來決定結果映射的表 現。 屬性: column, javaType, jdbcType, typeHandler,cases。cases 屬性就是實 例的數組。
@Case 方法 <case> 單獨執行個體的值和它對應的映射。屬性: value,type,results。Results 屬性是結 果數組,因此這個註解和實際的 ResultMap 很相似,由下面的 Results 註解指定。
@Results 方法 <resultMap> 結果映射的列表, 包含了一個特別結果 列如何被映射到屬性或欄位的詳情。 屬 性:value,是 Result 註解的數組。
@Result 方法 <result> <id> 在列和屬性或欄位之間的單獨結果映 射。屬 性:id,column, property, javaType ,jdbcType ,type Handler, one,many。id 屬性是一個布爾值,表 示了應該被用於比較(和在 XML 映射 中的<id>相似)的屬性。one 屬性是單 獨 的 聯 系, 和 <association> 相 似 , 而 many 屬 性 是 對 集 合 而 言 的 , 和 <collection>相似。 它們這樣命名是為了 避免名稱衝突。
@One 方法 <association> 複雜類型的單獨屬性值映射。屬性: select,已映射語句(也就是映射器方 法)的完全限定名,它可以載入合適類 型的執行個體。注意:聯合映射在註解 API 中是不支援的。這是因為 Java 註解的 限制,不允許循環參考。
@Many 方法 <collection> A mapping to a collection property of a complex type. Attributes: select, which is the fully qualified name of a mapped statement (i.e. mapper method) that can load a collection of instances of the appropriate types. NOTE You will notice that join mapping is not supported via the Annotations API. This is due to the limitation in Java Annotations that does not allow for circular references.
@MapKey 方法   複 雜 類 型 的 集合 屬 性 映射 。 屬 性 : select,是映射語句(也就是映射器方 法)的完全限定名,它可以載入合適類 型的一組執行個體。注意:聯合映射在 Java 註解中是不支援的。這是因為 Java 注 解的限制,不允許循環參考。
@Options 方法 映射語句的屬性 這個註解提供訪問交換和配置選項的 寬廣範圍, 它們通常在映射語句上作為 屬性出現。 而不是將每條語句註解變複 雜,Options 註解提供連貫清晰的方式 來訪問它們。屬性:useCache=true , flushCache=false , resultSetType=FORWARD_ONLY , statementType=PREPARED , fetchSize=-1 , , timeout=-1 useGeneratedKeys=false , keyProperty=”id”。 理解 Java 註解是很 重要的,因為沒有辦法來指定“null” 作為值。因此,一旦你使用了 Options 註解,語句就受所有預設值的支配。要 注意什麼樣的預設值來避免不期望的 行為。
@Insert @Update @Delete @Select 方法 <insert> <update> <delete> <select> 這些註解中的每一個代表了執行的真 實 SQL。 它們每一個都使用字串數組 (或單獨的字串)。如果傳遞的是字 符串數組, 它們由每個分隔它們的單獨 空間串聯起來。這就當用 Java 代碼構 建 SQL 時避免了“丟失空間”的問題。 然而,如果你喜歡,也歡迎你串聯單獨 的字串。屬性:value,這是字串 數組用來組成單獨的 SQL 陳述式。
@InsertProvider @UpdateProvider @DeleteProvider @SelectProvider 方法 <insert> <update> <delete> <select> 這些可選的 SQL 註解允許你指定一個 類名和一個方法在執行時來返回運行 允許建立動態 的 SQL。 基於執行的映射語句, MyBatis 會執行個體化這個類,然後執行由 provider 指定的方法. 這個方法可以選擇性的接 受參數對象作為它的唯一參數, 但是必 須只指定該參數或者沒有參數。屬性: type,method。type 屬性是類的完全限 定名。method 是該類中的那個方法名。 注意: 這節之後是對 SelectBuilder 類的 討論,它可以協助你以乾淨,容於閱讀 的方式來構建動態 SQL。
@Param Parameter N/A 如果你的映射器的方法需要多個參數, 這個註解可以被應用於映射器的方法 參數來給每個參數一個名字。否則,多 參數將會以它們的順序位置來被命名 (不包括任何 RowBounds 參數) 比如。 #{param1} , #{param2} 等 , 這 是 默 認 的 。 使 用 @Param(“person”),參數應該被命名為 #{person}。
@SelectKey Method <selectKey> This annotation duplicates the <selectKey> functionality for methods annotated with @Insert or @InsertProvider. It is ignored for other methods. If you specify a @SelectKey annotation, then MyBatis will ignore any generated key properties set via the @Options annotation, or configuration properties. Attributes: statement an array of strings which is the SQL statement to execute, keyProperty which is the property of the parameter object that will be updated with the new value, before which must be either true or false to denote if the SQL statement should be executed before or after the insert, resultType which is the Java type of the keyProperty, and statementType=PREPARED.
@ResultMap Method N/A This annotation is used to provide the id of a <resultMap> element in an XML mapper to a @Select or @SelectProvider annotation. This allows annotated selects to reuse resultmaps that are defined in XML. This annotation will override any @Results or @ConstructorArgs annotation if both are specified on an annotated select.
@ResultType Method N/A This annotation is used when using a result handler. In that case, the return type is void so MyBatis must have a way to determine the type of object to construct for each row. If there is an XML result map, use the @ResultMap annotation. If the result type is specified in XML on the <select> element, then no other annotation is necessary. In other cases, use this annotation. For example, if a @Select annotated method will use a result handler, the return type must be void and this annotation (or @ResultMap) is required. This annotation is ignored unless the method return type is void.

Mapper Annotation Examples

This example shows using the @SelectKey annotation to retrieve a value from a sequence before an insert:

@Insert("insert into table3 (id, name) values(#{nameId}, #{name})")@SelectKey(statement="call next value for TestSequence", keyProperty="nameId", before=true, resultType=int.class)int insertTable3(Name name);

This example shows using the @SelectKey annotation to retrieve an identity value after an insert:

@Insert("insert into table2 (name) values(#{name})")@SelectKey(statement="call identity()", keyProperty="nameId", before=false, resultType=int.class)int insertTable2(Name name);


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.