高級映射-一對多查詢-多對多查詢

一 一對多查詢-概述
1 需求
  • 查詢訂單及訂單明細
2 sql語句:
  • 確定主查詢表:訂單表 orders
  • 確定關(guān)聯(lián)查詢表:訂單明細表 Orderdetail
    SELECT
     orders.*,
     user.username,
     user.sex,
     user.address,
     orderdetail.id orderdetail_id,
     orderdetail.items_id,
     orderdetail.item_num,
     orderdetail.orders_id
    FROM
     orders,
     USER,
     orderdetail
    WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id
sql語句執(zhí)行結(jié)果
3 要求
  • 對orders映射不能出現(xiàn)重復(fù)記錄
4 思路
  • 在orders.java類中添加List<orderDetail>orderDetail屬性
  • 最終會將訂單信息映射到orders中,訂單所對應(yīng)的訂單明細映射到orders中的ordersDetail屬性中。
  • 映射成的orders記錄數(shù)為三條(orders信息不重復(fù))
  • 每個orders中的orderDetails屬性存儲了該訂單所對應(yīng)的訂單明細
二 一對多查詢-實現(xiàn)
1 在orders.java類中添加List<OrderdetailBean>OrderdetailBean屬性,并生成get和set方法
OrdersBean添加List集合
2 OrdersMapperCustom
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.OrdersMapperCustom">
    <!--訂單查詢關(guān)聯(lián)用戶的resultMap-->
    <resultMap id="OrdersUserResultMap" type="entity.OrdersBean">
        <!--配置映射的訂單信息-->
        <id column="id" property="id"></id>
        <result column="user_id" property="user_id"></result>
        <result column="number" property="number"></result>
        <result column="createtime" property="createtime"></result>
        <result column="note" property="note"></result>
        <!--配置映射的關(guān)聯(lián)的用戶信息-->
        <!--association:用于映射關(guān)聯(lián)查詢單個對象的信息
        property:要將關(guān)聯(lián)查詢的用戶信息映射到Orders中哪個屬性-->
        <association property="userBean" javaType="entity.UserBean">
            <!--id:關(guān)聯(lián)查詢用戶的唯一標識
            column:指定唯一標識用戶信息的列
            javaType:映射到user的那個屬性-->
            <id column="user_id" property="id"></id>
            <result column="username" property="username"></result>
            <result column="sex" property="sex"></result>
            <result column="address" property="address"></result>
        </association>
    </resultMap>
    <!--查詢訂單關(guān)聯(lián)用戶及訂單明細,使用resultMap-->
    <resultMap id="OrdersAndOrderdetailResultMap" type="entity.OrdersBean" extends="OrdersUserResultMap">
        <!--訂單信息-->
        <!--用戶信息-->


        <!--明細信息
        一個訂單關(guān)聯(lián)查詢出多條明細,要使用collection進行映射
        collection:對關(guān)聯(lián)查詢到的多條記錄映射到集合對象
        property:將關(guān)聯(lián)查詢到的多條記錄映射到OrdersBean屬性
        ofType:指定映射到集合屬性的poio的類型
        -->
        <collection property="orderdetailBeans" ofType="entity.OrderdetailBean">
            <!--id:訂單明細唯一標識-->
            <id column="orderdetail_id" property="id"></id>
            <result column="items_id" property="orders_id"></result>
            <result column="items_id" property="items_id"></result>
            <result column="orders_id" property="orders_id"></result>
        </collection>
    </resultMap>
    <!--查詢訂單關(guān)聯(lián)查詢用戶信息,使用resultType-->
    <select id="findOrdersUser" resultType="pojo.OrdersCustom">
    SELECT orders.*,user.username,user.sex,user.address FROM user,orders WHERE orders.user_id = user.id
</select>
    <!--查詢訂單關(guān)聯(lián)查詢用戶信息,使用resultMap-->
    <select id="findOrdersUserResultMap" resultMap="OrdersUserResultMap">
    SELECT orders.*,user.username,user.sex,user.address FROM user,orders WHERE orders.user_id = user.id
</select>
    <!--查詢訂單關(guān)聯(lián)用戶及訂單明細,使用resultMap-->
    <select id="findOrdersAndOrderdetailResultMap" resultMap="OrdersAndOrderdetailResultMap">
    SELECT
     orders.*,
     user.username,
     user.sex,
     user.address,
     orderdetail.id orderdetail_id,
     orderdetail.items_id,
     orderdetail.item_num,
     orderdetail.orders_id
    FROM
     orders,
     USER,
     orderdetail
    WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id
    </select>
</mapper>
3 OrdersMapperCustom接口
public List<OrdersBean> findOrdersAndOrderdetailResultMap() throws Exception;
4 OrdersMapperTest 測試
    @Test
    public void findOrdersAndOrderdetailResultMap() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);
        List<OrdersBean> ordersBeans = ordersMapperCustom.findOrdersAndOrderdetailResultMap();
        System.out.println(ordersBeans);
    }
測試結(jié)果
三 多對多查詢-概述
1 需求
  • 查詢用戶及用戶購買商品信息
2 sql語句

查詢主表是:用戶表
關(guān)聯(lián)表:由于用戶和商品沒有直接關(guān)聯(lián),通過訂單和訂單明細進行關(guān)聯(lián),所以關(guān)聯(lián)表orders,orderdetail,items

SELECT
     orders.*,
     user.username,
     user.sex,
     user.address,
     orderdetail.id orderdetail_id,
     orderdetail.items_id,
     orderdetail.item_num,
     orderdetail.orders_id,
     items.name items_name,
     items.detail items_detail,
     items.price items_price
    FROM
     orders,
     USER,
     orderdetail,
     items
    WHERE orders.user_id = user.id AND orderdetail.orders_id=orders.id AND orderdetail.items_id = items.id
3 思路
  • 將用戶信息映射到user中。
  • 在UserBean類中添加訂單列表屬性List<OrdersBean> ordersBean,將用戶創(chuàng)建的訂單映射到ordersBean。
  • 在OdersBean中添加訂單明細列表屬性List<OrderDetailBean> orderDetailBeans,將訂單的明細映射到orderDetailBeans。
  • 在OrderDetail中添加items屬性ItemsBean itemsBean,將訂單明細所對應(yīng)的商品映射到itemsBean。
四 多對多查詢-實現(xiàn)
1 配置映射
將用戶信息映射到user中

在Oders中添加訂單明細列表

在OrderDetail中添加items屬性
2 OrdersMapperCustom
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper
        PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="dao.OrdersMapperCustom">

    <!--查詢用戶及用戶購買商品信息-->
    <resultMap id="UserAndItemsResultMap" type="entity.UserBean">
        <!--用戶信息-->
        <id column="id" property="id"></id>
        <result column="username" property="username"></result>
        <result column="sex" property="sex"></result>
        <result column="address" property="address"></result>
        <!--訂單信息-->
        <collection property="ordersBeans" ofType="entity.OrdersBean">
            <id column="id" property="id"></id>
            <result column="user_id" property="user_id"></result>
            <result column="number" property="number"></result>
            <result column="createtime" property="createtime"></result>
            <result column="note" property="note"></result>
            <!--訂單明細-->
            <collection property="orderdetailBeans" ofType="entity.OrderdetailBean">
                <id column="orderdetail_id" property="id"></id>
                <result column="items_id" property="orders_id"></result>
                <result column="items_id" property="items_id"></result>
                <result column="orders_id" property="orders_id"></result>
                <!--商品信息-->
                <association property="itemsBean" javaType="entity.ItemsBean">
                <id column="items_id" property="id"></id>
                    <result column="items_name" property="name"></result>
                    <result column="items_detail" property="detail"></result>
                    <result column="items_price" property="price"></result>
                </association>
            </collection>
        </collection>
    </resultMap>
 
    <!--查詢用戶及用戶購買商品信息-->
    <select id="findUserAndItemsResultMap" resultMap="UserAndItemsResultMap">
  SELECT
 orders.*,
 user.username,
 user.sex,
 user.address,
 orderdetail.id orderdetail_id,
 orderdetail.items_id,
 orderdetail.item_num,
 orderdetail.orders_id,
 items.name items_name,
 items.detail items_detail,
 items.price items_price
FROM
 orders,
USER,
 orderdetail,
 items
WHERE
 orders.user_id = user.id AND orderdetail.orders_id=orders.id AND orderdetail.items_id = items.id
    </select>
</mapper>
3 接口
    public List<UserBean> findUserAndItemsResultMap() throws Exception;
4 測試
    @Test
    public void findUserAndItemsResultMap() throws Exception {
        SqlSession sqlSession = sqlSessionFactory.openSession();
        OrdersMapperCustom ordersMapperCustom = sqlSession.getMapper(OrdersMapperCustom.class);
        List<UserBean> userBeans  = ordersMapperCustom.findUserAndItemsResultMap();
        System.out.println(userBeans);
    }
執(zhí)行結(jié)果
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容