mybatis advanced -- one-to-many query

  First of all, we still give a requirement: query the order details according to the order id - we know that there can be multiple order details in an order (for students whose needs are not clear, please leave a message or click on the order place on Taobao.com) just know). At this time, one order corresponds to the id of multiple orders. When such a need arises, how should we inquire?

  At this time, our data model is as follows (left), since query users are also our needs, so we expand on the original basis . The data model is as follows (right):

 

    Obviously, it is unreasonable to implement it in the way of resultType. Because we need to create a pojo with both orders and order details, our mybatis framework will map many pojo objects for us (there are as many objects as there are order details).

    So we need to use resultMap for processing. The idea to solve the problem is: add a List field of order details in the orders class to define the type of the list as the Orderdetail type. Then through the configuration file, the obtained data is mapped to the list through the collection tag of resultMap.

    The specific implementation is as follows:

  sql statement

    Determine the main query table: order table

    Determine the associated query table: order details table

You can add order details table association     on the basis of one-to-one query .

    

copy code
 1 SELECT 
 2   orders.*,
 3   USER.username,
 4   USER.sex,
 5   USER.address,
 6   orderdetail.id orderdetail_id,
 7   orderdetail.items_id,
 8   orderdetail.items_num,
 9   orderdetail.orders_id
10 FROM
11   orders,
12   USER,
13   orderdetail
14 WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id
copy code

 

  problem display

    查询完毕以后,我们发现了一个问题:如图所示,我们的id出现了多条,这是因为数据库中对应的多个订单详情共同使用一个订单id导致的。这就导致了每条记录中都会出现一个orders的记录。具体解决办法,开始的时候就说了,这里就不再赘述了。

    

  在orders中添加list订单明细属性

    

 

    定义resultMap

copy code
 1 <!-- 订单及订单明细的resultMap
 2     使用extends继承,不用再重复用户的映射
 3      -->
 4     <resultMap type="cn.itcast.mybatis.po.Orders" id="id" extends="OrdersUserResultMap">
 5         <!-- 订单信息 -->
 6         <!-- 用户信息 -->
 7         <!-- 使用extends继承,不用在中配置订单信息和用户信息的映射 -->
 8         
 9         
10         <!-- 订单明细信息
11         一个订单关联查询出了多条明细,要使用collection进行映射
12         collection:对关联查询到多条记录映射到集合对象中
13         property:将关联查询到多条记录映射到cn.itcast.mybatis.po.Orders哪个属性
14         ofType:指定映射到list集合属性中pojo的类型
15          -->
16          <collection property="orderdetails" ofType="cn.itcast.mybatis.po.Orderdetail">
17              <!-- id:订单明细唯 一标识
18              property:要将订单明细的唯 一标识 映射到cn.itcast.mybatis.po.Orderdetail的哪个属性
19                -->
20              <id column="orderdetail_id" property="id"/>
21              <result column="items_id" property="itemsId"/>
22              <result column="items_num" property="itemsNum"/>
23              <result column="orders_id" property="ordersId"/>
24          </collection>
25         
26     
27     </resultMap>
copy code

    statement definition

    

  mapper

    

  Summarize

    In fact, the collection of resultMap is used to map multiple records of the associated query to a list collection attribute.

Reprinted in: http://www.cnblogs.com/liyasong/p/mybatis_yddcx.html

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324723659&siteId=291194637