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:
- Which is the main table of the query? Order Form
- 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.
select orders.*, user . , user . ' sex ' , user . , OrderDetail. ' id ' orderdetail_id, OrderDetail. ' items_id ' , OrderDetail. ' Items_num ' , OrderDetail. ' orders_id ' from orders, user , OrderDetail where orders. ' user_id ' =user . ' id ' and orders. ' id ' = orderdetail. ' orders_id '
This allows us to query out 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, it 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 list<orderdetail> OrderDetails
Property in the Orders.java class to encapsulate the order line items (which is simpler, the code is not posted), The order information is eventually mapped to orders, and the order details for that order are mapped to the OrderDetails attribute in orders (this is a bit like hibernate, and if it's hibernate, A list of OrderDetail is also maintained in the Orders class.
With this idea, the next step is to start writing the mapping file.
<select 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 map order information and associated user information as above, inherit the above- <!--Configure the associated order details-- <collection property ="OrderDetails" ofType=" Mybatis.po.Orderdetail "> <ID Column= "orderdetail_id" property ="id"/> <result column= "items_id" property ="Itemsid"/> <result column= "items_num" property ="Itemsnum"/ > <result column= "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:
publicinterface UserMapperOrders { //省去不相关代码 //查询订单(关联用户)及订单明细 publicfindOrdersAndOrderDetailResultMapthrows Exception;}
At this point, a one-to-many mapping is written, and the following tests:
@TestpublicvoidtestFindOrdersAndOrderDetailResultMapthrows Exception { SqlSession sqlSession = sqlSessionFactory.openSession(); UserMapperOrders userMapperOrders = 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.
Related reading: http://blog.csdn.net/column/details/smybatis.html
Learning Note Source: Https://github.com/eson15/MyBatis_Study
-Willing to share and progress together!
--My Blog home: http://blog.csdn.net/eson_15
"MyBatis Learning 09" One of the advanced mappings to multi-query