Use of MyBatis Four (iii) (Mapper.xml)

Source: Internet
Author: User

    1. A pair of multiple sets

      Items Category:

Public class items {private int itemid;private string itemname;private  float itemprice;private string itemdetail;public int getitemid ()  {return  itemid;} Public void setitemid (Int itemid)  {this.itemid = itemid;} Public string getitemname ()  {return itemname;} Public void setitemname (String itemname)  {this.itemname = itemname;} Public float getitemprice ()  {return itemprice;} Public void setitemprice (Float itemprice)  {this.itemprice = itemprice;} Public string getitemdetail ()  {return itemdetail;} Public void setitemdetail (String itemdetail)  {this.itemdetail = itemdetail;} @Overridepublic  string tostring ()  {return  "items [itemid="  + itemid +   ",  itemname="  + itemName+  ",  itemprice= " + itemPrice + ",  itemdetail= " + itemDetail+ "] ";}} 
<!--  One-to-many sets: User and User's order and order details  --><resultmap type= "user"  id= " Orderdetailinorderinusermap ">  <!--  PRIMARY key  -->  <id property= for query result set "userid"  column= "userid"/>  <result property= "username"  column= "username"/>   <result property= "Sex"  column= "Sex"/>  <result property= " Birthday " column=" Birthday "/>    <collection property=" Orders " oftype = "Mybatis_b.model.Order" >  <!--  One-to-many when,id  cannot default  -->       <id property= "OrderId"  column= "OrderId"/>      < Result property= "OrderNumber"  column= "OrderNumber"/>      <!--  collect nested  collect -->      <collection property= " OrderDetails " oftype=" mybatis_b.model.OrdErdetail ">          <id property=" OrderDetailId "  column= "Orderdetailid"/>    <result property= "order_id"  column= " order_id "/>          <result property=" item_id "  column= "item_id"/>    <result property= "Item_Number"  column= "item_ Number "/>          <result property=" Item_price "  column= "Item_price"/>      </collection>       </collection></resultMap>  <!--  One-to-many sets of one-to-many: orders and order details for users and users  --> <select id= "Finduserandordersanddetails"  resultmap= "Orderdetailinorderinusermap" >   select users.userid, users.username, users.birthday, users.sex, orders.orderid,    orders.ordernumber, orderdetail.* from users, orders, orderdetail   where  users.userid = orders.user_id and orders.orderid = orderdetail.order_id</ Select>

Java code calls:

One-to-many sets of one-to-many: User and user orders and order details @testpublic void Testfinduserandordersanddetails () throws Exception {// Get sqlsessionsqlsession sqlsession = Sqlsessionfactory.opensession ();//Specify the type of the Mapper interface, MyBatis the Mapper interface ordermapper ordermapper = Sqlsession.getmapper (Ordermapper.class) by means of dynamic proxy; list<user> list = Ordermapper.finduserandordersanddetails (); System.out.println (List.size ());iterator<user> Iterator = List.iterator (); while (Iterator.hasnext ()) {User Order = (User) iterator.next (); SYSTEM.OUT.PRINTLN (order);}}

Execution Result:

    1. Many-to-many (Manytomany)

<!--  Many-to-many: query the user, the user's order and order details and product information  -->  <resultmap type= "user"  id= " Iteminorderdetailinorderinusermap ">  <!--  PRIMARY key for query result set  -->  <id  property= "userid"  column= "userid"/>  <result property= "username"  column= " Username "/>  <result property=" sex " column=" Sex "/>  <result  property= "Birthday"  column= "Birthday"/>    <collection property= "Orders"  oftype= "Mybatis_b.model.Order" >    <!--  One-to-many time,id  cannot default  -->     <id property= "OrderId"  column= "OrderId"/>    < Result property= "OrderNumber"  column= "OrderNumber"/>         <collection property= "OrderDetails"  oftype= "Mybatis_b.model.OrderDetail" >     <id property= "OrderdEtailid " column=" Orderdetailid "/>    <result property=" order_id "  Column= "order_id"/>  <result property= "item_id"  column= "item_id"/>   <result property= "Item_Number"  column= "Item_Number"/>  <result property= " Item_price " column=" Item_price "/>  <association property=" Item " javaType=" Mybatis_b.model.Items ">    <id property=" ItemId " column=" ItemId "/>     <result property= "ItemName"  column= "ItemName"/>     <result property= "Itemprice"  column= "Itemprice"/>    <result  property= "Itemdetail"  column= "Itemdetail"/>  </association>     </collection>  </collection>  </resultMap>  <!--   Many-to-many: query the user, the user orders and order details and product information  --><Select id= "Finduserandordersdetailsanditems"  resultmap= "Iteminorderdetailinorderinusermap" >   select users.userid, users.username, users.birthday, users.sex, orders.orderid,    orders.ordernumber, orderdetail.*, items.itemname, items.itemprice,  items.itemdetail   from users, orders, orderdetail, items where  users.userid = orders.user_id  and orders.orderid = orderdetail.order_id  and items.itemid = orderdetail.item_id</select>

Java code calls ditto, just modify:

list<user> list = Ordermapper.finduserandordersdetailsanditems ();

Execution Result:

Use of MyBatis Four (iii) (Mapper.xml)

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.