1. 程式人生 > >MyBatis 註解(摘自MyBatis官方文件)

MyBatis 註解(摘自MyBatis官方文件)

註解 目標 相對應的 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 @SelectProviderannotation. 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.