首页 > 代码库 > 使用resultMap实现ibatis复合数据结构查询(1.多重属性查询;2.属性中含有列表查询)

使用resultMap实现ibatis复合数据结构查询(1.多重属性查询;2.属性中含有列表查询)


以订单为例(订单详情包括了订单的基本信息,配送物流信息,商品信息),直接上代码:


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.属性中含有列表查询)