【Mybatis進階之多表查詢】MyBatis resultMap 多表查詢

resultMap 用於映射 對象關係的 時使用。 
對照對象的屬性可以很方便的寫出 mapper.xml 映射文件。

下面用一個例子來再次說明resultMap 的映射過程。 
場景如下:(多表關聯查詢) 
需要查詢 多個用戶,當點擊查看是可以查看他的所有的訂單,點擊訂單時可以查看裏面的商品

如果要完成這個需求,對應的實體對象如下:

java類 對象結構(get set 這裏沒寫)

Order//訂單類
    |--int id
    |--int userId
    |--date createTime
    |--User user
            User //用戶信息
                |--int id
                |--String name
                |--String address
                |--List<Order> orderList //該用戶的所有訂單

    |--List<OrderItem> orderItemList//該訂單的詳情記錄
            OrderItem //訂單詳情
                |--int id
                |--orderId     //訂單id
                |--int goodsId //商品id
                |--int number  //購買數量
                |--goods goods
                        goods //商品對象
                            |--id   
                            |--name   
                            |--price

下面對應上面的文件 編寫 Mapper.xml 的 ResultMap映射代碼:

映射文件 OrderDao.xml

    <!-- 獲取用戶訂單和商品詳情 -->
    <!-- Order -->
    <resultMap type="Order" id="findUserAndOrderDetail">
        <id column="id" property="id"/>
        <result column="createTime" property="createTime"/>
        <!-- User user  -->
        <association property="user" javaType="User">
            <id column="userId" property="id"/><!-- 外鍵映射 -->
            <result column="name" property="name"/>
            <result column="address" property="address"/>
        </association>
        <!-- List<Order> orderItemList -->
        <collection property="orderItemList" ofType="OrderItem">
            <id column="orderId" property="id"/><!-- 外鍵映射 -->
            <result column="number" property="number"/>
            <result column="note" property="note"/>
            <!-- goods  -->
            <association property="goods" javaType="goods">
                <id column="goodsId" property="id"/><!-- 外鍵映射 -->
                <result column="goodsName" property="name"/>
                <result column="price" property="price"/>
            </association>
        </collection>
    </resultMap>
<select id="findByName"  resultMap="findUserAndOrderDetail">
        select order.*,
               user.name,user.address
               orderItem.number
               goods.name goodsName,goods.price
        from user,order,orderItem,goods 
        where user.id=order.userId and order.id = orderItem.orderId and goods.id = orderItem.goodsId
    </select>
  • 映射 List 時 使用 <collection oftype="包.對象"/>
  • 映射 對象時 使用 <association javaType="包.對象">
  • 外鍵關聯 使用<id column="goodsId" property="id"/>

接口

public interface OrderDao {
    public List<Orders> findOrderMapById()throws Exception;
}

 

  • 名稱、方法名,返回值,返回類型 做到一致。
  • OrderDao.xml == OrderDao.java (放在同一目錄下)
  • public List<Orders> findOrderMapById()throws Exception;
  • <resultMap type="Order" id="findUserAndOrderDetail">

4、junit測試代碼。

    public void findOrderMapById() throws Exception {
        SqlSession openSession = sqlSessionFactory.openSession();
        OrderDao mapper = openSession.getMapper(OrderDao.class);

        List<Orders> Orders= mapper.findUserAndOrderDetail();

        for(int i=0; i<Orders.size(); i++){
            System.out.println(Orders.get(i));
        }
        openSession.close();
    }

 

轉載內容

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章