1. 程式人生 > >使用resultMap實現ibatis復合數據結構查詢(1.多重屬性查詢;2.屬性中含有列表查詢)

使用resultMap實現ibatis復合數據結構查詢(1.多重屬性查詢;2.屬性中含有列表查詢)

class 查詢語句 log num cit code element 訂單 例如


以訂單為例(訂單詳情包括了訂單的基本信息,配送物流信息,商品信息),直接上代碼:


1.多重屬性查詢


java實體

public class OrderDetail {

    @XmlElement(required = true)
    protected String orderSn;
    @XmlElement(required = true)
    protected String orderAmount;
    @XmlElement(required = true)
    protected String orderStatus;
    @XmlElement(required = true)
    protected String orderAddTime;
    @XmlElement(required = true)
    protected Logistics logistics;
    @XmlElement(required = true)
    protected OrderGoods orderGoods;
    @XmlElement(required = true)
    protected List<OrderDetail> listData;
<pre name="code" class="java">      ... getter and setter...
}

class="orderDetail" 即為上面的類,有個屬性logistics的類型是復合類型Logistics

以下是訂單詳情的resultMap:

		<resultMap id="OrderInfoMap" class="orderDetail">
			<result property="orderSn" column="orderSn"/>
			<result property="orderAmount" column="orderAmount"/>
			<result property="orderStatus" column="orderStatus"/>
			<result property="orderStatus" column="orderAddTime"/>
			
			<result property="logistics.consignee" column="consignee"/>
			<!--  <result property="logistics.receiver" column="receiver"/> -->
			<result property="logistics.country" column="country"/>
			<result property="logistics.province" column="province"/>
			<result property="logistics.city" column="city"/>
			<result property="logistics.district" column="district"/>
			<result property="logistics.address" column="address"/>
			<result property="logistics.mobile" column="mobile"/>
			<result property="logistics.shippingName" column="shippingName"/>
			
			
			<result property="orderGoods.listData" select="wxShopOrder.getOrderGoodsList" 
			        column="orderSn"/>
		</resultMap>

當中"<result property="logistics.consignee" column="consignee"/> ",logistics.consignee就能夠用對象訪問的寫法來查詢,consignee是logistics的屬性,logistics是orderDetail的屬性.註意:column的名稱應該與sql語句查詢的列名一致.


sql查詢語句例如以下:

   <select id="getOrderDetail" parameterClass="string" resultMap="OrderInfoMap" >
      	 	select  
                  order_sn as orderSn,
                  order_amount as orderAmount,
                  order_status as orderStatus,
                  add_time as orderAddTime,
                  consignee,
                  country,
                         
                  b.region_name province,
                  c.region_name city,
                  d.region_name district,
                  address,
                  mobile,
                  shipping_name as shippingName
                  
			from  ecs_order_info  o	
				  left join ecs_region b on o.province=b.region_id
	        	  left join ecs_region c on o.city=c.region_id
	        	  left join ecs_region d on o.district=d.region_id   				
			where  order_sn=#orderSn#
        </select>



2.屬性中含有列表查詢


orderDetail有個屬性orderGoods的類型是List復合類型,應在resultMap的屬性定義中加入屬性select="getOrderGoodsList",getOrderGoodsList也是一個sql查詢

<result property="orderGoods.listData" select="wxShopOrder.getOrderGoodsList" column="orderSn"/>

註意:上面column="orderSn"指的是orderSn作為sql的查詢參數. 看下這個 getOrderGoodsList查詢

 <!-- 通過訂單號獲取訂單商品列表 -->
        <select id="getOrderGoodsList" parameterClass="string" resultMap="orderGoodsMap">    	 	
      	 	select goods_name as goodsName,
                   goods_number as goodsNumber
            from ecs_order_goods where order_id=
      	 	(select order_id from ecs_order_info where order_sn=#orderSn#)
        </select>



使用resultMap實現ibatis復合數據結構查詢(1.多重屬性查詢;2.屬性中含有列表查詢)