Mybatis 高級結果映射Association Collection Cache-ref Discriminator Cache

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。聯合映射與其它的結果集映射工作方式差不多,指定propertycolumnjavaType(通常MyBatis會自動識別)、jdbcType(如果需要)、typeHandler

不同的地方是您需要告訴MyBatis 如何加載一個聯合查詢。MyBatis使用兩種方式來加載:

·Nested Select:通過執行另一個返回預期複雜類型的映射SQL語句(即引用外部定義好的SQL語句塊)。

·Nested Results:通過嵌套結果映射(nested result mappings)來處理聯接結果集(joined results)的重複子集。

首先,讓我們檢查一下元素屬性。正如您看到的,它不同於普通只有selectresultMap屬性的結果映射。

Attribute

Description

property

映射數據庫列的字段或屬性。如果JavaBean 的屬性與給定的名稱匹配,就會使用匹配的名字。否則,MyBatis 將搜索給定名稱的字段。兩種情況下您都可以使用逗點的屬性形式。比如,您可以映射到”username”,也可以映射到更復雜點的”address.street.number”

column

數據庫的列名或者列標籤別名。與傳遞給resultSet.getString(columnName)的參數名稱相同。

注意: 在處理組合鍵時,您可以使用column= “{prop1=col1,prop2=col2}”這樣的語法,設置多個列名傳入到嵌套查詢語句。這就會把prop1prop2設置到目標嵌套選擇語句的參數對象中。

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}”這樣的語法,設置多個列名傳入到嵌套語句。這就會把prop1prop2設置到目標嵌套語句的參數對象中。

 例如:  

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <resultMap id=”blogResult” type=”Blog”>  
  2. <association property="author" column="blog_author_id" javaType="Author"  
  3. select=”selectAuthor”/>  
  4. </resultMap>  
  5.    
  6. <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>  
  7. SELECT * FROM BLOG WHERE ID = #{id}  
  8. </select>  
  9.    
  10. <select id=”selectAuthor” parameterType=”int” resultType="Author">  
  11. SELECT * FROM AUTHOR WHERE ID = #{id}  
  12. </select>  
  13. <wbr>  

我們使用兩個select語句:一個用來加載Blog,另一個用來加載AuthorBlogresultMap 描述了使用“selectAuthor”語句來加載author的屬性。

如果列名和屬性名稱相匹配的話,所有匹配的屬性都會自動加載。

 

譯者注:

上面的例子,首先執行<select id=selectBlog”>,執行結果存放到<resultMap id=blogResult”>結果映射中。“blogResult”是一個Blog類型,從<select id=selectBlog”>查出的數據都會自動賦值給”blogResult”的與列名匹配的屬性,這時blog_idtitle等就被賦值了。同時“blogResult”還有一個關聯屬性"Author",執行嵌套查詢select=”selectAuthor”後,Author對象的屬性idusernamepasswordemailbio也被賦於數據庫匹配的值。

 

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 讓您把結果映射‘鏈接’到一起,用來處理嵌套結果。舉個例子會更好理解,例子在表格下方。

您已經在上面看到了一個非常複雜的嵌套聯合的例子,接下的演示的例子會更簡單一些。我們把BlogAuthor表聯接起來查詢,而不是執行分開的查詢語句:

[sql] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <select id="selectBlog" parameterType="int" resultMap="blogResult">  
  2. select  
  3. B.id as blog_id,  
  4. B.title as blog_title,  
  5. B.author_id as blog_author_id,  
  6. A.id as author_id,  
  7. A.username as author_username,  
  8. A.password as author_password,  
  9. A.email as author_email,  
  10. A.bio as author_bio  
  11. from Blog B left outer join Author A on B.author_id = A.id  
  12. where B.id = #{id}  
  13. </select>  

注意到這個連接(join),要確保所有的別名都是唯一且無歧義的。這使映射容易多了,現在我們來映射結果集:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <resultMap id="blogResult" type="Blog">  
  2. <id property=”blog_id” column="id" />  
  3. <result property="title" column="blog_title"/>  
  4. <association property="author" column="blog_author_id" javaType="Author"  
  5. resultMap=”authorResult”/>  
  6. </resultMap>  
  7.    
  8. <resultMap id="authorResult" type="Author">  
  9. <id property="id" column="author_id"/>  
  10. <result property="username" column="author_username"/>  
  11. <result property="password" column="author_password"/>  
  12. <result property="email" column="author_email"/>  
  13. <result property="bio" column="author_bio"/>  
  14. </resultMap>  

在上面的例子中,您會看到Blog的作者(“author”)聯合一個“authorResult”結果映射來加載Author實例。

重點提示:id元素在嵌套結果映射中扮演了非常重要的角色,您應該總是指定一個或多個屬性來唯一標識這個結果集。事實上,如果您沒有那樣做,MyBatis也會工作,但是會導致嚴重性能開銷。選擇儘量少的屬性來唯一標識結果,而使用主鍵是最明顯的選擇(即使是複合主鍵)。

上面的例子使用一個擴展的resultMap 元素來聯合映射。這可使Author結果映射可重複使用。然後,如果您不需要重用它,您可以直接嵌套這個聯合結果映射。下面例子就是使用這樣的方式: 

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <resultMap id="blogResult" type="Blog">  
  2. <id property=”blog_id” column="id" />  
  3. <result property="title" column="blog_title"/>  
  4. <association property="author" column="blog_author_id" javaType="Author">  
  5. <id property="id" column="author_id"/>  
  6. <result property="username" column="author_username"/>  
  7. <result property="password" column="author_password"/>  
  8. <result property="email" column="author_email"/>  
  9. <result property="bio" column="author_bio"/>  
  10. </association>  
  11. </resultMap>  

在上面的例子中您已經看到如果處理“一對一”(“has one”)類型的聯合查詢。但是對於“一對多”(“has many”)的情況如果處理呢?這個問題在下一節討論。

Collection元素 

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <collection property="posts" ofType="domain.blog.Post">  
  2. <id property="id" column="post_id"/>  
  3. <result property="subject" column="post_subject"/>  
  4. <result property="body" column="post_body"/>  
  5. </collection>  

collection元素的作用差不多和association元素的作用一樣。事實上,它們非常相似,以至於再對相似點進行描述會顯得冗餘,因此我們只關注它們的不同點。

繼續我們上面的例子,一個Blog只有一個Author。但一個Blog有許多帖子(文章)。在Blog類中,會像下面這樣定義相應屬性: 

private List<Post> posts;

映射一個嵌套結果集到一個列表,我們使用collection元素。就像association 元素那樣,我們使用嵌套查詢,或者從連接中嵌套結果集。 

集合嵌套選擇(Nested Select for Collection

首先我們使用嵌套選擇來加載Blog的文章。 

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <resultMap id=”blogResult” type=”Blog”>  
  2. <collection property="posts" javaType=”ArrayList” column="blog_id"  
  3. ofType="Post" select=”selectPostsForBlog”/>  
  4. </resultMap>  
  5.    
  6. <select id=”selectBlog” parameterType=”int” resultMap=”blogResult”>  
  7. SELECT * FROM BLOG WHERE ID = #{id}  
  8. </select>  
  9.    
  10. <select id=”selectPostsForBlog” parameterType=”int” resultType="Author">  
  11. SELECT * FROM POST WHERE BLOG_ID = #{id}  
  12. </select>  

一看上去這有許多東西需要注意,但大部分看起與我們在association元素中學過的相似。首先,您會注意到我們使用了collection元素,然後會注意到一個新的屬性“ofType”。這個元素是用來區別JavaBean屬性(或者字段)類型和集合所包括的類型。因此您會讀到下面這段代碼。

 

<collection property="posts" javaType=”ArrayList” column="blog_id"

ofType="Post" select=”selectPostsForBlog”/>

è理解爲:“一個名爲posts,類型爲PostArrayList集合(A collection of posts in an ArrayList of type Post)” 。

javaType屬性不是必須的,通常MyBatis 會自動識別,所以您通常可以簡略地寫成:

<collection property="posts" column="blog_id" ofType="Post"

select=”selectPostsForBlog”/>



集合的嵌套結果集(Nested Results for Collection

這時候,您可能已經猜出嵌套結果集是怎樣工作的了,因爲它與association非常相似,只不過多了一個屬性“ofType”

讓我們看下這個SQL 

[sql] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <select id="selectBlog" parameterType="int" resultMap="blogResult">  
  2. select  
  3. B.id as blog_id,  
  4. B.title as blog_title,  
  5. B.author_id as blog_author_id,  
  6. P.id as post_id,  
  7. P.subject as post_subject,  
  8. P.body as post_body,  
  9. from Blog B  
  10. left outer join Post P on B.id = P.blog_id  
  11. where B.id = #{id}  
  12. </select>  

同樣,我們把BlogPost兩張表連接在一起,並且也保證列標籤名在映射的時候是唯一且無歧義的。現在將BlogPost的集合映射在一起是多麼簡單:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <resultMap id="blogResult" type="Blog">  
  2. <id property=”id” column="blog_id" />  
  3. <result property="title" column="blog_title"/>  
  4. <collection property="posts" ofType="Post">  
  5. <id property="id" column="post_id"/>  
  6. <result property="subject" column="post_subject"/>  
  7. <result property="body" column="post_body"/>  
  8. </collection>  
  9. </resultMap>  

再次強調一下,id 元素是非常重要的。如果您忘了或者不知道id 元素的作用,請先讀一下上面association一節。

如果希望結果映射有更好的可重用性,您可以使用下面的方式:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <resultMap id="blogResult" type="Blog">  
  2. <id property=”id” column="blog_id" />  
  3. <result property="title" column="blog_title"/>  
  4. <collection property="posts" ofType="Post" resultMap=”blogPostResult”/>  
  5. </resultMap>  
  6.    
  7. <resultMap id="blogPostResult" type="Post">  
  8. <id property="id" column="post_id"/>  
  9. <result property="subject" column="post_subject"/>  
  10. <result property="body" column="post_body"/>  
  11. </resultMap>  

èNote:在您的映射中沒有深度、寬度、聯合和集合數目的限制。但應該謹記,在進行映射的時候也要考慮性能的因素。應用程序的單元測試和性能測試幫助您發現最好的方式可能要花很長時間。但幸運的是,MyBatis允許您以後可以修改您的想法,這時只需要修改少量代碼就行了。

關於高級聯合和集合映射是一個比較深入的課題,文檔只能幫您瞭解到這裏,多做一些實踐,一切將很快變得容易理解。


Discriminator元素

<discriminator javaType="int" column="draft">

<case value="1" resultType="DraftPost"/>

</discriminator>

 

有時候一條數據庫查詢可能會返回包括各種不同的數據類型的結果集。Discriminator(識別器)元素被設計來處理這種情況,以及其它像類繼承層次情況。識別器非常好理解,它就像java裏的switch語句。

 

Discriminator定義要指定columnjavaType屬性。列是MyBatis將要取出進行比較的值,javaType用來確定適當的測試是否正確運行(雖然String在大部分情況下都可以工作),例:  

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <resultMap id="vehicleResult" type="Vehicle">  
  2. <id property=”id” column="id" />  
  3. <result property="vin" column="vin"/>  
  4. <result property="year" column="year"/>  
  5. <result property="make" column="make"/>  
  6. <result property="model" column="model"/>  
  7. <result property="color" column="color"/>  
  8. <discriminator javaType="int" column="vehicle_type">  
  9. <case value="1" resultMap="carResult"/>  
  10. <case value="2" resultMap="truckResult"/>  
  11. <case value="3" resultMap="vanResult"/>  
  12. <case value="4" resultMap="suvResult"/>  
  13. </discriminator>  
  14. </resultMap>  

在這個例子中,MyBatis將會從結果集中取出每條記錄,然後比較它的vehicle type的值。如果匹配任何discriminator中的case,它將使用由case指定的resultMap。這是排它性的,換句話說,其它的caseresultMap將會被忽略(除非使用我們下面說到的extended)。如果沒有匹配到任何caseMyBatis只是簡單的使用定義在discriminator塊外面的resultMap。所以,如果carResult像下面這樣定義:

 

<resultMap id="carResult" type="Car">

<result property=”doorCount” column="door_count" />

</resultMap>

 

那麼,只有doorCount屬性會被加載。這樣做是爲了與識別器cases羣組完全獨立開來,哪怕它與上一層的resultMap一點關係都沒有。在剛纔的例子裏我們當然知道carsvehicles的關係,a Car is-a Vehicle。因此,我們也要把其它屬性加載進來。我們要稍稍改動一下resultMap

 

<resultMap id="carResult" type="Car"extends=”vehicleResult”>

<result property=”doorCount” column="door_count" />

</resultMap>

 

現在,vehicleResultcarResult的所有屬性都會被加載。

可能有人會認爲這樣擴展映射定義有一點單調了,所以還有一種可選的更加簡單明瞭的映射風格語法。例如:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <resultMap id="vehicleResult" type="Vehicle">  
  2. <id property=”id” column="id" />  
  3. <result property="vin" column="vin"/>  
  4. <result property="year" column="year"/>  
  5. <result property="make" column="make"/>  
  6. <result property="model" column="model"/>  
  7. <result property="color" column="color"/>  
  8. <discriminator javaType="int" column="vehicle_type">  
  9. <case value="1" resultType="carResult">  
  10. <result property=”doorCount” column="door_count" />  
  11. </case>  
  12. <case value="2" resultType="truckResult">  
  13. <result property=”boxSize” column="box_size" />  
  14. <result property=”extendedCab” column="extended_cab" />  
  15. </case>  
  16. <case value="3" resultType="vanResult">  
  17. <result property=”powerSlidingDoor” column="power_sliding_door" />  
  18. </case>  
  19. <case value="4" resultType="suvResult">  
  20. <result property=”allWheelDrive” column="all_wheel_drive" />  
  21. </case>  
  22. </discriminator>  
  23. </resultMap>  

è記住:對於這麼多的結果映射,如果您不指定任何的結果集,那麼MyBatis 會自動地將列名與屬性相匹配。所以上面所舉的例子比實際中需要的要詳細。儘管如此,大部分數據庫有點複雜,並且它並不是所有情況都是完全可以適用的。

Cache元素

MyBatis包含一個強大的、可配置、可定製的查詢緩存機制。MyBatis 3 的緩存實現有了許多改進,使它更強大更容易配置。默認的情況,緩存是沒有開啓,除了會話緩存以外,它可以提高性能,且能解決循環依賴。開啓二級緩存,您只需要在SQL映射文件中加入簡單的一行:

 

<cache/>

 

這句簡單的語句作用如下:

·所有映射文件裏的select語句的結果都會被緩存。

·所有映射文件裏的insertupdatedelete語句執行都會清空緩存

·緩存使用最近最少使用算法(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框架比較複雜的接口之一,先給個示例:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public interface Cache {  
  2. String getId();  
  3. int getSize();  
  4. void putObject(Object key, Object value);  
  5. Object getObject(Object key);  
  6. boolean hasKey(Object key);  
  7. Object removeObject(Object key);  
  8. void clear();  
  9. ReadWriteLock getReadWriteLock();  
  10. }  

要配置您的緩存,簡單地添加一個公共的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”/> 

因爲有默認值,所以您不需要使用這種方式明確地配置這些語句。如果您想改變默認的動作,只需要設置flushCacheuseCache 屬性即可。舉個例子來說,在許多的場合下您可能排除緩存中某些特定的select語句。或者您想用select語句清空緩存。同樣的,您也可能有一些update 語句在執行的時候不需要清空緩存。

cache-ref元素

回想上一節,我們僅僅只是討論在某一個命名空間裏使用或者刷新緩存。但有可能您想要在不同的命名空間裏共享同一個緩存配置或者實例。在這種情況下,您就可以使用cache-ref 元素來引用另外一個緩存。

<cache-ref namespace=”com.someone.application.data.SomeMapper”/>


例子:

接下來看下一個完整的sqlmap可以運行的ibatis文件:

[html] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. <?xml version="1.0" encoding="UTF-8" ?>  
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">  
  3. <mapper namespace="com.chudong.xy.dao.item.IItemDao">  
  4.   
  5.     <resultMap type="Item" id="itemResultMap" >  
  6.         <id property="id" column="id" />  
  7.         <result property="numIid" column="num_iid" />  
  8.         <result property="title" column="title" />  
  9.         <result property="subTitle" column="sub_title" />  
  10.         <result property="cat" column="cat" />  
  11.         <result property="picUrl" column="pic_url" />  
  12.         <result property="picThumUrl" column="pic_thum_url" />  
  13.         <result property="outerId" column="outer_id" />  
  14.         <result property="props" column="props" />      
  15.         <result property="marketPrice" column="market_price" />     
  16.         <result property="price" column="price" />      
  17.         <result property="num" column="num" />      
  18.         <result property="listTime" column="list_time" />   
  19.         <result property="delistTime" column="delist_time" />   
  20.         <result property="sales" column="sales" />      
  21.         <result property="created" column="created" />      
  22.         <result property="modified" column="modified" />    
  23.         <result property="enableStatus" column="enable_status" />   
  24.     </resultMap>  
  25.       
  26.     <!-- 與活動和搭配套餐進行關聯 -->  
  27.     <resultMap type="Item" id="itemDetailResultMap" extends="itemResultMap">  
  28.         <association property="active" column="id" javaType="com.chudong.xy.domain.active.Active" select="selectActive"/>  
  29.         <collection property="withPackages" column="id" javaType="java.util.ArrayList" ofType="com.chudong.xy.domain.item.Item" select="selectWithPackages"/>    
  30.     </resultMap>  
  31.       
  32.     <resultMap type="Active" id="activeResultMap">  
  33.         <id property="id" column="id" />  
  34.         <result property="title" column="title" />  
  35.         <result property="picUrl" column="pic_url" />  
  36.     </resultMap>  
  37.   
  38.     <!-- 查詢單個商品,並裝載活動和搭配套餐 -->  
  39.     <select id="queryDetailById" parameterType="long" resultMap="itemDetailResultMap">  
  40.         SELECT * from item where id = #{id} and enable_status = 1  
  41.     </select>  
  42.       
  43.     <!-- 根據商品id得到一個活動 -->  
  44.     <select id="selectActive" parameterType="long" resultMap="activeResultMap">    
  45.         select id,title,pic_url from active where id = (select active_id from active_item where item_id = #{id} and enable_status=1 limit 0,1)   
  46.     </select>  
  47.     <!-- 根據商品id得到搭配套餐中的商品 -->  
  48.     <select id="selectWithPackages" parameterType="long" resultMap="itemResultMap">  
  49.         select * from item where id in(select mapping_id from item_relation where item_id = #{id} and type=1 and enable_status=1)  
  50.     </select>  
  51.       
  52. </mapper>  

java類:

[java] view plain copy
 在CODE上查看代碼片派生到我的代碼片
  1. public class Item implements Serializable {  
  2.   
  3.     /** 
  4.      *  
  5.      */  
  6.     private static final long serialVersionUID = 3969923837162162882L;  
  7.   
  8.     /** 
  9.      *  
  10.      */  
  11.     private Long id;  
  12.       
  13.     /** 
  14.      * 商品編碼 
  15.      */  
  16.     private Long numIid;  
  17.       
  18.     /** 
  19.      * 商品標題 
  20.      */  
  21.     private String title;  
  22.       
  23.     /** 
  24.      * 副標題 
  25.      */  
  26.     private String subTitle;  
  27.       
  28.     /** 
  29.      * 商品類目, 
  30.      */  
  31.     private Integer cat;  
  32.       
  33.     /** 
  34.      * 商品主圖地址 
  35.      */  
  36.     private String picUrl;  
  37.       
  38.     /** 
  39.      * 商品縮略圖 
  40.      */  
  41.     private String picThumUrl;  
  42.       
  43.     /** 
  44.      * 商家編碼, 
  45.      */  
  46.     private String outerId;  
  47.       
  48.     /** 
  49.      * 商品屬性 
  50.      */  
  51.     private String props;  
  52.       
  53.     /** 
  54.      * 銷售價格 
  55.      */  
  56.     private Double price;  
  57.       
  58.     /** 
  59.      * 商品的市場價格 
  60.      */  
  61.     private Double marketPrice;  
  62.       
  63.     /** 
  64.      * 商品數量 
  65.      */  
  66.     private Integer num;  
  67.       
  68.     /** 
  69.      * 商品上架時間 
  70.      */  
  71.     private Date listTime;  
  72.       
  73.     /** 
  74.      * 商品下架時間 
  75.      */  
  76.     private Date delistTime;  
  77.       
  78.     /** 
  79.      * 商品銷量 
  80.      */  
  81.     private Integer sales;    
  82.   
  83.     private Date created;  
  84.       
  85.     private Date modified;  
  86.       
  87.     /** 
  88.      * 數據可用狀態,0表示不可用,1表示可用 
  89.      */  
  90.     private Integer enableStatus;  
  91.       
  92.     /** 
  93.      * 商品的url,不存儲數據庫 
  94.      */  
  95.     private String itemTaobaoUrl;  
  96.       
  97.     /** 
  98.      * 商品所關聯的活動 
  99.      */  
  100.     private Active active;  
  101.       
  102.     /** 
  103.      * 該商品的搭配套餐 
  104.      */  
  105.     private List<Item> withPackages;  
  106.   
  107.     //省略了set get方法  
  108. }  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章