[/*Java*/]iBatis多键值查询 |
nybon 发表于 2005/4/27 18:04:49 |
摘自iBATIS SQL Maps 开发指南Clinton Begin 著刘 涛 译
Map类型输入参数假如没必要写一个Java Bean作为参数,而要传入的参数又不只一个时,可以使用Map类(如HashMap,TreeMap等)作为参数对象。例如:<statement id=”insertProduct” parameterClass=”java.util.Map”>select * from PRODUCTwhere PRD_CAT_ID = #catId#and PRD_CODE = #code#</statement>可以注意到mapped statement的形式完全没有区别!上面的例子中,如果把Map对象作为输入参数去调用mapped statement,Map对象必须包含键值“catId”和“code”。键值引用的对象必须是合适的类型,以上面的例子来说,必须是Integer和String。Result Map(参见以下章节)也支持使用Map类型作为结果参数。要获得更多信息,请参见“Result Map”和“使用SQL Map API编程”部分。Map类型也可以使用别名。例如,可以用“map”来代替“java.util.Map”。这些别名参见下面的“支持Parameter Map和Result Map的数据类型”表格。
复杂类型集合的属性Result Map还可以装入代表复杂类型对象集合(List)的属性,用以表示在数据库中相互关系为多对多或一对多的数据。拥有集合属性的类作为“一”的一方,而在集合中的对象作为“多”的一方。用来装入对象集合的mapped statement和上面例子一样。唯一的不同是,让SQL Map架构装入复杂类型集合(List)的业务对象的属性必须是java.util.List或java.util.Collection类型。例如,如果Category拥有Product对象的List,mapped-statement就像下面的例子(假设Category类有一个叫productList的属性,类型是java.util.List):<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>
|
|
|

.: 公告
This blog focuses on:
Semantic Web && Java Technology |
|
« | August 2025 | » | 日 | 一 | 二 | 三 | 四 | 五 | 六 | | | | | | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | | | | | | | |
|
.: 我的分类(专题)
|

.: 最新日志
.: 最新回复
|

blog名称:SW Portal 日志总数:171 评论数量:219 留言数量:8 访问次数:1044951 建立时间:2004年10月30日 |
|

.: 留言板
|

.: 链接
|

|