ibatis高级查询技术的使用

在ibatis中使用XML

1)XML参数

<select id="getByXmlId" resultClass="Account" parameterClass="xml">
	select 
		accountId,
		username,
		password,
		firstname,
		lastname,
		address1,
		address2
	from 
		Account
	where accountId = #accountId#
</select>

String parameter = "<parameter><accountId>3</accountId></parameter>";
Account account = sqlMapClient.queryForObject(
	"Account.getXmlId"
	,parameter
);


2)DOM参数

暂时不讲解,不推荐写,别计较麻烦

3)XML结果

<select id="getByIdValue<span style="font-family: Arial, Helvetica, sans-serif;">Xml</span>" resultClass="xml" xmlResultName="account">
	select 
		accountId,
		username,
		password,
	from 
		Account
	where accountId = #accountId#
</select>


String xmlData = (String)sqlMap.queryForObject("Account.getByIdValueXml", new Integer(1));

结果:

<?xml version="1.0" encoding="UTF-8" ?>
<account>
	<accountid>1</accountid>
	<username>lmeadors</username>
	<password>blah</password>
</account>

也可以返回多条记录,详解见《ibatis实战》P109


4)复杂集合

<sqlMap namspace="Ch6">
	<resultMap id="resultAccountInfoMap" class="org.apache.mapper2.examples.bean.AccountInfo">
		<result property ="account.accountId"
				column="accountId"/>
		
		<result property ="orderList"
				select="Ch6.getOrderInfoList"
				column="accountId"
		/>
	</resultMap>

	<select id="getOrderInfoList"  resultMap="ResultOrderInfoMap">
		select orderId from orders where accountId = #value#
	</select>
</sqlMap>


5)映射继承

<resultMap id="document" class="testdomai.Document">
	<result property="id" column="DOCUMENT_ID"/>
	<result property="title" column="TITLE"/>
	<result property="type" column="TYPE"/>
 	<strong><discriminator  column="TYPE" javaType="string">
		<subMap value="Book" resultMap="book"/>
		<subMap value="Newspaper" resultMap="news"/>
	</discriminator  ></strong>
</resultMap>

<resultMap id="book" class="testdomai.Book" extends="document">
	<result property="pages" column="DOCUMENT PAGENUMBER"/>
</resultMap>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值