mybatis Getting Screenshot four (order item data model one to one, one to many, many to many)

 

 

---------------------------------

One query

Query order information, information related to query the user to create an order of

1. Advanced Mapping - one query - use resultType

 

 

2. Advanced Mapping - one query - use resultMap

 

 

In mapper.xml defined mapping ResultMap

 

<!-- 订单查询关联用户的resultMap
    将整个查询的结果映射到cn.itcast.mybatis.po.Orders中
     -->
    <resultMap type="cn.itcast.mybatis.po.Orders" id="OrdersUserResultMap">
        <!-- 配置映射的订单信息 -->
        <!-- id:指定查询列中的唯 一标识,订单信息的中的唯 一标识,如果有多个列组成唯一标识,配置多个id
            column:订单信息的唯 一标识 列
            property:订单信息的唯 一标识 列所映射到Orders中哪个属性
          -->
        <id column="id" property="id"/>
        <result column="user_id" property="userId"/>
        <result column="number" property="number"/>
        <result column="createtime" property="createtime"/>
        <result column="note" property=note/>
        
        <!-- 配置映射的关联的用户信息 -->
        <!-- association:用于映射关联查询单个对象的信息
        property:要将关联查询的用户信息映射到Orders中哪个属性 javaType:表示该属性对应的实体类型
     -->
        <association property="user"  javaType="cn.itcast.mybatis.po.User">
            <!-- id:关联查询用户的唯 一标识
            column:指定唯 一标识用户信息的列 指查询出来的列
            property:映射到user的哪个属性
             -->
            <id column="user_id" property="id"/>
            <result column="username" property="username"/>
            <result column="sex" property="sex"/>
            <result column="address" property="address"/>
        
        </association>
    </resultMap>

 

 

 

 resultType and resultMap achieve one query Summary

 

resultType: Use resultType achieve a more simple, if not included pojo check out the column names, column names need to increase the corresponding attribute to complete the mapping.

If there are no special requirements of query results recommended resultType.

 

resultMap: resultMap need to be defined separately, to achieve a little trouble, if there are special requirements for query results, can be done using resultMap query attribute mapping pojo of association.

 

resultMap can implement lazy loading, resultType can not be achieved lazy loading.

--------------------------------------------------

Many queries

 

In-one on the basis of the above, add order details associated with the query

Determine the main query table: Table Orders

Determine the association lookup table: Order Details

Lookup table to determine the association: user table

Entity classes:

sql statement and the corresponding results:

mapper.xml configured resultMap

 

  <!--
        查询订单信息 关联查询用户以及订单明细信息 因为订单和用户的ResultMap已经在上面定义所以这里使用extends之后
        只需要配置订单明细的映射关系即可
    -->
        <resultMap type="org.mybatis.po.Orders" id="OrdersAndUserAndOrderDetailResultMap" extends="OrdersUserResultMap">
        <!-- 订单信息 -->
        <!-- 用户信息 -->
        <!-- 使用extends继承,不用在中配置订单信息和用户信息的映射 -->
        
        
        <!-- 订单明细信息
        一个订单关联查询出了多条明细,要使用collection进行映射
        collection:对关联查询到多条记录映射到集合对象中
        property:将关联查询到多条记录映射到org.mybatis.po.Orders哪个属性
        ofType:指定映射到list集合属性中pojo的类型
         -->
         <collection property="orderdetailList" ofType="org.mybatis.po.Orderdetail">
             <!-- id:订单明细唯 一标识
             property:要将订单明细的唯 一标识 映射到org.mybatis.po.Orderdetail的哪个属性
               -->
             <id column="orderdetailid" property="id"/>
            <result column="orders_id" property="ordersId"/>
            <result column="items_id" property="itemsId"/>
            <result column="items_num" property="itemsNum"/>
         </collection>
    </resultMap>

 

Object statement:

 

<!-- 查询订单信息 关联查询用户以及订单明细信息  mysql中注释:#-->
    <select id="findOrdersAndUserAndOrderDetailResultMap" resultMap="OrdersAndUserAndOrderDetailResultMap">
        select orders.*,#订单信息
        user.username,user.sex,user.address,#用户数据
        orderdetail.id as
        orderdetailid,orderdetail.orders_id,orderdetail.items_id,orderdetail.items_num#订单明细数据
        from orders,user,orderdetail
        where orders.user_id=user.id and
        orders.id=orderdetail.orders_id
    </select>

 

 mapper Interface

Test categories:

Breakpoint debugging:

----

 

------------------------------------------------

 Many to many query

sql statement: check out only one user corresponds to two orders corresponding to two per order details every detail corresponds to a commodity

Entity classes:

Table Relationships:

 

Object statement:

 

  <!-- 查询用户及其购买的商品信息  -->
    <select id="findUserAndItemsResultMap" resultMap="UserAndItemsResultMap">
        select
        orders.*,#订单信息
        user.username,user.sex,user.address,#用户数据
        orderdetail.id
        as
        orderdetailid,orderdetail.orders_id,orderdetail.items_id,orderdetail.items_num,#订单明细数据
        items.name,items.price,items.detail#商品数据
        from
        orders,user,orderdetail,items
        where orders.user_id=user.id and
        orders.id=orderdetail.orders_id and orderdetail.items_id=items.id
    </select>

 

resultmap definition:

 

<!-- 查询用户及购买的商品  type写的是user类的全路径  把数据映射到user中   -->
    <resultMap type="org.mybatis.po.User" id="UserAndItemsResultMap">
        <!-- 用户信息 -->
        <id column="user_id" property="id" />
        <result column="username" property="username" />
        <result column="sex" property="sex" />
        <result column="address" property="address" />

        <!-- 订单信息一个用户对应多个订单,使用collection映射-->
        <collection property="ordersList" ofType="org.mybatis.po.Orders">
            <id column="id" property="id" />
            <result column="user_id" property="userId" />
            <result column="number" property="number" />
            <result column="createtime" property="createtime" />
            <result column="note" property="note" />
            
            <!-- 订单明细一个订单包括 多个明细  -->
            <collection property="orderdetailList" ofType="org.mybatis.po.Orderdetail">
                <id column="orderdetailid" property="id" />
                <result column="orders_id" property="ordersId" />
                <result column="items_id" property="itemsId" />
                <result column="items_num" property="itemsNum" />
                
                <!-- 商品信息一个订单明细对应一个商品-->
                <association property="items" javaType="org.mybatis.po.Items">
                    <id column="items_id" property="id"></id>
                    <result column="name" property="name" />
                    <result column="price" property="price" />
                    <result column="detail" property="detail" />
                </association>
                
            </collection>
            
        </collection>
        
    </resultMap>
 
 

Test categories:

Breakpoint debugging:

Check out only one user

 

 Corresponding to the two orders

 

 Each line corresponds to detail 2

 

 Each corresponds to a commodity breakdown

---------------------------------------------------------------

 

Published 393 original articles · won praise 41 · views 260 000 +

Guess you like

Origin blog.csdn.net/qq_32440951/article/details/105182633