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, id。value 屬性是 Result 注解的數組。 The id attribute is the name of the result mapping.
@Result 方法
  • <result>
  • <id>
在列和屬性或字段之間的單獨結果映 射。屬 性:id,column, property, javaType ,jdbcType ,type Handler, one,many。id 屬性是一個布爾值,表 示了應該被用於比較(和在 XML 映射 中的<id>相似)的屬性。one 屬性是單 獨 的 聯 系, 和 <association> 相 似 , 而 many 屬 性 是 對 集 合 而 言 的 , 和 <collection>相似。 它們這樣命名是為了 避免名稱沖突。
@One 方法 <association> 復雜類型的單獨屬性值映射。屬性: select,已映射語句(也就是映射器方 法)的完全限定名,它可以加載合適類 型的實例。注意:聯合映射在注解 API 中是不支持的。這是因為 Java 注解的 限制,不允許循環引用。 fetchType, which supersedes the global configuration parameterlazyLoadingEnabled for this mapping.
@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,fetchType, which supersedes the global configuration parameterlazyLoadingEnabled for this mapping. 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=FlushCachePolicy.DEFAULT , resultSetType=FORWARD_ONLY , statementType=PREPARED , fetchSize=-1 , , timeout=-1 useGeneratedKeys=false , keyProperty=”id” , keyColumn=”” , resultSets=””。 理解 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 指定的方法. 該方法可以有選擇地接受參數對象.(In MyBatis 3.4 or later, it’s allow multiple parameters) 屬性: type,method。type 屬性是類。method 屬性是方法名。 注意: 這節之后是對 類的 討論,它可以幫助你以干凈,容於閱讀 的方式來構建動態 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@InsertProvider@Update or@UpdateProvider. 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 orfalse to denote if the SQL statement should be executed before or after the insert, resultType which is the Java type of the keyProperty, andstatementType=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.
@Flush Method N/A If this annotation is used, it can be called theSqlSession#flushStatements() via method defined at a Mapper interface.(MyBatis 3.3 or above)
映射申明樣例

這個例子展示了如何使用 @SelectKey 注解來在插入前讀取數據庫序列的值:

@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);

這個例子展示了如何使用 @SelectKey 注解來在插入后讀取數據庫識別列的值:

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

This example shows using the @Flush annotation to call the SqlSession#flushStatements():

@Flush List<BatchResult> flush();

These examples show how to name a ResultMap by specifying id attribute of @Results annotation.

@Results(id = "userResult", value = { @Result(property = "id", column = "uid", id = true), @Result(property = "firstName", column = "first_name"), @Result(property = "lastName", column = "last_name") }) @Select("select * from users where id = #{id}") User getUserById(Integer id); @Results(id = "companyResults") @ConstructorArgs({ @Arg(property = "id", column = "cid", id = true), @Arg(property = "name", column = "name") }) @Select("select * from company where id = #{id}") Company getCompanyById(Integer id);

This example shows solo parameter using the Sql Provider annotation:

@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName") List<User> getUsersByName(String name); class UserSqlBuilder { public String buildGetUsersByName(final String name) { return new SQL(){{ SELECT("*"); FROM("users"); if (name != null) { WHERE("name like #{value} || '%'"); } ORDER_BY("id"); }}.toString(); } }

This example shows multiple parameters using the Sql Provider annotation:

@SelectProvider(type = UserSqlBuilder.class, method = "buildGetUsersByName") List<User> getUsersByName( @Param("name") String name, @Param("orderByColumn") String orderByColumn); class UserSqlBuilder { // If not use @Param, you should be define same arguments with mapper method public String buildGetUsersByName( final String name, final String orderByColumn) { return new SQL(){{ SELECT("*"); FROM("users"); WHERE("name like #{name} || '%'"); ORDER_BY(orderByColumn); }}.toString(); } // If use @Param, you can define only arguments to be used public String buildGetUsersByName(@Param("orderByColumn") final String orderByColumn) { return new SQL(){{ SELECT("*"); FROM("users"); WHERE("name like #{name} || '%'"); ORDER_BY(orderByColumn); }}.toString(); } }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM