Use of MyBatis four (ii) (MAPPER.XML)

Source: Internet
Author: User
<span id="Label3"></p><p><p>When you perform a query operation, you typically return a result set that appears in several cases:<br></p></p> <p><p>1. One-to-one query: the <span style="color: rgb(227, 108, 9);">unique primary key of the returned result set is</span> Non-duplicated. (there is a unique primary key, which <span style="color: rgb(227, 108, 9);">differs from Oracle's primary key</span> )</p></p> <p><p>Query the order and the user information of the order, two ways to receive the result set:</p></p> <p><p>1) use the Pojo object to receive the result set (unique primary key:)</p></p> <p><p></p></p> <pre class="brush:java;toolbar: true; auto-links: false;">public class ordercustom extends user {private int orderid; // Order Idprivate int user_id; //user Idprivate string ordernumber; //order Number Public int  getorderid ()  {return orderid;} Public void setorderid (int orderid)  {this.orderid = orderid;} public int getuser_id ()  {return user_id;} public void setuser_id (int user_id)  {this.user_id = user_id;} Public string getordernumber ()  {return ordernumber;} Public void setordernumber (string ordernumber)  {this.ordernumber = ordernumber;} @Overridepublic  string tostring ()  {string userstring = super.tostring (); return   "ordercustom {orderid="  + orderId +  " user_id="  + user_id+   ",  ordernumber="  + orderNumber +  "["  + userString +  "]" + "} ";}} </pre> <pre class="brush:xml;toolbar: true; auto-links: false;"><pre class="brush:xml;toolbar:true; auto-links:false"><!--  Enquiry Order and user information,resulttype -->   <select databaseid= "oracle"  id= "findordersusers_resulttype"  resulttype= "orderCustom" >  select orders.*, users.* from orders, users where  Orders.user_id = users.userid  </select> </pre></pre> <pre class="brush:java;toolbar: true; auto-links: false;">public class ordermappertest extends testcase {private sqlsessionfactory  sqlsessionfactory;private string resource =  "sqlmapconfig.xml"; @Overrideprotected   Void setup ()  throws exception {super.setup ();//read configuration file via input stream Inputstream inputstream  = resources.getresourceasstream (resource);//get sqlsessionfactorysqlsessionfactory = new  sqlsessionfactorybuilder (). Build (inputstream,  "development_oracle");} One-to-one query, using Resulttype@testpublic void testfindordersusers_resulttype ()  throws exception  {//get sqlsessionsqlsession sqlsession = sqlsessionfactory.opensession ();//specify  mapper The type of the   interface, MyBatis implements the Mapper interface Ordermapper ordermapper = sqlsession.getmapper by means of a dynamic proxy ( ordermapper.class); List<ordercustom> list = ordermapper.findordersusers_resulttype (); System.out.println (list.size ()); Iterator<ordercustom> iteratoR = list.iterator ();while  (iterator.hasnext ())  {OrderCustom orderCustom =  ( Ordercustom)  iterator.next (); System.out.println (ordercustom);}}}</pre> <p><p>Execution Result:</p></p> <p><p></p></p> <p><p>2) receive result sets using Resultmap</p></p> <pre class="brush:java;toolbar: true; auto-links: false;">Public class order {private int orderid; //order idprivate int user_id;  //user Idprivate string ordernumber; //order number Private user user; //user information private  list<orderdetail> orderdetails;  //order details Public int getorderid ()  { return orderid;} Public void setorderid (int orderid)  {this.orderid = orderid;} public int getuser_id ()  {return user_id;} public void setuser_id (int user_id)  {this.user_id = user_id;} Public string getordernumber ()  {return ordernumber;} Public void setordernumber (string ordernumber)  {this.ordernumber = ordernumber;} Public user getuser ()  {return user;} Public void setuser (user user)  {this.user = user;} Public list<orderdetail> getorderdetails ()  {return orderdetails;} public&Nbsp;void setorderdetails (list<orderdetail> orderdetails)  {this.orderDetails =  orderdetails;} @Overridepublic  string tostring ()  {return  "order [orderid="  + orderId  +  ",  user_id="  + user_id+  ",  ordernumber="  + orderNumber +  " ,  user= " + user+ ",  orderdetails= " + orderDetails + "] ";}}</pre> <pre class="brush:xml;toolbar: true; auto-links: false;"><!--  Enquiry of orders and user information, resultmap --><resultmap id= "orderusermap"  type= "mybatis_ B.model.order ">  <!-- order Information  -->  <!--  id  here It is mybatis to map the OrderId field to the order  object for a one-to-one query, you must write  -->  <id property= "orderId"   column= "orderId"/>  <result property= "user_id"  column= "user_id"/>  < result property= "ordernumber"  column= "ordernumber"/>  <!--  User Information  -->   <!--  Package User Information   use  association  as a user object-->  <!--  Association: for one-to-one queries, map the following fields that are queried to a single object       property= "user"  :  Specifies the properties stored by the encapsulated object in orders       javaType :  the javatype    of the specified property    user.username, user.sex, user.address  -->  <association  property= "user"  javatype= "mybatis_b.model.UsEr ">  <!--  here ID is the ID of user, if write to User id attribute assignment  -->       <id property= "userid"  column= "userid"/>      <result  property= "username"  column= "username"/>      <result  property= "sex"  column= "sex"/>      <result property= "birthday"  column= "birthday"/>      <result property= "address"  column = "address"/>      <result property= "detail"  column= "detail"/>       <result property= "score"  column= "score"/>  </ association></resultmap>  <select id= "findordersusers_resultmap"  resultMap= " Orderusermap ">    select orders.*, users.* from orders, users  where orders.user_id = users.userid</select> </pre> <p><p>Java calling code, just modify</p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;">list<ordercustom> list = Ordermapper.findordersusers_resultmap ();</pre></pre> <p><p>2. One-to-many queries: The unique primary key of the <span style="font-size: 13.3333px; line-height: 24px;">returned</span> <span style="font-size: 13.3333px; line-height: 24px; color: rgb(227, 108, 9);">result set is</span> <span style="font-size: 13.3333px; line-height: 24px;">Non-duplicated. As Follows:</span></p></p> <p><p><span style="font-size: 13.3333px; line-height: 24px;">By querying the order and order details, you can see that there <span style="font-size: 13.3333px; line-height: 24px; color: rgb(227, 108, 9);">are two order details in the record that belong to the same order <span style="font-size: 13.3333px; line-height: 24px; color: rgb(0, 0, 0);">, which is a one-to-many query result set. The result set has 5 records, but there should be only four OrderID (because of two duplicates). </span></span><br></span></p></p> <p><p><span style="font-size: 13.3333px; line-height: 24px;"></span></p></p> <p><p> <span style="font-size:13.3333px; line-height:24px;">     one-to-many Query results need to be received using resultmap, The code is as Follows: <br> </span> </p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><!--  orders and details  --><resultmap id= "orderdetails"  type= "mybatis_b.model.Order" >       <!-- id  here represents the unique primary key for the result set, although duplicate OrderDetail are present, but belong to the same orderid - ->      <id property= "orderId"  column= "orderId"/>       <result property= "user_id"  column= "user_id"/>       <result property= "ordernumber"  column= "ordernumber"/>       <!-- collect: for a one-to-many query, receive  List<object>  object;  oftype representation  List  Type of object in  -->      <!--  List<orderdetail in the above  Order  class >  is used to receive a one-to-many result set  -->      <collection property= " OrderDetails " oftype=" Mybatis_b.model.OrderDetail ">      <id  property= "orderdetailid"  column= "Orderdetailid "/>      <result property=" item_id " column=" item_id "/>      <result property=" item_number " column=" Item_Number "/ >      <result property= "item_price"  column= "item_price"/>       </collection></resultmap><select id= "findOrderDetails"  resultmap= "orderdetails" >  select orders.*, orderDetail.orderDetailId,  orderdetail.item_id,   orderdetail.item_number, orderdetail.item_price from  orders, orderdetail   where orders.orderid = orderdetail.order_id</ Select></pre> <p><p> </p></p> <pre class="brush:xml;toolbar: true; auto-links: false;">Public class orderdetail {private int orderdetailid;  //order Detail Idprivate  int order_id;//order idprivate int item_id;//merchandise idprivate int item_number;// Product quantity Private float item_price;//price public int getorderdetailid ()  {return  orderdetailid;} Public void setorderdetailid (int orderdetailid)  {this.orderDetailId =  orderdetailid;} public int getorder_id ()  {return order_id;} public void setorder_id (int order_id)  {this.order_id = order_id;} public int getitem_id ()  {return item_id;} public void setitem_id (int item_id)  {this.item_id = item_id;} Public int getitem_number ()  {return item_number;} Public void setitem_number (int item_number)  {this.item_number = item_number;} Public float getitem_price ()  {return item_price;} PubliC void setitem_price (float item_price)  {this.item_price = item_price;} @Overridepublic  string tostring ()  {return  "orderdetail [orderdetailid="  +  orderdetailid +  ",  order_id=" + order_id +  ",  item_id="  + item_id  +  ",  item_number=" + item_number +  ",  item_price="  + item_price  +  "]";}}</pre> <p style="line-height: 24px; font-size: 13.3333px; white-space: normal;"><p style="line-height: 24px; font-size: 13.3333px; white-space: normal;">Java calling code, just modify</p></p> <pre class="brush:java;toolbar: true; auto-links: false;"><pre class="brush:java;toolbar: true; auto-links: false;">list<ordercustom> list = Ordermapper.findorderdetails ();</pre></pre> <p><p><span style="font-size: 13.3333px; line-height: 24px;">Execution results: as can be seen, the second order information, there are two OrderDetail information<br></span></p></p> <p><p><span style="font-size: 13.3333px; line-height: 24px;"></span></p></p> <p><p><span style="font-size: 13.3333px; line-height: 24px;">3. Resultmap Inheritance</span></p></p> <p><p><span style="font-size: 13.3333px; line-height: 24px;">The inheritance of Resultmap can be used in the MyBatis mapper.xml configuration File. As follows:<br></span></p></p> <p><p><span style="font-size: 13.3333px; line-height: 24px;">Inquire about orders, order details, and user information for Orders. We already have orderusermap this resultmap to receive orders and user Information. The following inherits this Resultmap:</span></p></p> <pre class="brush:xml;toolbar: true; auto-links: false;"><!--  orders, Order Details and user information  --><resultmap id= "orderdetailsanduser"  type= "mybatis_ B.model.order " extends=" orderusermap ">  <collection property=" orderDetails "  Oftype= "mybatis_b.model.OrderDetail" >  <id property= "orderdetailid"  column= " Orderdetailid "/>  <result property=" item_id " column=" item_id "/>  < result property= "item_number"  column= "item_number"/>  <result property= "item_ Price " column=" Item_price "/>  </collection></resultmap>  <select  id= "findorderdetailsanduser"  resultmap= "orderdetailsanduser" >  select orders.*, users.*, orderdetail.orderdetailid, orderdetail.item_id,   orderdetail.item_number,  orderDetail.item_price from orders, users, orderDetail   where  Orders.user_id = users.userid&nbsP;and orders.orderid = orderdetail.order_id</select> </pre> <p><p><span style="font-size: 13.3333px; line-height: 24px;"></span>The code orderdetailsanduser This resultmap inherits the orderusermap, so the order and user property configurations can be omitted, reducing the amount of code.<br></p></p><p><p>Use of MyBatis four (ii) (MAPPER.XML)</p></p></span>

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.