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>

你可能感兴趣的:(ibatis高级查询技术的使用)