Mybatis高階-resultMap之collection聚集
版權宣告:本文由 低調小熊貓 發表於 低調小熊貓的部落格
轉載宣告:自由轉載-非商用-非衍生-保持署名,非商業轉載請註明作者及出處,商業轉載請聯絡作者本人qq:2696284032
文章連結: ofollow,noindex" target="_blank">https://aodeng.cc/archives/mybatisgaoji
簡介
聚集元素用來處理“一對多”的關係。需要指定對映的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>
低調小熊貓獨家解析
先給大家看一張圖,我就是靠這一張圖學會的

ok,這樣聰明的估計就學會了,不會的看上面程式碼吧
額,還是解釋幾個地方
<collection property="checkers"ofType="com.account.web.vo.admin.system.SysUserArchiveVo" select="findChecker3" column="{expenseId2=expenseId}"> </collection>