首页 > 数据库 > 其他综合 >

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

2014-07-12

以订单为例,直接上代码: 1 多重属性查询 java实体 public class OrderDetail { @XmlElement(required = true) protected String orderSn; @XmlElement(required = true)

以订单为例,直接上代码:

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;
        ... getter  setter...
}

class="orderDetail" 即为上面的类,有个属性logistics的类型是复合类型Logistics

		<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>
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的属性定义中给这个list做查询 :getOrderGoodsList

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

看下这个查询 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 -- orderGoodsMap
<resultMap id="orderGoodsMap" class="com.uid.trace.module.sale.schema.OrderGoods">
	<result property="goodsName" column="goodsName"/>
	<result property="goodsNumber" column="goodsNumber"/>			

</resultMap>
相关文章
最新文章
热点推荐