Use resultmap to implement ibatis composite Data Structure Query (1. Multi-attribute query; 2. List query included in the attribute)

Source: Internet
Author: User


Take the order as an example and directly add the Code:


1. Multi-attribute Query


Java entity

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" is the preceding class. The type of the attribute logistics is a composite type 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>


The SQL query statement is as follows:

   <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. the attribute contains list queries.


Orderdetail has the attribute ordergoods of the list compound type. You should query the list in the attribute definition of resultmap: getordergoodslist

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

Check the getordergoodslist query.

<! -- Obtain the order item list using the order number --> <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>

The returned result is also a resultmap -- ordergoodsmap


<resultMap id="orderGoodsMap" class="com.uid.trace.module.sale.schema.OrderGoods"><result property="goodsName" column="goodsName"/><result property="goodsNumber" column="goodsNumber"/></resultMap>


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.