1: 一对多关联
持有复杂类型的类为一方,复杂类型为多方.
2: 一对多关联 :
3: 一对多关联 : 联合查询方式
4:iBATIS在各主流数据库中获得自增主键的方式
[color=indigo]转载请注明: [url]http://chrislee.iteye.com/[/url][/color]
持有复杂类型的类为一方,复杂类型为多方.
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]