1. 程式人生 > >Mybatis高階-resultMap之collection聚集

Mybatis高階-resultMap之collection聚集

簡介

聚集元素用來處理“一對多”的關係。需要指定對映的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中物件的型別ofType(Java實體類);對應的資料庫表的列名稱;
額,估計這樣說大家聽不懂,簡單的意思就是把兩張表聯絡起來,用於解決一些奇怪的需求

程式碼

1.定義簡單的sql片段

<!-- 基本查詢-->
    <sql id="vo_select">
        SELECT
            vo.expenseId,
            vo.projectId,
            vo.expenseUserId,
            sua.realName as expenseUserName,
            vo.expenseTime,
            vo.expenseTypeId,
            vo.beneficialDepartmentId,
            sd.name as beneficialDepartmentName,
            vo.currencyId,
            vo.money,
            vo.paperCheckerId,
            vo.paperCheckerTime,
            vo.paidDepartmentId,
            vo.paidUserId,
            vo.paidTime,
            vo.expenseNote,
            vo.description,
            vo.currentStatus,
            vo.invoiceAmount,
            vo.paperFlag,
            vo.recordFlag,
            vo.createUserId,
            vo.createTime
        FROM p_expense vo
        INNER JOIN sys_user_archive sua ON sua.userId=vo.expenseUserId
        INNER JOIN sys_department sd ON sd.departmentId=vo.beneficialDepartmentId
    </sql> 

2.查詢條件拼接,返回resultMap

<select id="findAll" parameterType="Pasv" resultMap="pexpenseMap">
        <include refid="vo_select"/>
        WHERE 1=1
        <!--根據登陸人判斷流程稽核步驟的人是否一樣 -->
        <if test="vo.userId !=null and vo.userId !=''">
        AND
        (
         EXISTS(
             select 0 from p_expense_flow pef 
             where pef.flag=0 and pef.checkUserIds like concat('%#',#{vo.userId},'#%')
             AND pef.expenseID=vo.expenseId
         )
        )
        </if>

        <!-- 根據時間查詢 -->
        <if test="vo.searchStartTime !=null and vo.searchStartTime !=''">
            AND vo.createTime >=DATE_FORMAT(#{vo.searchStartTime},'%Y-%m-%d')
        </if>
        <if test="vo.searchEndTime !=null and vo.searchEndTime !=''">
            AND vo.createTime <DATE_FORMAT(date_add(#{vo.searchEndTime}, INTERVAL 1 day),'%Y-%m-%d')
        </if>
        <!-- 註釋掉是避免從我的任務中查詢顯示一條資料-->
        <!-- <if test="null!=vo.expenseId and ''!=vo.expenseId">
            AND vo.expenseId = #{vo.expenseId}
        </if> -->
        <if test="null!=vo.projectId and ''!=vo.projectId">
            AND vo.projectId = #{vo.projectId}
        </if>
        <if test="null!=vo.expenseUserId and ''!=vo.expenseUserId">
            AND vo.expenseUserId = #{vo.expenseUserId}
        </if>
        <if test="null!=vo.expenseTime and ''!=vo.expenseTime">
            AND vo.expenseTime = #{vo.expenseTime}
        </if>
        <if test="null!=vo.expenseTypeId and ''!=vo.expenseTypeId">
            AND vo.expenseTypeId = #{vo.expenseTypeId}
        </if>
        <if test="null!=vo.beneficialDepartmentId and ''!=vo.beneficialDepartmentId">
            AND vo.beneficialDepartmentId = #{vo.beneficialDepartmentId}
        </if>
        <if test="null!=vo.currencyId and ''!=vo.currencyId">
            AND vo.currencyId = #{vo.currencyId}
        </if>
        <if test="null!=vo.money and ''!=vo.money">
            AND vo.money = #{vo.money}
        </if>
        <!-- 根據報銷人 -->
        <if test="vo.searchName !=null and vo.searchName !=''">
            AND sua.realName LIKE CONCAT(CONCAT('%', #{vo.searchName}),'%')
        </if>
        <if test="null!=vo.paperCheckerId and ''!=vo.paperCheckerId">
            AND vo.paperCheckerId = #{vo.paperCheckerId}
        </if>
        <if test="null!=vo.paperCheckerTime and ''!=vo.paperCheckerTime">
            AND vo.paperCheckerTime = #{vo.paperCheckerTime}
        </if>
        <if test="null!=vo.paidDepartmentId and ''!=vo.paidDepartmentId">
            AND vo.paidDepartmentId = #{vo.paidDepartmentId}
        </if>
        <if test="null!=vo.paidUserId and ''!=vo.paidUserId">
            AND vo.paidUserId = #{vo.paidUserId}
        </if>
        <if test="null!=vo.paidTime and ''!=vo.paidTime">
            AND vo.paidTime = #{vo.paidTime}
        </if>
        <if test="null!=vo.description and ''!=vo.description">
            AND vo.description = #{vo.description}
        </if>
        <if test="null!=vo.currentStatus and ''!=vo.currentStatus">
            AND vo.currentStatus = #{vo.currentStatus}
        </if>
        <if test="null!=vo.invoiceAmount and ''!=vo.invoiceAmount">
            AND vo.invoiceAmount = #{vo.invoiceAmount}
        </if>
        <if test="null!=vo.paperFlag and ''!=vo.paperFlag">
            AND vo.paperFlag = #{vo.paperFlag}
        </if>
        <if test="null!=vo.recordFlag and ''!=vo.recordFlag">
            AND vo.recordFlag = #{vo.recordFlag}
        </if>
        <if test="null!=vo.createUserId and ''!=vo.createUserId">
            AND vo.createUserId = #{vo.createUserId}
        </if>
        <if test="null!=vo.createTime and ''!=vo.createTime">
            AND vo.createTime = #{vo.createTime}
        </if>
        <if test="null!=vo.enabled and ''!=vo.enabled">
            AND vo.enabled = #{vo.enabled}
        </if>
        <!-- 根據報銷金額範圍查詢 -->
        <if test="vo.searchStartMoney !=null and vo.searchStartMoney !='' and vo.searchStartMoney!=0">
         and vo.money <![CDATA[ >= ]]> #{vo.searchStartMoney}
        </if>
        <if test="vo.searchEndMoney !=null and vo.searchEndMoney !='' and vo.searchEndMoney!=0">
         and vo.money <![CDATA[ <= ]]> #{vo.searchEndMoney}
        </if>
    </select>

3.定義resultMap,用於上面返回的resultMap,重點在於collection,先看程式碼,我下面解釋

<resultMap type="com.account.web.vo.project.PExpenseVo" id="pexpenseMap">
        <id column="expenseId" property="expenseId"/>
        <result column="projectId" property="projectId"/>
        <result column="expenseUserId" property="expenseUserId"/>
        <result column="expenseUserName" property="expenseUserName"/>
        <result column="expenseTime" property="expenseTime"/>
        <result column="expenseTypeId" property="expenseTypeId"/>
        <result column="beneficialDepartmentId" property="beneficialDepartmentId"/>
        <result column="beneficialDepartmentName" property="beneficialDepartmentName"/>
        <result column="currencyId" property="currencyId"/>
        <result column="money" property="money"/>
        <result column="paperCheckerId" property="paperCheckerId"/>
        <result column="paperCheckerTime" property="paperCheckerTime"/>
        <result column="paidDepartmentId" property="paidDepartmentId"/>
        <result column="paidUserId" property="paidUserId"/>
        <result column="paidTime" property="paidTime"/>
        <result column="expenseNote" property="expenseNote"/>
        <result column="description" property="description"/>
        <result column="currentStatus" property="currentStatus"/>
        <result column="invoiceAmount" property="invoiceAmount"/>
        <result column="paperFlag" property="paperFlag"/>
        <result column="recordFlag" property="recordFlag"/>
        <result column="createUserId" property="createUserId"/>
        <result column="createTime" property="createTime"/>
        <collection property="checkers"  ofType="com.account.web.vo.admin.system.SysUserArchiveVo"
         select="findChecker3" column="{expenseId2=expenseId}">
        </collection>
    </resultMap>

4.定義collection用的sql片段

<select id="findChecker3" resultType="com.account.web.vo.admin.system.SysUserArchiveVo">
        select pef.expenseFlowId,sua.userId,sua.realName,pef.folwMomentId as folwMomentId
        from sys_user_archive sua
        INNER JOIN p_expense_flow pef ON pef.checkUserId =sua.userId 
        AND pef.expenseid=#{expenseId2}
    </select>

低調小熊貓獨家解析

先給大家看一張圖,我就是靠這一張圖學會的,不要說圖看不清楚,自己ctrl+

ok,這樣聰明的估計就學會了,不會的看上面程式碼吧
額,還是解釋幾個地方

<collection property="checkers"  ofType="com.account.web.vo.admin.system.SysUserArchiveVo"
         select="findChecker3" column="{expenseId2=expenseId}">
        </collection>

1.property=”checkers”就是上面那個resultMap返回的實體類裡面封裝的一個集合屬性。

2.ofType=”com.account.web.vo.admin.system.SysUserArchiveVo”就是集合的型別

3.select=”findChecker3”就是第四步使用的sql片段的id

4.column=”{expenseId2=expenseId}”那,這個就比較重要了,expenseId就是上面resultMap的欄位的名字,expenseId2就是下面sql片段裡面條件接收值的欄位

相關推薦

Mybatis高階-resultMapcollection聚集

簡介 聚集元素用來處理“一對多”的關係。需要指定對映的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中物件的型別ofType(Java實體類);對應的資料庫表的列名稱; 額,估計這樣說大家聽不懂,簡單的意思就是把兩張表聯絡起來,用於解

resultMapcollection聚集兩種實現方式

最近做得專案用到了MyBatis處理一對多的對映關係,下面的兩個方法中用到了集合的巢狀查詢方法,下面仔細學習一下這兩種方式 聚集元素用來處理“一對多”的關係。需要指定對映的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中物件的型別ofType(Java實體

resultMapcollection聚集

  聚集元素用來處理“一對多”的關係。需要指定對映的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中物件的型別ofType(Java實體類);對應的資料庫表的列名稱; 不同情況需要告訴MyBatis 如何載入一個聚集。My

mybatis中<resultMap>和<collection>標籤的巢狀使用

mybatis中和標籤的巢狀使用 實現功能 為了解決A實體類的其中一個私有屬性b,對應的是另一個實體類B的物件,查詢A的所有資訊; 需求   通過mybatis框架:查詢年級表下的所有班級的詳細資

mybatis查詢優化collection,一對多查詢

上程式碼 //簡單訂單實體 public class Order { private String orderid; private String price; private List<OrderGoods> good

MyBatis(八) resultMap (三) 延遲載入 (懶載入)

繼承第七章的例子: 下面我們講延遲載入 也叫 懶載入。 延遲載入:resultMap 的資料以 樹狀結構 為主,當我們用到分支資料的時候再查下,反之則,不查詢,這樣可以節省資料庫資源。 那麼我在上一個demo的基礎做一點小改進: 1、首先在 myba

【筆記】Mybatis高階查詢(五)--使用resultMap的<collection>進行巢狀查詢及延遲載入

下面例子通過<collection>實現一個通過使用者編號查詢使用者下面的角色及許可權的需求,支援延遲載入。下面以自下而上的過程來實現這樣的巢狀查詢功能。並且這個自下而上的過程中每一個方法都是獨立可用的方法。上層的結果都以下層方法為基礎。所有物件都設定為延遲載入。

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

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

mybatis學習筆記——mybatis的Mapper XML檔案中resultMap屬性

resultMap resultMap:自定義結果集對映規則,自定義某個JavaBean的封裝規則。 id:唯一id,方便引用。 type:自定義規則的Java類。 具體其他屬性詳細資訊和配置程式碼如下: <resultMap id="MyEmp" type="com.te

mybatis-高階結果對映一對多

在一對多的關係中, 主表的資料回對應關聯表中的多條資料。 因此, 查詢時就會查詢出多條結果, 所以, 向類似的情況我們會使用 List 來進行儲存關聯表中獲取到的資訊。 1 資料準備 建立以下的名為 mybatis 的資料庫, 並在其下建立4個表。

mybatis-高階結果對映一對一(多種方式, 有沒提到的你找我)

mybatis的高階結果對映可以很輕鬆的幫助我們處理一對一, 一對多的資料關係。 1 資料準備 1.1 資料庫 建立以下的名為 mybatis 的資料庫, 並在其下建立4個表。 1.2 實體類, 介面和XML 以上為生成的專案結構。 2 一對一對映 建立

【筆記】Mybatis高階查詢--使用resultMap配置一對一對映

1. 使用resultMap配置一對一對映 除了使用Mybatis的自動對映處理一對一巢狀外,還可以在XML對映檔案中配置結果對映。以下例子是用resultMap配置來處理上一節一對一對映的。 在SysUserMapper.xml中增加以下res

Python高階資料結構Collection

本章是Python高階資料結構的第一篇,由於之前沒有接觸過太多的Python版本的資料結構,所以在學習的過程中集百家之長和自己的見解,加以實踐,學習Python。 Python中用到tuple的方法,和注意事項都以程式碼的形式體現,高階之處在與其可以處理特

MyBatis原始碼分析@ResultMap註解詳解

MyBatis原始碼分析之@ResultMap註解詳解 在前一篇文章講**@MapKey註解時,我原想將@ResultMap註解也一起拿出來說一下,但是發現@ResultMap解析載入原始碼非常多,想想就不在一篇文章中講了,分開單獨來說,這一篇就來徹底探索一下@ResultMap**

mybatis query多級List級聯類 collection標籤使用《示例》上

需求:查詢時需要對比stationStatsInfo 充電裝置,介面資訊的基本資訊的最後修改時間,三者只要有一處修改,就認為是最新修改時間,然後與輸入引數lastQueryTime進行對比,最後符合條

MyBatis學習10】高階對映多對多查詢

  本文來總結一下mybatis中的多對多對映,從第8節的文章中可以看出,使用者表和商品表示多對多關係,它們兩的多對多是通過訂單項和訂單明細這兩張表所關聯起來的,那麼這一節主要來總結一下使用者表和商

mybatis聯合查詢 高階結果對映

MyBatis的建立基於這樣一個思想:資料庫並不是您想怎樣就怎樣的。雖然我們希望所有的資料庫遵守第三正規化或BCNF(修正的第三正規化),但它們不是。如果有一個數據庫能夠完美對映到所有應用程式,也將是非常棒的,但也沒有。結果集對映就是MyBatis為解決這些問題而提供

Mybatis高階應用-巢狀查詢association和collection

Mybatis高階應用-巢狀查詢 1. 關聯-association 2. 集合-collection 本文示例領域模型: 訂單:訂單編號、顧客編號,總金額 顧客:顧客編號、顧客姓名、顧客手

mybatisresultMap使用返回分組資料

1. resultMap 1.1 引言 resultMap是mybatis最重要的強大元素。通過描述資料的關係結構,將結果集進行對映到java類或java bean中,達到結果集複雜處理的目的。本文解決的主要問題的分組資料的返回 1.2 問題 假

MyBatis從入門到精通(九):MyBatis高階結果對映一對一對映

最近在讀劉增輝老師所著的《MyBatis從入門到精通》一書,很有收穫,於是將自己學習的過程以部落格形式輸出,如有錯誤,歡迎指正,如幫助到你,不勝榮幸! 本篇部落格主要講解MyBatis中實現查詢結果一對一對映的3種方式: 使用別名實現自動對映 使用resultMap配置 使用resultMap的associ