闲人野居
好好学习,天天向上
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 布衣郎 阅读(5460) 评论(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

搜索

  •  

积分与排名

  • 积分 - 355560
  • 排名 - 154

最新评论

阅读排行榜

评论排行榜