<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    閑人野居
    好好學(xué)習(xí),天天向上
    posts - 57,  comments - 137,  trackbacks - 0
    ??? hibernate 的強(qiáng)大在于完全的對(duì)象化,對(duì)于對(duì)象之間的關(guān)系解決的比較好,如1對(duì)1,1對(duì)多,多對(duì)1,以及多對(duì)多。當(dāng)然也包括繼承關(guān)系。
    ??? 而ibatis這方面就比較遜色了,不過對(duì)于也支持簡(jiǎn)單的關(guān)連查詢,如1對(duì)1,和1對(duì)多。對(duì)于一般的情況來說,這兩種已經(jīng)足夠了,當(dāng)然不能層疊更新是一個(gè)缺陷,看了半天文檔,也沒有找到對(duì)象之間的層疊更新,估計(jì)是不支持。
    ??? 以前的版本ibatis處理關(guān)連是通過執(zhí)行兩次sql來實(shí)現(xiàn)的,如下的實(shí)例:
    ??? 一對(duì)多關(guān)聯(lián):
    ?

    <sqlMap namespace="User">
    <typeAlias alias="user" type="com.ibatis.sample.User"/>
    <typeAlias alias="address" type="com.ibatis.sample.Address"/>
    <resultMap id="get-user-result" class="user">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="sex" column="sex"/>
    <result property="addresses" column="id" select="User.getAddressByUserId"/>
    </resultMap>
    <select id="getUsers" parameterClass="java.lang.String" resultMap="get-user-result">
    <![CDATA[
    select id,name,sex
    from t_user
    where id = #id#
    ]]>
    </select>
    <select id="getAddressByUserId" parameterClass="int" resultClass="address">
    <![CDATA[
    select address,zipcode
    from t_address
    where user_id = #userid#
    ]]>
    </select>
    </sqlMap>?? ?


    這里通過在resultMap 中定義嵌套查詢getAddressByUserId,我們實(shí)現(xiàn)了關(guān)聯(lián)數(shù)據(jù)的讀取。
    需要注意的是,這里有一個(gè)潛在的性能問題,也就是所謂“n+1”Select問題。
    一對(duì)一關(guān)聯(lián):
    對(duì)于這種情況,我們可以采用一次Select兩張表的方式,避免這樣的性能開銷(假設(shè)上面示例中,每個(gè)User 只有一個(gè)對(duì)應(yīng)的Address記錄):

    ?
    <resultMap id="get-user-result" class="user">
    <result property="id" column="id"/>
    <result property="name" column="name"/>
    <result property="sex" column="sex"/>
    <result property="address" column="t_address.address"/>
    <result property="zipCode" column="t_address.zipcode"/>
    </resultMap>
    <select id="getUsers" parameterClass="java.lang.String" resultMap="get-user-result">
    <![CDATA[
    select *
    from t_user,t_address
    where t_user.id=t_address.user_id
    ]]>
    </select>
    ??? ?


    在現(xiàn)在的版本中,對(duì)于n+1問題,ibatis已經(jīng)很好的解決了。如下的配置:
    一對(duì)一
    ?

    <resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
    <result property=”id” column=”PRD_ID”/>
    <result property=”description” column=”PRD_DESCRIPTION”/>
    <result property=”category” resultMap=“get-category-result” />
    </resultMap>
    <resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
    <result property=”id” column=”CAT_ID” />
    <result property=”description” column=”CAT_DESCRIPTION” />
    </resultMap>
    <select id=”getProduct” parameterClass=”int” resultMap=”get-product-result”>
    select *
    from PRODUCT, CATEGORY
    where PRD_CAT_ID=CAT_ID
    and PRD_ID = #value#
    </select>?? ?

    可以使用內(nèi)在的resultMap來解決此問題。
    同樣一對(duì)多如下:

    ?
    <sqlMap namespace="ProductCategory">
    <resultMap id=”categoryResult” class=”com.ibatis.example.Category” groupBy=”id”>
    <result property=”id” column=”CAT_ID”/>
    <result property=”description” column=”CAT_DESCRIPTION”/>
    <result property=”productList” resultMap=”ProductCategory.productResult”/>
    </resultMap>
    <resultMap id=”productResult” class=”com.ibatis.example.Product”>
    <result property=”id” column=”PRD_ID”/>
    <result property=”description” column=”PRD_DESCRIPTION”/>
    </resultMap>
    <select id=”getCategory” parameterClass=”int” resultMap=”categoryResult”>
    select C.CAT_ID, C.CAT_DESCRIPTION, P.PRD_ID, P.PRD_DESCRIPTION
    from CATEGORY C
    left outer join PRODUCT P
    on C.CAT_ID = P.PRD_CAT_ID
    where CAT_ID = #value#
    </select>
    </sqlMap>?? ?


    注意,需要使用增加groupBy屬性來分類

    posted on 2007-01-16 16:22 布衣郎 閱讀(5488) 評(píng)論(8)  編輯  收藏 所屬分類: orm

    FeedBack:
    # re: ibatis 對(duì)象關(guān)系實(shí)現(xiàn)
    2007-01-17 09:49 | 實(shí)用主義
    ibatis 最主要的作用是什么,LZ還不知道呀,不要白讀書呀  回復(fù)  更多評(píng)論
      
    # re: ibatis 對(duì)象關(guān)系實(shí)現(xiàn)
    2007-01-17 10:41 | 布衣郎
    @實(shí)用主義
    既然是半成品的orm,當(dāng)然不能完全只是依賴于sql和存儲(chǔ)過程,那還不如直接去用jdbc了。  回復(fù)  更多評(píng)論
      
    # re: ibatis 對(duì)象關(guān)系實(shí)現(xiàn)
    2007-05-22 10:28 | murphy
    你好,我在做ibatis時(shí)使用你所提到的方法,但是出現(xiàn)了問題,提示是無法找到類似你提到的1:1關(guān)系中的get-category-result,不知道是什么原因呢?

    附:
    --- The error happened while setting a property on the result object.
    --- Cause: com.ibatis.sqlmap.client.SqlMapException: There is no result map named getPartByID in this SqlMap.
      回復(fù)  更多評(píng)論
      
    # re: ibatis 對(duì)象關(guān)系實(shí)現(xiàn)
    2007-05-22 15:35 | 布衣郎
    @murphy
    你沒有定義getPartByID這個(gè)SqlMap,仔細(xì)看看你的配置文件  回復(fù)  更多評(píng)論
      
    # re: ibatis 對(duì)象關(guān)系實(shí)現(xiàn)
    2007-05-23 10:12 | murphy
    你好,已經(jīng)配置了,就是因?yàn)榕渲昧诉€不行啊.
    附:
    <resultMap id="getPartByID" class="com.****.domain.Part">
    <result property="part_ID" column="part_ID"/>
    <result property="part_Code" column="part_Code"/>
    <result property="part_Name" column="part_Name"/>
    </resultMap>

    ---------------------------------------------------------------
    <resultMap id="pdpList" class="pdp">
    <result property="pdp_ID" column="pdp_ID"/>
    <result property="pdp_Code" column="pdp_Code"/>
    <result property="pdp_Charge" column="pdp_Charge"/>
    <result property="part" resultMap="getPartByID"/>
    .............................
    </resultMap>   回復(fù)  更多評(píng)論
      
    # re: ibatis 對(duì)象關(guān)系實(shí)現(xiàn)
    2007-05-23 14:49 | 布衣郎
    你的Part中的屬性也是part_ID,part_Code,而不是字段名稱嗎?  回復(fù)  更多評(píng)論
      
    # re: ibatis 對(duì)象關(guān)系實(shí)現(xiàn)
    2007-05-25 11:33 | murphy
    Part中屬性和數(shù)據(jù)庫(kù)中的字段是基本對(duì)應(yīng)的,這和找不到Map有什么直接關(guān)系嗎?  回復(fù)  更多評(píng)論
      
    # re: ibatis 對(duì)象關(guān)系實(shí)現(xiàn)
    2007-05-25 15:40 | 布衣郎
    有可能,主要的問題在于part_ID這個(gè)命名,以前beanutils對(duì)于兩個(gè)連寫的大寫字符有個(gè)小bug,解析不了。要不換換看看。  回復(fù)  更多評(píng)論
      

    只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


    網(wǎng)站導(dǎo)航:
     

    <2007年1月>
    31123456
    78910111213
    14151617181920
    21222324252627
    28293031123
    45678910

    常用鏈接

    留言簿(12)

    隨筆分類(59)

    隨筆檔案(57)

    blog

    java

    uml

    搜索

    •  

    積分與排名

    • 積分 - 357265
    • 排名 - 155

    最新評(píng)論

    閱讀排行榜

    評(píng)論排行榜

    主站蜘蛛池模板: 波多野结衣在线免费视频 | 免费真实播放国产乱子伦| 亚洲乱码一二三四区国产| 无码人妻久久一区二区三区免费 | 久久国产免费一区| 久久亚洲一区二区| 中文字幕在线观看免费视频| 精品亚洲aⅴ在线观看| 99re免费99re在线视频手机版| 亚洲色大成网站www永久| 67pao强力打造国产免费| 久久久久精品国产亚洲AV无码| 成年女人免费视频播放77777| 亚洲人成电影网站色www| 国产午夜无码视频免费网站| 一级白嫩美女毛片免费| 亚洲色中文字幕无码AV| 日韩精品内射视频免费观看| 亚洲国产日产无码精品| 精品少妇人妻AV免费久久洗澡| 亚洲精品国产首次亮相| 亚洲男人天堂2020| 午夜影院免费观看| 亚洲熟妇无码AV不卡在线播放 | 国内成人精品亚洲日本语音| 久久久久久亚洲精品不卡| 国产免费无码AV片在线观看不卡| 精品亚洲aⅴ在线观看| 黄a大片av永久免费| 久久久久久av无码免费看大片| 亚洲a在线视频视频| 午夜免费不卡毛片完整版| 一个人晚上在线观看的免费视频| 久久亚洲美女精品国产精品| 精品免费久久久久久成人影院| 久久99精品免费一区二区| 亚洲午夜精品国产电影在线观看| 国产伦精品一区二区三区免费下载 | 成人女人A级毛片免费软件| 菠萝菠萝蜜在线免费视频| 久久精品国产亚洲av影院|