【Ibatis】(十)、xmlResultName详解

如何将查询结果集映射为 xml格式的数据?

映射文件:

Xml代码   收藏代码
  1. <!--   mapping to xml data     -->  
  2.     <select id="selectXmlData" resultClass="xml" xmlResultName="Product" parameterClass="int">  
  3.         <![CDATA[ 
  4.             select * from t_product where prd_id=#value# 
  5.         ]]>  
  6.     </select>  
  7.     <select id="selectXmlDatas" resultClass="xml" xmlResultName="Product">  
  8.         <![CDATA[ 
  9.             select prd_id as id, 
  10.        prd_description as description, 
  11.        prd_price as price 
  12. from t_product   
  13.                 ]]>  
  14.     </select>  
  15. <!--  END  mapping to xml data-->  

 DAO层:

Java代码   收藏代码
  1. public Object getXmlData(int id) throws SQLException {  
  2.         init();  
  3.         Object obj = (Object)sqlMapClient.queryForObject("selectXmlData", id);  
  4.         return obj;  
  5.     }  
  6.   
  7.   
  8.     public List getXmlDatas() throws SQLException {  
  9.         init();  
  10.         List list = (List)sqlMapClient.queryForList("selectXmlDatas");  
  11.         return list;  
  12.     }  

 测试类:

Java代码   收藏代码
  1. public class ProductTest {  
  2.   
  3.     ProductDAO productDao = new ProductDAOImpl();  
  4.       
  5.     public static void main(String[] args) throws SQLException {  
  6.         ProductTest ptest = new ProductTest();  
  7.         ptest.getXmlData(1);  
  8.         System.out.println("-------------------");  
  9.         ptest.getXmlDatas();  
  10.     }  
  11.       
  12.     /** 
  13.      * 获得XML格式的数据 
  14.      * @throws SQLException 
  15.      */  
  16.     public void getXmlData(int id) throws SQLException{  
  17.         // 查找t_product表,将其结果映射到xml!  
  18.         // 返回值是xml形式的字符串  
  19.         Object obj = (Object) productDao.getXmlData(1);  
  20.         if(obj instanceof Product) {  
  21.             System.out.println("return type is Product");  
  22.         }else if(obj instanceof String) {  
  23.             System.out.println("return type is String");  
  24.         }else {  
  25.             System.out.println("return type is unknow");  
  26.         }  
  27.         System.out.println(obj);  
  28.     }  
  29.       
  30.     public void getXmlDatas() throws SQLException {  
  31.         List list = (List)productDao.getXmlDatas();  
  32.         Iterator i = list.iterator();  
  33.         while(i.hasNext()) {  
  34.             Object obj = i.next();  
  35.             System.out.println(obj.getClass());       
  36.         }     
  37.         System.out.println(list);  
  38.     }  
  39. }  

 结果:

return type is String
<?xml version="1.0" encoding="UTF-8"?>

<Product>

      <prd_id> </prd_id>

      <prd_description>basketball</prd_description>

     <prd_price>206.99</prd_price>

</Product>
-------------------
class java.lang.String
class java.lang.String
[<?xml version="1.0" encoding="UTF-8"?>

<Product>

      <id> 1</id>

      <description>basketball</description>

     <price>206.99</price>

</Product>,

<?xml version="1.0" encoding="UTF-8"?>

<Product>

     <id> 2 </id>

     <description>football</description>

    <price>106.99</price>

</Product>]

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值