ibatis高级查询技术的使用

论坛 期权论坛 脚本     
匿名网站用户   2020-12-21 09:35   11   0

在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>

分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:1136255
帖子:227251
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP