"MyBatis Learning 09" One of the advanced mappings to multi-query

Source: Internet
Author: User

The previous blog summarizes a one-to-one mapping, this article summarizes a one-to-many mapping, from the previous article in the mapping diagram, we know that the ordering and order details is a one-to-many relationship, so this article mainly to query the order form, and then related to the order list, so there is a more than one problem out.
The first is to write the SQL statement, the SQL statement when the time to follow two points:

    1. Which is the main table of the query? Order Form
    2. Which is the associated table for the query? Order Schedule

Having defined the main table and the associated table, we can write the SQL below, and we'll add an association to the order schedule on the SQL basis of the previous section.

SELECTorders.*,  User. ' Username ',User. ' Sex ',User. ' Address ', OrderDetail ' id ' orderdetail_id, orderdetail. ' items_id ', OrderDetail. ' Items_num ', OrderDetail ' orders_id ' fromorders,USER, OrderDetailWHEREOrders. 'user_id`=User. ' ID ' andOrders. ' ID '=OrderDetail. ' orders_id '

This allows us to query all the fields in the order table, the user table and some fields of the OrderDetail table, and, of course, all the fields, depending on the specific needs. Look at the results of the query:

  As can be seen from the results, the order information is duplicated, the order item is not duplicated, because a pair of well, this is very good understanding. So if we use Resulttype to do the mapping, there will be a repetition of the order information, we do not want this result, that is, the mapping of orders can not be repeated records of the situation. Then we need to add a property in the Orders.java class List<OrderDetail> orderDetails to encapsulate the order line item (which is relatively simple, the code is not posted), and the order information is eventually mapped to orders. The order details for the order are mapped to the OrderDetails attribute in orders (this is a bit similar to hibernate, and if hibernate, a list of OrderDetail is maintained in the Orders Class).
  With this idea in mind, the next step is to write the mapping file.

<id= "Findordersandorderdetailresultmap"  resultmap= " Ordersandorderdetailresultmap ">    Select       orders.*,      user. ' username ',      user. ' Sex ',      user. ' Address ',      orderdetail ' id ' orderdetail_id,      orderdetail. ' items_id ',      OrderDetail . ' Items_num ',      orderdetail ' orders_id ' from      orders,      USER,      orderdetail       </Select>

So we're going to define a resultmap named Ordersandorderdetailresultmap, as follows:

<Resultmap type="Mybatis.po.Orders" id="Ordersandorderdetailresultmap" extends="Ordersuserresultmap">    <!--Configure the map order information and associated user information as above, inherit the above-    <!--Configure the associated order details-    <Collection Property="OrderDetails" OfType="Mybatis.po.Orderdetail">        <Idcolumn="Orderdetail_id" Property="id"/>        <Resultcolumn="Items_id" Property="Itemsid"/>        <Resultcolumn="Items_num" Property="Itemsnum"/>        <Resultcolumn="Orders_id" Property="Ordersid"/>    </Collection></Resultmap>

Here we see an inheritance, because the order information and the associated user information is exactly the same as the previous one, we do not need to write again, <resultMap> support inheritance, directly inherit the Resultmap, and then add the order details this part can be.
  <collection>is used to process a one-to-many mapping label, The property attribute is the attribute name of the OrderDetail list in the Orders.java class, that is, the List,oftype attribute that you just defined indicates what is in the list, either a fully qualified name or an alias. Then <collection> the tags and attributes are the same as before, and no longer repeat them.
  Then define the Mapper interface:

 Public Interface usermapperorders {    // omit irrelevant code     // Enquiry Order (associated user) and order Details     publicthrows  Exception;}

At this point, a one-to-many mapping is written, and the following tests:

@Test  Public void throws Exception {    = sqlsessionfactory.opensession ();     = Sqlsession.getmapper (usermapperorders.  Class);    List<Orders> list = usermapperorders.findordersandorderdetailresultmap ();    SYSTEM.OUT.PRINTLN (list);}

One-to-many summarizes this, and the next blog post will summarize many-to-many mappings in MyBatis.

"MyBatis Learning 09" One of the advanced mappings to multi-query

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.