MyBatis的創建基于這樣一個思想:數據庫并不是您想怎樣就怎樣的。雖然我們希望所有的數據庫遵守第三范式或BCNF(修正的第三范式),但它們不是。如果有一個數據庫能夠完美映射到所有應用程序,也將是非常棒的,但也沒有。結果集映射就是MyBatis為解決這些問題而提供的解決方案。例如,我們如何映射下面這條語句?
[java] view plain copy PRint?<!– Very Complex Statement –> <select id=”selectBlogDetails” parameterType=“int” resultMap=“detailedBlogResultMap”> select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.passWord as author_password, A.email as author_email, A.bio as author_bio, A.favourite_section as author_favourite_section, P.id as post_id, P.blog_id as post_blog_id, P.author_id as post_author_id, P.created_on as post_created_on, P.section as post_section, P.subject as post_subject, P.draft as draft, P.body as post_body, C.id as comment_id, C.post_id as comment_post_id, C.name as comment_name, C.comment as comment_text, T.id as tag_id, T.name as tag_name from Blog B left outer join Author A on B.author_id = A.id left outer join Post P on B.id = P.blog_id left outer join Comment C on P.id = C.post_id left outer join Post_Tag PT on PT.post_id = P.id left outer join Tag T on PT.tag_id = T.id where B.id = #{id} </select> <wbr><!-- Very Complex Statement --><select id="selectBlogDetails" parameterType="int" resultMap="detailedBlogResultMap">selectB.id as blog_id,B.title as blog_title,B.author_id as blog_author_id,A.id as author_id,A.username as author_username,A.password as author_password,A.email as author_email,A.bio as author_bio,A.favourite_section as author_favourite_section,P.id as post_id,P.blog_id as post_blog_id,P.author_id as post_author_id,P.created_on as post_created_on,P.section as post_section,P.subject as post_subject,P.draft as draft,P.body as post_body,C.id as comment_id,C.post_id as comment_post_id,C.name as comment_name,C.comment as comment_text,T.id as tag_id,T.name as tag_namefrom Blog Bleft outer join Author A on B.author_id = A.idleft outer join Post P on B.id = P.blog_idleft outer join Comment C on P.id = C.post_idleft outer join Post_Tag PT on PT.post_id = P.idleft outer join Tag T on PT.tag_id = T.idwhere B.id = #{id}</select>您可能想要把它映射到一個智能的對象模型,包括由一個作者寫的一個博客,有許多文章(Post,帖子),每個文章由0個或者多個評論和標簽。下面是一個復雜ResultMap 的完整例子(假定作者、博客、文章、評論和標簽都是別名)。仔細看看這個例子,但是不用太擔心,我們會一步步地來分析,一眼看上去可能讓人沮喪,但是實際上非常簡單的
[java] view plain copy print?<!– Very Complex Result Map –> <resultMap id=”detailedBlogResultMap” type=“Blog”> <constructor> <idArg column=”blog_id” javaType=“int”/> </constructor> <result property=”title” column=“blog_title”/> <association property=”author” column=“blog_author_id” javaType=“ Author”> <id property=”id” column=“author_id”/> <result property=”username” column=“author_username”/> <result property=”password” column=“author_password”/> <result property=”email” column=“author_email”/> <result property=”bio” column=“author_bio”/> <result property=”favouriteSection” column=“author_favourite_section”/> </association> <collection property=”posts” ofType=“Post”> <id property=”id” column=“post_id”/> <result property=”subject” column=“post_subject”/> <association property=”author” column=“post_author_id” javaType=“Author”/> <collection property=”comments” column=“post_id” ofType=“ Comment”> <id property=”id” column=“comment_id”/> </collection> <collection property=”tags” column=“post_id” ofType=“ Tag” > <id property=”id” column=“tag_id”/> </collection> <discriminator javaType=”int” column=“draft”> <case value=“1” resultType=“DraftPost”/> </discriminator> </collection> </resultMap> <wbr><!-- Very Complex Result Map --><resultMap id="detailedBlogResultMap" type="Blog"><constructor><idArg column="blog_id" javaType="int"/></constructor><result property="title" column="blog_title"/><association property="author" column="blog_author_id" javaType=" Author"><id property="id" column="author_id"/><result property="username" column="author_username"/><result property="password" column="author_password"/><result property="email" column="author_email"/><result property="bio" column="author_bio"/><result property="favouriteSection" column="author_favourite_section"/></association><collection property="posts" ofType="Post"><id property="id" column="post_id"/><result property="subject" column="post_subject"/><association property="author" column="post_author_id" javaType="Author"/><collection property="comments" column="post_id" ofType=" Comment"><id property="id" column="comment_id"/></collection><collection property="tags" column="post_id" ofType=" Tag" ><id property="id" column="tag_id"/></collection><discriminator javaType="int" column="draft"><case value="1" resultType="DraftPost"/></discriminator></collection></resultMap>這個resultMap 的元素的子元素比較多,討論起來比較寬泛。下面我們從概念上概覽一下這個resultMap的元素。
resultMap
·constructor–實例化的時候通過構造器將結果集注入到類中
oidArg– ID 參數; 將結果集標記為ID,以方便全局調用
oarg–注入構造器的結果集
·id–結果集ID,將結果集標記為ID,以方便全局調用
·result–注入一個字段或者javabean屬性的結果
·association–復雜類型聯合;許多查詢結果合成這個類型
o嵌套結果映射– associations能引用自身,或者從其它地方引用
·collection–復雜類型集合
o嵌套結果映射– collections能引用自身,或者從其它地方引用
·discriminator–使用一個結果值以決定使用哪個resultMap
ocase–基于不同值的結果映射
§嵌套結果映射–case也能引用它自身, 所以也能包含這些同樣的元素。它也可以從外部引用resultMap
è最佳實踐:逐步地生成resultMap,單元測試對此非常有幫助。如果您嘗試一下子就生成像上面這樣巨大的resultMap,可能會出錯,并且工作起來非常吃力。從簡單地開始,再一步步地擴展,并且進行單元測試。使用框架開發有一個缺點,它們有時像是一個黑合。為了確保達到您所預想的行為,最好的方式就是進行單元測試。這對提交bugs 也非常有用。
下一節,我們一步步地查看這些細節。
id, result元素
<id property=”id” column=”post_id”/>
<result property=”subject” column=”post_subject”/>
這是最基本的結果集映射。id 和result 將列映射到屬性或簡單的數據類型字段(String, int, double, Date等)。
這兩者唯一不同的是,在比較對象實例時id 作為結果集的標識屬性。這有助于提高總體性能,特別是應用緩存和嵌套結果映射的時候。
Id、result屬性如下:
Attribute
Description
property
映射數據庫列的字段或屬性。如果JavaBean 的屬性與給定的名稱匹配,就會使用匹配的名字。否則,MyBatis 將搜索給定名稱的字段。兩種情況下您都可以使用逗點的屬性形式。比如,您可以映射到“username”,也可以映射到“address.street.number”。
column
數據庫的列名或者列標簽別名。與傳遞給resultSet.getString(columnName)的參數名稱相同。
javaType
完整Java類名或別名(參考上面的內置別名列表)。如果映射到一個JavaBean,那MyBatis 通常會自行檢測到。然而,如果映射到一個HashMap,那您應該明確指定javaType 來確保所需行為。
jdbcType
這張表下面支持的JDBC類型列表列出的JDBC類型。這個屬性只在insert,update 或delete 的時候針對允許空的列有用。JDBC 需要這項,但MyBatis 不需要。如果您直接編寫JDBC代碼,在允許為空值的情況下需要指定這個類型。
typeHandler
我們已經在文檔中討論過默認類型處理器。使用這個屬性可以重寫默認類型處理器。它的值可以是一個TypeHandler實現的完整類名,也可以是一個類型別名。
支持的JDBC類型
MyBatis支持如下的JDBC類型:
BIT
FLOAT
CHAR
TIMESTAMP
OTHER
UNDEFINED
TINYINT
REAL
VARCHAR
BINARY
BLOB
NVARCHAR
SMALLINT
DOUBLE
LONGVARCHAR
VARBINARY
CLOB
NCHAR
INTEGER
NUMERIC
DATE
LONGVARBINARY
BOOLEAN
NCLOB
BIGINT
DECIMAL
TIME
NULL
CURSOR
Constructor元素
<constructor>
<idArg column=”id” javaType=”int”/>
<arg column=”username” javaType=”String”/>
</constructor>
當屬性與DTO,或者與您自己的域模型一起工作的時候,許多場合要用到不變類。通常,包含引用,或者查找的數據很少或者數據不會改變的的表,適合映射到不變類中。構造器注入允許您在類實例化后給類設值,這不需要通過public方法。MyBatis同樣也支持private屬性和JavaBeans的私有屬性達到這一點,但是一些用戶可能更喜歡使用構造器注入。構造器元素可以做到這點。
考慮下面的構造器:
public class User {
//…
public User(int id, String username) {
//…
}
//…
}
為了將結果注入構造器,MyBatis需要使用它的參數類型來標記構造器。Java沒有辦法通過參數名稱來反射獲得。因此當創建constructor 元素,確保參數是按順序的并且指定了正確的類型。
<constructor>
<idArg column=”id” javaType=”int”/>
<arg column=”username” javaType=”String”/>
</constructor>
其它的屬性與規則與id、result元素的一樣。
Attribute
Description
column
數據庫的列名或者列標簽別名。與傳遞給resultSet.getString(columnName)的參數名稱相同。
javaType
完整java類名或別名(參考上面的內置別名列表)。如果映射到一個JavaBean,那MyBatis 通常會自行檢測到。然而,如果映射到一個HashMap,那您應該明確指定javaType 來確保所需行為。
jdbcType
支持的JDBC類型列表中列出的JDBC類型。這個屬性只在insert,update 或delete 的時候針對允許空的列有用。JDBC 需要這項,但MyBatis 不需要。如果您直接編寫JDBC代碼,在允許為空值的情況下需要指定這個類型。
typeHandler
我們已經在文檔中討論過默認類型處理器。使用這個屬性可以重寫默認類型處理器。它的值可以是一個TypeHandler實現的完整類名,也可以是一個類型別名。
Association元素
<association property=”author” column=”blog_author_id” javaType=” Author”>
<id property=”id” column=”author_id”/>
<result property=”username” column=”author_username”/>
</association>
Association元素處理“has-one”(一對一)這種類型關系。比如在我們的例子中,一個Blog有一個Author。聯合映射與其它的結果集映射工作方式差不多,指定property、column、javaType(通常MyBatis會自動識別)、jdbcType(如果需要)、typeHandler。
不同的地方是您需要告訴MyBatis 如何加載一個聯合查詢。MyBatis使用兩種方式來加載:
·Nested Select:通過執行另一個返回預期復雜類型的映射SQL語句(即引用外部定義好的SQL語句塊)。
·Nested Results:通過嵌套結果映射(nested result mappings)來處理聯接結果集(joined results)的重復子集。
首先,讓我們檢查一下元素屬性。正如您看到的,它不同于普通只有select和resultMap屬性的結果映射。
Attribute
Description
property
映射數據庫列的字段或屬性。如果JavaBean 的屬性與給定的名稱匹配,就會使用匹配的名字。否則,MyBatis 將搜索給定名稱的字段。兩種情況下您都可以使用逗點的屬性形式。比如,您可以映射到”username”,也可以映射到更復雜點的”address.street.number”。
column
數據庫的列名或者列標簽別名。與傳遞給resultSet.getString(columnName)的參數名稱相同。
注意: 在處理組合鍵時,您可以使用column= “{prop1=col1,prop2=col2}”這樣的語法,設置多個列名傳入到嵌套查詢語句。這就會把prop1和prop2設置到目標嵌套選擇語句的參數對象中。
javaType
完整java類名或別名(參考上面的內置別名列表)。如果映射到一個JavaBean,那MyBatis 通常會自行檢測到。然而,如果映射到一個HashMap,那您應該明確指定javaType 來確保所需行為。
jdbcType
支持的JDBC類型列表中列出的JDBC類型。這個屬性只在insert,update 或delete 的時候針對允許空的列有用。JDBC 需要這項,但MyBatis 不需要。如果您直接編寫JDBC代碼,在允許為空值的情況下需要指定這個類型。
typeHandler
我們已經在文檔中討論過默認類型處理器。使用這個屬性可以重寫默認類型處理器。它的值可以是一個TypeHandler實現的完整類名,也可以是一個類型別名。
聯合嵌套選擇(Nested Select for Association)
select
通過這個屬性,通過ID引用另一個加載復雜類型的映射語句。從指定列屬性中返回的值,將作為參數設置給目標select 語句。表格下方將有一個例子。注意:在處理組合鍵時,您可以使用column=”{prop1=col1,prop2=col2}”這樣的語法,設置多個列名傳入到嵌套語句。這就會把prop1和prop2設置到目標嵌套語句的參數對象中。
例如:
[html] view plain copy print?<resultMap id=”blogResult” type=”Blog”> <association property=“author” column=“blog_author_id” javaType=“Author” select=”selectAuthor”/> </resultMap> <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”> SELECT * FROM BLOG WHERE ID = #{id} </select> <select id=”selectAuthor” parameterType=”int” resultType=“Author”> SELECT * FROM AUTHOR WHERE ID = #{id} </select> <wbr><resultMap id=”blogResult” type=”Blog”><association property="author" column="blog_author_id" javaType="Author"select=”selectAuthor”/></resultMap><select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>SELECT * FROM BLOG WHERE ID = #{id}</select><select id=”selectAuthor” parameterType=”int” resultType="Author">SELECT * FROM AUTHOR WHERE ID = #{id}</select>我們使用兩個select語句:一個用來加載Blog,另一個用來加載Author。Blog的resultMap 描述了使用“selectAuthor”語句來加載author的屬性。
如果列名和屬性名稱相匹配的話,所有匹配的屬性都會自動加載。
譯者注:
上面的例子,首先執行<select id=“selectBlog”>,執行結果存放到<resultMap id=“blogResult”>結果映射中。“blogResult”是一個Blog類型,從<select id=“selectBlog”>查出的數據都會自動賦值給”blogResult”的與列名匹配的屬性,這時blog_id,title等就被賦值了。同時“blogResult”還有一個關聯屬性“Author”,執行嵌套查詢select=”selectAuthor”后,Author對象的屬性id,username,password,email,bio也被賦于數據庫匹配的值。
Blog
{
blog_id;
title;
Author author
{
id;
username;
password;
email;
bio;
}
}
雖然這個方法簡單,但是對于大數據集或列表查詢,就不盡如人意了。這個問題被稱為“N+1 選擇問題”(N+1 Selects Problem)。概括地說,N+1選擇問題是這樣產生的:
·您執行單條SQL語句去獲取一個列表的記錄( “+1”)。
·對列表中的每一條記錄,再執行一個聯合select 語句來加載每條記錄更加詳細的信息(“N”)。
這個問題會導致成千上萬的SQL語句的執行,因此并非總是可取的。
上面的例子,MyBatis可以使用延遲加載這些查詢,因此這些查詢立馬可節省開銷。然而,如果您加載一個列表后立即迭代訪問嵌套的數據,這將會調用所有的延遲加載,因此性能會變得非常糟糕。
鑒于此,這有另外一種方式。
聯合嵌套結果集(Nested Results for Association)
resultMap
一個可以映射聯合嵌套結果集到一個適合的對象視圖上的ResultMap 。這是一個替代的方式去調用另一個select 語句。它允許您去聯合多個表到一個結果集里。這樣的結果集可能包括冗余的、重復的需要分解和正確映射到一個嵌套對象視圖的數據組。簡言之,MyBatis 讓您把結果映射‘鏈接’到一起,用來處理嵌套結果。舉個例子會更好理解,例子在表格下方。
您已經在上面看到了一個非常復雜的嵌套聯合的例子,接下的演示的例子會更簡單一些。我們把Blog和Author表聯接起來查詢,而不是執行分開的查詢語句:
[html] view plain copy print?<select id=“selectBlog” parameterType=“int” resultMap=“blogResult”> select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, A.id as author_id, A.username as author_username, A.password as author_password, A.email as author_email, A.bio as author_bio from Blog B left outer join Author A on B.author_id = A.id where B.id = #{id} </select> <wbr><select id="selectBlog" parameterType="int" resultMap="blogResult">selectB.id as blog_id,B.title as blog_title,B.author_id as blog_author_id,A.id as author_id,A.username as author_username,A.password as author_password,A.email as author_email,A.bio as author_biofrom Blog B left outer join Author A on B.author_id = A.idwhere B.id = #{id}</select>注意到這個連接(join),要確保所有的別名都是唯一且無歧義的。這使映射容易多了,現在我們來映射結果集:
[html] view plain copy print?<resultMap id=“blogResult” type=“Blog”> <id property=”blog_id” column=“id” /> <result property=“title” column=“blog_title”/> <association property=“author” column=“blog_author_id” javaType=“Author” resultMap=”authorResult”/> </resultMap> <resultMap id=“authorResult” type=“Author”> <id property=“id” column=“author_id”/> <result property=“username” column=“author_username”/> <result property=“password” column=“author_password”/> <result property=“email” column=“author_email”/> <result property=“bio” column=“author_bio”/> </resultMap> <wbr><resultMap id="blogResult" type="Blog"><id property=”blog_id” column="id" /><result property="title" column="blog_title"/><association property="author" column="blog_author_id" javaType="Author"resultMap=”authorResult”/></resultMap><resultMap id="authorResult" type="Author"><id property="id" column="author_id"/><result property="username" column="author_username"/><result property="password" column="author_password"/><result property="email" column="author_email"/><result property="bio" column="author_bio"/></resultMap>在上面的例子中,您會看到Blog的作者(“author”)聯合一個“authorResult”結果映射來加載Author實例。
重點提示:id元素在嵌套結果映射中扮演了非常重要的角色,您應該總是指定一個或多個屬性來唯一標識這個結果集。事實上,如果您沒有那樣做,MyBatis也會工作,但是會導致嚴重性能開銷。選擇盡量少的屬性來唯一標識結果,而使用主鍵是最明顯的選擇(即使是復合主鍵)。
上面的例子使用一個擴展的resultMap 元素來聯合映射。這可使Author結果映射可重復使用。然后,如果您不需要重用它,您可以直接嵌套這個聯合結果映射。下面例子就是使用這樣的方式:
[html] view plain copy print?<resultMap id=“blogResult” type=“Blog”> <id property=”blog_id” column=“id” /> <result property=“title” column=“blog_title”/> <association property=“author” column=“blog_author_id” javaType=“Author”> <id property=“id” column=“author_id”/> <result property=“username” column=“author_username”/> <result property=“password” column=“author_password”/> <result property=“email” column=“author_email”/> <result property=“bio” column=“author_bio”/> </association> </resultMap> <wbr><resultMap id="blogResult" type="Blog"><id property=”blog_id” column="id" /><result property="title" column="blog_title"/><association property="author" column="blog_author_id" javaType="Author"><id property="id" column="author_id"/><result property="username" column="author_username"/><result property="password" column="author_password"/><result property="email" column="author_email"/><result property="bio" column="author_bio"/></association></resultMap>在上面的例子中您已經看到如果處理“一對一”(“has one”)類型的聯合查詢。但是對于“一對多”(“has many”)的情況如果處理呢?這個問題在下一節討論。
Collection元素
[html] view plain copy print?<collection property=“posts” ofType=“domain.blog.Post”> <id property=“id” column=“post_id”/> <result property=“subject” column=“post_subject”/> <result property=“body” column=“post_body”/> </collection> <wbr><collection property="posts" ofType="domain.blog.Post"><id property="id" column="post_id"/><result property="subject" column="post_subject"/><result property="body" column="post_body"/></collection>collection元素的作用差不多和association元素的作用一樣。事實上,它們非常相似,以至于再對相似點進行描述會顯得冗余,因此我們只關注它們的不同點。
繼續我們上面的例子,一個Blog只有一個Author。但一個Blog有許多帖子(文章)。在Blog類中,會像下面這樣定義相應屬性:
private List<Post> posts;
映射一個嵌套結果集到一個列表,我們使用collection元素。就像association 元素那樣,我們使用嵌套查詢,或者從連接中嵌套結果集。
集合嵌套選擇(Nested Select for Collection)
首先我們使用嵌套選擇來加載Blog的文章。
[html] view plain copy print?<resultMap id=”blogResult” type=”Blog”> <collection property=“posts” javaType=”ArrayList” column=“blog_id” ofType=“Post” select=”selectPostsForBlog”/> </resultMap> <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”> SELECT * FROM BLOG WHERE ID = #{id} </select> <select id=”selectPostsForBlog” parameterType=”int” resultType=“Author”> SELECT * FROM POST WHERE BLOG_ID = #{id} </select> <wbr><resultMap id=”blogResult” type=”Blog”><collection property="posts" javaType=”ArrayList” column="blog_id"ofType="Post" select=”selectPostsForBlog”/></resultMap><select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>SELECT * FROM BLOG WHERE ID = #{id}</select><select id=”selectPostsForBlog” parameterType=”int” resultType="Author">SELECT * FROM POST WHERE BLOG_ID = #{id}</select>一看上去這有許多東西需要注意,但大部分看起與我們在association元素中學過的相似。首先,您會注意到我們使用了collection元素,然后會注意到一個新的屬性“ofType”。這個元素是用來區別JavaBean屬性(或者字段)類型和集合所包括的類型。因此您會讀到下面這段代碼。
<collection property=”posts” javaType=”ArrayList” column=”blog_id”
ofType=”Post” select=”selectPostsForBlog”/>
è理解為:“一個名為posts,類型為Post的ArrayList集合(A collection of posts in an ArrayList of type Post)” 。
javaType屬性不是必須的,通常MyBatis 會自動識別,所以您通??梢院喡缘貙懗桑?/p>
<collection property=”posts” column=”blog_id” ofType=”Post”
select=”selectPostsForBlog”/>
集合的嵌套結果集(Nested Results for Collection)
這時候,您可能已經猜出嵌套結果集是怎樣工作的了,因為它與association非常相似,只不過多了一個屬性“ofType”。
讓我們看下這個SQL:
[java] view plain copy print?<select id=“selectBlog” parameterType=“int” resultMap=“blogResult”> select B.id as blog_id, B.title as blog_title, B.author_id as blog_author_id, P.id as post_id, P.subject as post_subject, P.body as post_body, from Blog B left outer join Post P on B.id = P.blog_id where B.id = #{id} </select> <wbr><select id="selectBlog" parameterType="int" resultMap="blogResult">selectB.id as blog_id,B.title as blog_title,B.author_id as blog_author_id,P.id as post_id,P.subject as post_subject,P.body as post_body,from Blog Bleft outer join Post P on B.id = P.blog_idwhere B.id = #{id}</select>同樣,我們把Blog和Post兩張表連接在一起,并且也保證列標簽名在映射的時候是唯一且無歧義的?,F在將Blog和Post的集合映射在一起是多么簡單:
[java] view plain copy print?<resultMap id=“blogResult” type=“Blog”> <id property=”id” column=”blog_id” /> <result property=”title” column=“blog_title”/> <collection property=”posts” ofType=“Post”> <id property=”id” column=“post_id”/> <result property=”subject” column=“post_subject”/> <result property=”body” column=“post_body”/> </collection> </resultMap><resultMap id="blogResult" type="Blog"><id property=”id” column="blog_id" /><result property="title" column="blog_title"/><collection property="posts" ofType="Post"><id property="id" column="post_id"/><result property="subject" column="post_subject"/><result property="body" column="post_body"/></collection></resultMap>再次強調一下,id 元素是非常重要的。如果您忘了或者不知道id 元素的作用,請先讀一下上面association 一節。
如果希望結果映射有更好的可重用性,您可以使用下面的方式:
[java] view plain copy print?<resultMap id=“blogResult” type=“Blog”> <id property=”id” column=”blog_id” /> <result property=”title” column=“blog_title”/> <collection property=”posts” ofType=“Post” resultMap=”blogPostResult”/> </resultMap> <resultMap id=”blogPostResult” type=“Post”> <id property=”id” column=“post_id”/> <result property=”subject” column=“post_subject”/> <result property=”body” column=“post_body”/> </resultMap><resultMap id="blogResult" type="Blog"><id property=”id” column="blog_id" /><result property="title" column="blog_title"/><collection property="posts" ofType="Post" resultMap=”blogPostResult”/></resultMap><resultMap id="blogPostResult" type="Post"><id property="id" column="post_id"/><result property="subject" column="post_subject"/><result property="body" column="post_body"/></resultMap>èNote:在您的映射中沒有深度、寬度、聯合和集合數目的限制。但應該謹記,在進行映射的時候也要考慮性能的因素。應用程序的單元測試和性能測試幫助您發現最好的方式可能要花很長時間。但幸運的是,MyBatis允許您以后可以修改您的想法,這時只需要修改少量代碼就行了。
關于高級聯合和集合映射是一個比較深入的課題,文檔只能幫您了解到這里,多做一些實踐,一切將很快變得容易理解。
Discriminator元素
<discriminator javaType=”int” column=”draft”>
<case value=”1” resultType=”DraftPost”/>
</discriminator>
有時候一條數據庫查詢可能會返回包括各種不同的數據類型的結果集。Discriminator(識別器)元素被設計來處理這種情況,以及其它像類繼承層次情況。識別器非常好理解,它就像java里的switch語句。
Discriminator定義要指定column和javaType屬性。列是MyBatis將要取出進行比較的值,javaType用來確定適當的測試是否正確運行(雖然String在大部分情況下都可以工作),例:
[java] view plain copy print?<resultMap id=“vehicleResult” type=“Vehicle”> <id property=”id” column=”id” /> <result property=”vin” column=“vin”/> <result property=”year” column=“year”/> <result property=”make” column=“make”/> <result property=”model” column=“model”/> <result property=”color” column=“color”/> <discriminator javaType=”int” column=“vehicle_type”> <case value=“1” resultMap=“carResult”/> <case value=“2” resultMap=“truckResult”/> <case value=“3” resultMap=“vanResult”/> <case value=“4” resultMap=“suvResult”/> </discriminator> </resultMap> <wbr><resultMap id="vehicleResult" type="Vehicle"><id property=”id” column="id" /><result property="vin" column="vin"/><result property="year" column="year"/><result property="make" column="make"/><result property="model" column="model"/><result property="color" column="color"/><discriminator javaType="int" column="vehicle_type"><case value="1" resultMap="carResult"/><case value="2" resultMap="truckResult"/><case value="3" resultMap="vanResult"/><case value="4" resultMap="suvResult"/></discriminator></resultMap>在這個例子中,MyBatis將會從結果集中取出每條記錄,然后比較它的vehicle type的值。如果匹配任何discriminator中的case,它將使用由case指定的resultMap。這是排它性的,換句話說,其它的case的resultMap將會被忽略(除非使用我們下面說到的extended)。如果沒有匹配到任何case,MyBatis只是簡單的使用定義在discriminator塊外面的resultMap。所以,如果carResult像下面這樣定義:
<resultMap id=”carResult” type=”Car”>
<result property=”doorCount” column=”door_count” />
</resultMap>
那么,只有doorCount屬性會被加載。這樣做是為了與識別器cases群組完全獨立開來,哪怕它與上一層的resultMap 一點關系都沒有。在剛才的例子里我們當然知道cars和vehicles的關系,a Car is-a Vehicle。因此,我們也要把其它屬性加載進來。我們要稍稍改動一下resultMap:
<resultMap id=”carResult” type=”Car”extends=”vehicleResult”>
<result property=”doorCount” column=”door_count” />
</resultMap>
現在,vehicleResult和carResult的所有屬性都會被加載。
可能有人會認為這樣擴展映射定義有一點單調了,所以還有一種可選的更加簡單明了的映射風格語法。例如:
[java] view plain copy print?<resultMap id=“vehicleResult” type=“Vehicle”> <id property=”id” column=”id” /> <result property=”vin” column=“vin”/> <result property=”year” column=“year”/> <result property=”make” column=“make”/> <result property=”model” column=“model”/> <result property=”color” column=“color”/> <discriminator javaType=”int” column=“vehicle_type”> <case value=“1” resultType=“carResult”> <result property=”doorCount” column=”door_count” /> </case> <case value=“2” resultType=“truckResult”> <result property=”boxSize” column=”box_size” /> <result property=”extendedCab” column=”extended_cab” /> </case> <case value=“3” resultType=“vanResult”> <result property=”powerSlidingDoor” column=”power_sliding_door” /> </case> <case value=“4” resultType=“suvResult”> <result property=”allWheelDrive” column=”all_wheel_drive” /> </case> </discriminator> </resultMap> <wbr><resultMap id="vehicleResult" type="Vehicle"><id property=”id” column="id" /><result property="vin" column="vin"/><result property="year" column="year"/><result property="make" column="make"/><result property="model" column="model"/><result property="color" column="color"/><discriminator javaType="int" column="vehicle_type"><case value="1" resultType="carResult"><result property=”doorCount” column="door_count" /></case><case value="2" resultType="truckResult"><result property=”boxSize” column="box_size" /><result property=”extendedCab” column="extended_cab" /></case><case value="3" resultType="vanResult"><result property=”powerSlidingDoor” column="power_sliding_door" /></case><case value="4" resultType="suvResult"><result property=”allWheelDrive” column="all_wheel_drive" /></case></discriminator></resultMap>è記?。?/strong>對于這么多的結果映射,如果您不指定任何的結果集,那么MyBatis 會自動地將列名與屬性相匹配。所以上面所舉的例子比實際中需要的要詳細。盡管如此,大部分數據庫有點復雜,并且它并不是所有情況都是完全可以適用的。
Cache元素
MyBatis包含一個強大的、可配置、可定制的查詢緩存機制。MyBatis 3 的緩存實現有了許多改進,使它更強大更容易配置。默認的情況,緩存是沒有開啟,除了會話緩存以外,它可以提高性能,且能解決循環依賴。開啟二級緩存,您只需要在SQL映射文件中加入簡單的一行:
<cache/>
這句簡單的語句作用如下:
·所有映射文件里的select語句的結果都會被緩存。
·所有映射文件里的insert、update和delete語句執行都會清空緩存。
·緩存使用最近最少使用算法(LRU)來回收。
·緩存不會被設定的時間所清空。
·每個緩存可以存儲1024 個列表或對象的引用(不管查詢方法返回的是什么)。
·緩存將作為“讀/寫”緩存,意味著檢索的對象不是共享的且可以被調用者安全地修改,而不會被其它調用者或者線程干擾。
所有這些特性都可以通過cache元素進行修改。例如:
<cache
eviction=”FIFO”
flushInterval=”60000”
size=”512”
readOnly=”true”/>
這種高級的配置創建一個每60秒刷新一次的FIFO 緩存,存儲512個結果對象或列表的引用,并且返回的對象是只讀的。因此在不用的線程里的調用者修改它們可能會引用沖突。
可用的回收算法如下:
·LRU–最近最少使用:移出最近最長時間內都沒有被使用的對象。
·FIFO–先進先出:移除最先進入緩存的對象。
·SOFT–軟引用: 基于垃圾回收機制和軟引用規則來移除對象(空間內存不足時才進行回收)。
·WEAK–弱引用:基于垃圾回收機制和弱引用規則(垃圾回收器掃描到時即進行回收)。
默認使用LRU。
flushInterval:設置任何正整數,代表一個以毫秒為單位的合理時間。默認是沒有設置,因此沒有刷新間隔時間被使用,在語句每次調用時才進行刷新。
Size:屬性可以設置為一個正整數,您需要留意您要緩存對象的大小和環境中可用的內存空間。默認是1024。
readOnly:屬性可以被設置為true 或false。只讀緩存將對所有調用者返回同一個實例。因此這些對象都不能被修改,這可以極大的提高性能??蓪懙木彺鎸⑼ㄟ^序列化來返回一個緩存對象的拷貝。這會比較慢,但是比較安全。所以默認值是false。
使用自定義緩存
除了上面已經定義好的緩存方式,您能夠通過您自己的緩存實現來完全重寫緩存行為,或者通過創建第三方緩存解決方案的適配器。
<cache type=”com.domain.something.MyCustomCache”/>
這個例子演示了如果自定義緩存實現。由type指定的類必須實現org.mybatis.cache.Cache接口。這個接口是MyBatis框架比較復雜的接口之一,先給個示例:
public interface Cache {
String getId();
int getSize();
void putObject(Object key, Object value);
Object getObject(Object key);
boolean hasKey(Object key);
Object removeObject(Object key);
void clear();
ReadWriteLock getReadWriteLock();
}
要配置您的緩存,簡單地添加一個公共的JavaBeans 屬性到您的緩存實現中,然后通過cache 元素設置屬性進行傳遞,下面示例,將在您的緩存實現上調用一個setCacheFile(String file)方法。
<cache type=”com.domain.something.MyCustomCache”>
<property name=”cacheFile” value=”/tmp/my-custom-cache.tmp”/>
</cache>
您可以使用所有簡單的JavaBeans屬性,MyBatis會自動進行轉換。
需要牢記的是一個緩存配置和緩存實例都綁定到一個SQL Map 文件命名空間。因此,所有的這個相同命名空間的語句也都和這個緩存綁定。語句可以修改如何與這個緩存相匹配,或者使用兩個簡單的屬性來完全排除它們自己。默認情況下,語句像下面這樣來配置:
<select … flushCache=”false” useCache=”true”/>
<insert … flushCache=”true”/>
<update … flushCache=”true”/>
<delete … flushCache=”true”/>
因為有默認值,所以您不需要使用這種方式明確地配置這些語句。如果您想改變默認的動作,只需要設置flushCache和useCache 屬性即可。舉個例子來說,在許多的場合下您可能排除緩存中某些特定的select語句?;蛘吣胗胹elect語句清空緩存。同樣的,您也可能有一些update 語句在執行的時候不需要清空緩存。
cache-ref元素
回想上一節,我們僅僅只是討論在某一個命名空間里使用或者刷新緩存。但有可能您想要在不同的命名空間里共享同一個緩存配置或者實例。在這種情況下,您就可以使用cache-ref 元素來引用另外一個緩存。
<cache-ref namespace=”com.someone.application.data.SomeMapper”/>
新聞熱點
疑難解答