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

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

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

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

    <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,我們實現了關聯數據的讀取。
    需要注意的是,這里有一個潛在的性能問題,也就是所謂“n+1”Select問題。
    一對一關聯:
    對于這種情況,我們可以采用一次Select兩張表的方式,避免這樣的性能開銷(假設上面示例中,每個User 只有一個對應的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>
    ??? ?


    在現在的版本中,對于n+1問題,ibatis已經很好的解決了。如下的配置:
    一對一
    ?

    <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>?? ?

    可以使用內在的resultMap來解決此問題。
    同樣一對多如下:

    ?
    <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 布衣郎 閱讀(5489) 評論(8)  編輯  收藏 所屬分類: orm

    FeedBack:
    # re: ibatis 對象關系實現
    2007-01-17 09:49 | 實用主義
    ibatis 最主要的作用是什么,LZ還不知道呀,不要白讀書呀  回復  更多評論
      
    # re: ibatis 對象關系實現
    2007-01-17 10:41 | 布衣郎
    @實用主義
    既然是半成品的orm,當然不能完全只是依賴于sql和存儲過程,那還不如直接去用jdbc了。  回復  更多評論
      
    # re: ibatis 對象關系實現
    2007-05-22 10:28 | murphy
    你好,我在做ibatis時使用你所提到的方法,但是出現了問題,提示是無法找到類似你提到的1:1關系中的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.
      回復  更多評論
      
    # re: ibatis 對象關系實現
    2007-05-22 15:35 | 布衣郎
    @murphy
    你沒有定義getPartByID這個SqlMap,仔細看看你的配置文件  回復  更多評論
      
    # re: ibatis 對象關系實現
    2007-05-23 10:12 | murphy
    你好,已經配置了,就是因為配置了還不行啊.
    附:
    <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>   回復  更多評論
      
    # re: ibatis 對象關系實現
    2007-05-23 14:49 | 布衣郎
    你的Part中的屬性也是part_ID,part_Code,而不是字段名稱嗎?  回復  更多評論
      
    # re: ibatis 對象關系實現
    2007-05-25 11:33 | murphy
    Part中屬性和數據庫中的字段是基本對應的,這和找不到Map有什么直接關系嗎?  回復  更多評論
      
    # re: ibatis 對象關系實現
    2007-05-25 15:40 | 布衣郎
    有可能,主要的問題在于part_ID這個命名,以前beanutils對于兩個連寫的大寫字符有個小bug,解析不了。要不換換看看。  回復  更多評論
      

    只有注冊用戶登錄后才能發表評論。


    網站導航:
     

    <2007年5月>
    293012345
    6789101112
    13141516171819
    20212223242526
    272829303112
    3456789

    常用鏈接

    留言簿(12)

    隨筆分類(59)

    隨筆檔案(57)

    blog

    java

    uml

    搜索

    •  

    積分與排名

    • 積分 - 357288
    • 排名 - 155

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲AV无码精品色午夜在线观看| 18女人水真多免费高清毛片 | 久久久久久噜噜精品免费直播| 四虎影视www四虎免费| 在线电影你懂的亚洲| 免费人成激情视频在线观看冫| 亚洲精品自产拍在线观看| 在线毛片片免费观看| 五月天网站亚洲小说| 最近最新高清免费中文字幕| 亚洲视频在线一区二区三区| 亚洲免费观看在线视频| 在线观看日本亚洲一区| 免费观看国产精品| 国产精品一区二区三区免费| 亚洲av永久无码精品秋霞电影影院| 免费看成人AA片无码视频吃奶| 久久亚洲AV成人出白浆无码国产 | 精品久久久久久亚洲综合网| 亚洲精品A在线观看| 在线免费观看伊人三级电影| 亚洲视频在线免费播放| 免费无码又爽又刺激毛片| 男男gay做爽爽免费视频| 国产亚洲无线码一区二区| 久久国产乱子伦精品免费看| 亚洲第一AAAAA片| 日本免费久久久久久久网站| 亚洲视频一区在线| 久久精品a一国产成人免费网站| 国产成人亚洲精品| 国产老女人精品免费视频| 精品亚洲成a人在线观看| 国产成人精品久久亚洲高清不卡 | 一级毛片a免费播放王色电影 | 国产精品免费大片一区二区| 亚洲制服中文字幕第一区| 99爱在线精品视频免费观看9| 水蜜桃亚洲一二三四在线| 曰皮全部过程视频免费国产30分钟| 中文字幕免费播放|