复杂类型集合的属性:
Result Map还可以装入代表复杂类型对象集合(List)的属性,用以表示在数据库中相互关系为多对多或一对多的数据。拥有集合属性的类作为“一”的一方,而在集合中的对象作为“多”的一方。用来装入对象集合的mapped statement和上面例子一样。唯一的不同是,让SQL Map架构装入复杂类型集合(List)的业务对象的属性必须是java.util.List或java.util.Collection类型。
<resultMap id=”get-category-result” class=”com.ibatis.example.Category”>
<result property=”id” column=”CAT_ID”/>
<result property=”description” column=”CAT_DESCRIPTION”/>
<result property=”productList” column=”CAT_ID” select=” getProductsByCatId”/>
</resultMap>
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<statement id=”getCategory” parameterClass=”int” resultMap=”get-category-result”>
select * from CATEGORY where CAT_ID = #value#
</statement>
<statement id=”getProductsByCatId” parameterClass=”int” resultMap=”get-product-result”>
select * from PRODUCT where PRD_CAT_ID = #value#
</statement>
上面一次执行的顺序是3->1->4->2。
组合键值或多个复杂参数属性:
在有多个属性之互相关联的时候就用下面的这种格式:
<resultMap id=”get-order-result” class=”com.ibatis.example.Order”>
<result property=”id” column=”ORD_ID”/>
<result property=”customerId” column=”ORD_CST_ID”/>
…
<result property=”payments” column=”{itemId=ORD_ID, custId=ORD_CST_ID}” select=” getOrderPayments”/>
</resultMap>
<statement id=”getOrderPayments” resultMap=”get-payment-result”>
select * from PAYMENT where PAY_ORD_ID = #itemId# and PAY_CST_ID = #custId#
</statement>
parameterClass属性是可选的,但强烈建议使用。它的目的是限制输入参数的类型为指定的Java类,并优化框架的性能。如果您使用parameterMap,则没有必要使用parameterClass属性。
parameterMap属性的值等于一个预先定义的<parameterMap>元素的名称。
resultClass属性可以让您指定一个Java类,根据ResultSetMetaData将其自动映射到JDBC的ResultSet。只要是Java Bean的属性名称和ResultSet的列名匹配,属性自动赋值给列值。这使得查询mapped statement变得很短。如:
<statement id="getPerson" parameterClass=”int” resultClass="examples.domain.Person">
SELECT PER_ID as id,
PER_FIRST_NAME as firstName,
PER_LAST_NAME as lastName,
PER_BIRTH_DATE as birthDate,
PER_WEIGHT_KG as weightInKilograms,
PER_HEIGHT_M as heightInMeters
FROM PERSON
WHERE PER_ID = #value#
</statement>
resultMap可以控制数据如何从结果集中取出,以及哪一个属性匹配哪一个字段。不象使用resultClass的自动映射方法,resultMap属性可以允许指定字段的数据类型,NULL的替代值复杂类型映射(包括其他Java Bean,集合类型和基本类型包装类)。如:
<resultMap id=”get-product-result” class=”com.ibatis.example.Product”>
<result property=”id” column=”PRD_ID”/>
<result property=”description” column=”PRD_DESCRIPTION”/>
</resultMap>
<statement id=”getProduct” resultMap=”get-product-result”>
select * from PRODUCT
</statement>
分享到:
相关推荐
ibatis实例,ibatis实例,mysql应用举例,大家快来看。
ibatis实现原理解析
Ibatis增删改简单应用配置示例说明实例 oracle 11g + ibatis2
ibatis批量
ibatis框架简单应用
Ibatis应用实例.docx,非常非常值得推荐下载
基于iBATIS动态SQL的应用研究.pdf
ibatis学习 ibatis总结 ibatis ibatis ibatis
ibatis的配置文件和映射文件,以及ibatis的应用
ajax和ibatis的综合应用例子,内带了Ibaties的配置文件,是实现的一个读取XML文件的例子
将IBatis.NET 1.92 1.6.2版本的库在.NET 4.0下应用。结果发现总是报错Ambiguous match found 经过参考资料,重新整理了ibatis的资料,原因如下: iBatis中使用了Castle.DynamicProxy 1.5 而Castle.DynamicProxy ...
ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料ibatis资料
ibatis入门与ibatis迭代的用法
1,建立应用程序 2,在项目中增加对IBatisNet.Common、IBatisNet.DataAccess、IBatisNet.DataMapper的引用; 3,建立pojo类 4,配置ibatis对应pojo的sqlmap文件 5,把providers.config复制到debug目录下,不用做任何...
基于SQLserver 数据库的IBatis中存储过程应用实例
ibatis demo,ibatis例子,ibatis示例
iBATIS 级联iBATIS 级联iBATIS 级联
实现ibatis手动控制加载sqlmap文件,终于不用重启应用了
书中既详实地介绍了iBATIS的设计理念和基础知识,也讨论了动态SQL、高速缓存、DAD框架等高级主题,还讲解了iBATIS在实际开发中的应用。《iBATIS实战》的最后给出了一个设计优雅、层次清晰的示例程序JGameStore,该...
Ibatis增删改简单应用配置示例说明,及项目 第一次写文档,请大家多包涵