Ibatis复杂门类属性(即自定义类型的属性)使用

Ibatis复杂类型属性(即自定义类型的属性)使用

因为mapped statement知道如何装入合适的数据和Java类,通过将resultMap的property和相应的mapped statement联系起来,可以自动地给复杂类型(即用户创建的类)的属性赋值。复杂类型用以表示在数据库中相互关系为一对一,一对多的数据。对于一对多的数据关系,拥有复杂类型属性的类作为“多”的一方,而复杂属性本身则作为“一”的一方。考虑下面的例子:
<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” column=”PRD_CAT_ID” select=”getCategory”/>
</resultMap>
<resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
<result property=”id” column=”CAT_ID”/>
<result property=”description” column=”CAT_DESCRIPTION”/>
</resultMap>
<statement id=”getProduct” parameterClass=”int” resultMap=”get-product-result”>
select * from PRODUCT where PRD_ID = #value#
</statement>
<statement id=”getCategory” parameterClass=”int” resultMap=”get-category-result”>
select * from CATEGORY where CAT_ID = #value#
</statement>
上面的例子中,Product对象拥有一个类型为Category的category属性。因为category是复杂类型(用户定义的类型),JDBC不知道如何给它赋值。通过将category属性值和另一个mapped statement联系起来,为SQL Map引擎如何给它赋值提供了足够的信息。通过执行“getProduct”,“get-product-result”Result Map使用PRD_CAT_ID字段的值去调用“getCategory”。“get-category-result”Result Map将初始化一个Category对象并赋值给它。然后整个Category对象将赋值给Product的category属性。

相关介绍,可参考这里

 http://www.diandian315.com/thread-36-1-1.html