1. 程式人生 > >mybatis 使用collection標籤實現一對多查詢(多分頁使用)

mybatis 使用collection標籤實現一對多查詢(多分頁使用)

在使用**ListPage分頁工具類進行分頁操作時,如果使用一對多查詢,會造成查詢出來每一頁資料數量不等於實際規定的每一個數據數量。原因在於,一對多查詢的結果是包含了與子錶鏈接的資料,例如在查詢10個訂單(order_header)資料時,假如有兩個訂單均包含2條訂單條目(order_detail)資料,那麼最終查詢的10條資料只有8條order_header表的資料。


通常,我們為了解決這種問題,首先會先獲取到10條order_header資料,然後在遍歷訂單資料分別獲取每一條訂單資料的訂單條目資料,這樣會浪費資料庫的執行時間,下面使用collection進行查詢的方法,可以避開普通collection查詢造成的問題,也能達到其效果。

Mapper:

<?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="OrderHeaderMapper">
    <resultMap id="OrderView_M" type="OrderView_M">
        <id column="order_id" property="orderID" jdbcType="INTEGER"/>
        <result column="order_number" property="orderNumber" jdbcType="VARCHAR"/>
        <result column="total_amount" property="totalAmount" jdbcType="DECIMAL"/>
	<result column="pay_status_id" property="payStatusID"/>
        <result column="order_status_id" property="orderStatusID"/>
        <collection property="orderDetail" column="order_id" ofType="OrderDetailView_M" javaType="ArrayList" select="getOrderDetailByOrderID">

        </collection>
    </resultMap>

	<resultMap id="OrderDetailView_M" type="OrderDetailView_M">
		<id column="order_detail_id" property="orderDetailID" jdbcType="INTEGER"/>
		<result column="product_name" property="productName" jdbcType="VARCHAR"/>
		<result column="photo_path" property="photoPath" jdbcType="VARCHAR"/>
	</resultMap>
	<!-- page為一個自定義分頁類,其中包括一個pd的map -->
	<select id="getOrderByCustomerListPage" parameterType="page" resultMap="OrderView_M">
        	SELECT
			oh.order_id,
			oh.order_number,
			oh.total_amount,
			oh.pay_status_id,
            		oh.order_status_id
		FROM
			order_header oh
		WHERE oh.pay_status_id = #{pd.orderStatusID}
		ORDER BY oh.order_date DESC
    	</select>

	<select id="getOrderDetailByOrderID" parameterType="java.lang.Integer" resultMap="OrderDetailView_M">
		SELECT
		od.order_detail_id,
		od.product_name,
		od.photo_path,
		od.order_id
		FROM
		order_detail od
		LEFT JOIN product p ON p.product_id = od.product_id
		WHERE od.order_id = #{order_id}
		ORDER BY p.sku
	</select>
</mapper>


public class OrderView_M {
    private Integer orderID;
    private String orderNumber;
    private BigDecimal totalAmount;
    private Integer payStatusID;
    private Integer orderStatusID;
    private List<OrderDetailView_M> orderDetail;

    public Integer getPayStatusID() {
        return payStatusID;
    }

    public Integer getOrderStatusID() {
        return orderStatusID;
    }

    public void setOrderStatusID(Integer orderStatusID) {
        this.orderStatusID = orderStatusID;
    }

    public void setPayStatusID(Integer payStatusID) {
        this.payStatusID = payStatusID;
    }

    public Integer getOrderID() {
        return orderID;
    }

    public void setOrderID(Integer orderID) {
        this.orderID = orderID;
    }

    public String getOrderNumber() {
        return orderNumber;
    }

    public void setOrderNumber(String orderNumber) {
        this.orderNumber = orderNumber;
    }

    public BigDecimal getTotalAmount() {
        return totalAmount;
    }

    public void setTotalAmount(BigDecimal totalAmount) {
        this.totalAmount = totalAmount;
    }

    public List<OrderDetailView_M> getOrderDetail() {
        return orderDetail;
    }

    public void setOrderDetail(List<OrderDetailView_M> orderDetail) {
        this.orderDetail = orderDetail;
    }
}


public class OrderDetailView_M {
    private Integer orderDetailID;
    private String productName;
    private String photoPath;

    public String getProductName() {
        return productName;
    }

    public void setProductName(String productName) {
        this.productName = productName;
    }

    public String getPhotoPath() {
        return photoPath;
    }

    public void setPhotoPath(String photoPath) {
        this.photoPath = photoPath;
    }

    public Integer getOrderDetailID() {
        return orderDetailID;
    }

    public void setOrderDetailID(Integer orderDetailID) {
        this.orderDetailID = orderDetailID;
    }
}


相關推薦

mybatis 使用collection標籤實現一對查詢(使用)

在使用**ListPage分頁工具類進行分頁操作時,如果使用一對多查詢,會造成查詢出來每一頁資料數量不等於實際規定的每一個數據數量。原因在於,一對多查詢的結果是包含了與子錶鏈接的資料,例如在查詢10個訂單(order_header)資料時,假如有兩個訂單均包含2條訂單條目(

MyBatis從入門到精通(十二):使用collection標籤實現巢狀查詢

最近在讀劉增輝老師所著的《MyBatis從入門到精通》一書,很有收穫,於是將自己學習的過程以部落格形式輸出,如有錯誤,歡迎指正,如幫助到你,不勝榮幸! 本篇部落格主要講解使用collection標籤實現巢狀查詢的方法。 1. 需求升級 在上篇部落格中,我們實現了需求:根據使用者id查詢使用者資訊的同時獲取使用

MVC+Bootstrap+Drapper使用PagedList.Mvc支持查詢條件

pagedlist amp new 源代碼 post header this 當前 都是 前幾天做一個小小小項目,使用了MVC+Bootstrap,以前做分頁都是異步加載Mvc部分視圖的方式,因為這個是小項目,就隨便一點。一般的列表頁面,少不了有查詢條件,下面分享下Drap

SpringBoot+Mybatis+ Druid+PageHelper 實現多數據源並

utf 重置數據 count system 配置文件 urn 規模 pos mapper 前言 本篇文章主要講述的是SpringBoot整合Mybatis、Druid和PageHelper 並實現多數據源和分頁。其中SpringBoot整合Mybatis這塊,在之前的的一篇

springmvc配置myBatis,並實現增刪改查、功能

springmvc配置myBatis      myBatis是一個數據庫訪問的外掛,通過jdbc實現。      實現思路:      1.maven引入myBatis <!-- mybatis核心包 --> <dependenc

【筆記】Mybatis高階查詢(四)--使用resultMap的<collection>標籤實現一對查詢

<collection>集合的巢狀結果對映就是指通過一次SQL查詢將所有的結果查詢出來,然後對映到不同的物件中。在一對多的關係中,主表一條資料會對應關聯表的多條資料。因此一般查詢時會查詢出多條結果,按照一對多的資料對映時,最終的結果數會小於等於查詢的總記錄數。

mybatis xml中實現一對查詢時, 子查詢個引數

1、mapper檔案中: List<Object> getXXXXX(@Param("taskId")String taskId,@Param("taskType")String taskType); 2、xml檔案中 <select id="" resultMap

mybatis使用resultMap實現一對查詢 (需求:查詢訂單以及訂單明細)

使用resultMap將查詢出的訂單明細資訊對映Order,java的屬性中 在Orders.java建立訂單明細屬性(一個集合物件,一訂單對應多個訂單明細) public class Orders { private int id;//訂單號 private in

PHP連接數據庫實現條件查詢功能——關於租房頁面的完整實例操作

ots cnblogs 信息 val 租房 btn earch 拼接 round 租房頁面如圖: 代碼如下: <!DOCTYPE html><html> <head> <meta charset="UTF-8"

Spring Data JPA 二:實現表關聯查詢

最近在對JPA的使用過程中發現對於單表的操作很是方便,但是當設計到多表聯查的時候就需要有一些特殊的操作了。 專案中有一個場景是後臺需要做一個分頁的列表查詢,所需要的資料分散在兩張表中,如果是用mybatis的話直接定義resultMap,然後手寫SQL就可以了。而在JPA中就需要用到JPQL

【筆記】Mybatis高階查詢(三)--使用<association>標籤實現巢狀查詢及延遲載入

<association>標籤實現巢狀查詢,需要用到以下屬性: select:另一個對映查詢的ID,Mybatis會額外執行這個查詢獲取巢狀物件的結果。 column:列名或別名,將主查詢中列的結果作為巢狀查詢的引數,配置方式如column=

MyBatis-Plus表聯合查詢並且(3表)

這3張表的關係是   模型表Model  ===> 訓練表Training ===》應用表Application(大概的邏輯是:選擇應用,然後訓練,然後成為模型)如有不理解的可加我微信:17625089935 白天問我一般晚上回 首先我們先建立實體Model(我使用的d

(六)springboot + mybatis plus實現表聯查3.X版本

註明 : 上兩篇文章我們講解了springboot+mybatis-plus對於單表的CRUD和條件構造器的使用方法,但是對於我們的實戰專案中多表聯查也是經常會出現的。今天我們就來說下怎麼在springboot+MP模式下實現多表聯查並分頁。 MP推薦使用的是

SpringData jpa 實現條件動態查詢功能

問題由來: 剛開始使用springdata的時候,只會用findByName這樣的簡單查詢,這樣寫dao層確實非常的快,但是在我們做篩選功能的時候,這樣的查詢似乎很難滿足我們的需求,但是都已經用上的springdata又不想再去寫mybatis這樣在xml裡面判

SpringBoot+Mybatis+ Druid+PageHelper 實現資料來源並

前言 本篇文章主要講述的是SpringBoot整合Mybatis、Druid和PageHelper 並實現多資料來源和分頁。其中SpringBoot整合Mybatis這塊,在之前的的一篇文章中已經講述了,這裡就不過多說明了。重點是講述在多資料來源下的如何配置使用Druid和PageHelper 。 Druid

Entity Framework4.1實現動態條件查詢和排序

EF通用的分頁實現: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2

springboot + mybatis plus實現表聯查

auto score ice get pro err type 實現 app 1 配置分頁插件 public class MybatisPlusConfig { @Bean public PaginationInterceptor paginationInt

MyBatis從入門到精通(十):使用association標籤實現巢狀查詢

最近在讀劉增輝老師所著的《MyBatis從入門到精通》一書,很有收穫,於是將自己學習的過程以部落格形式輸出,如有錯誤,歡迎指正,如幫助到你,不勝榮幸! 本篇部落格主要講解使用association標籤實現巢狀查詢的方法。 1. 明確需求 仍然延用上篇部落格中的需求:根據使用者id查詢使用者資訊的同時獲取該使用

HBase條件及查詢的一些方法

nosql數據庫 應用場景 實現簡單 信息 byte 多條 多個 不可用 寫性能 HBase是Apache Hadoop生態系統中的重要一員,它的海量數據存儲能力,超高的數據讀寫性能,以及優秀的可擴展性使之成為最受歡迎的NoSQL數據庫之一。它超強的插入和讀取性能與它的數據

MyBatis學習總結——實現關聯表查詢(轉)

得到 into primary 字符串 student prim oci ssr ret 原文鏈接:孤傲蒼狼 一、一對一關聯 1.1、提出需求   根據班級id查詢班級信息(帶老師的信息) 1.2、創建表和數據   創建一張教師表和班級表,這裏我們假設一個老師只負責教一個