iBATIS笔记

本文介绍了一对多关联查询的两种方式:子查询和联合查询,并展示了如何在iBATIS中配置这些查询。此外,还提供了在不同数据库(如Oracle、SQL Server和MySQL)中获取自增主键的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1: 一对多关联
持有复杂类型的类为一方,复杂类型为多方.

2: 一对多关联 :
		<!-- 多(book)对一(catelog)子查询方式

<resultMap class="book" id="getBook">

<!-- 普通property -->
<result property="bookid" column="bookid"/>
<result property="bookname" column="bookname"/>
<result property="price" column="price"/>
<result property="picture" column="picture"/>

<!-- 一方 -->
<result property="catelog" column="catelogid"
select="getCategory"/><!-- 注意 1 getCategory -->
</resultMap>

<resultMap class="catelog" id="getCatelog">
<result property="catelogid" column="catelogid"/>
<result property="catelogname" column="catelogname"/>
</resultMap>

<select id="findBookByProperty"
parameterClass="pv"
resultMap="getBook">
SELECT bookid, catelogid, bookname, price, picture, jointime
FROM book
WHERE $property$=#value#
</select>

<!-- 注意 对应 1 -->
<select id="getCategory" parameterClass="int"
resultMap="getCatelog">
SELECT catelogid, catelogname
FROM catelog
WHERE catelogid=#catelogid#
</select>
-->



3: 一对多关联 : 联合查询方式

	<!-- 多(book)对一(catelog)联合查询方式 -->
<resultMap class="book" id="getBook">

<!-- 普通property -->
<result property="bookid" column="bookid"/>
<result property="bookname" column="bookname"/>
<result property="price" column="price"/>
<result property="picture" column="picture"/>

<!-- 一方 -->
<result property="catelog.catelogid" column="catelogid" />
<result property="catelog.catelogname" column="catelogname" />
</resultMap>

<select id="findBookByProperty"
parameterClass="pv"
resultMap="getBook">
SELECT bookid, book.catelogid, bookname, price, picture, jointime, catelog.catelogid, catelogname
FROM book, catelog
WHERE book.catelogid=catelog.catelogid
AND `book`.$property$=#value#
</select>


4:iBATIS在各主流数据库中获得自增主键的方式

	<!-- Oracle SEQUENCE Example -->
<insert id="insertProduct-ORACLE" parameterClass="com.domain.Product">
<selectKey resultClass="int" keyProperty="id" >
SELECT STOCKIDSEQUENCE.NEXTVAL AS ID FROM DUAL
</selectKey>
insert into PRODUCT (PRD_ID,PRD_DESCRIPTION)
values (#id#,#description#)
</insert>

<!-- Microsoft SQL Server IDENTITY Column Example -->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
insert into PRODUCT (PRD_DESCRIPTION)
values (#description#)
<selectKey resultClass="int" keyProperty="id" >
SELECT @@IDENTITY AS ID
</selectKey>
</insert>

<!-- MySQL auto_increment Column Example -->
<insert id="insertProduct-MS-SQL" parameterClass="com.domain.Product">
insert into PRODUCT (PRD_DESCRIPTION)
values (#description#)
<selectKey resultClass="int" keyProperty="id" >
SELECT last_insert_id()
</selectKey>
</insert>


[color=indigo]转载请注明: [url]http://chrislee.iteye.com/[/url][/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值