Mybatis高级-resultMap之collection聚集
From: https://aodeng.cc/archives/mybatisgaoji
簡介
聚集元素用來處理“一對多”的關(guān)系。需要指定映射的Java實體類的屬性,屬性的javaType(一般為ArrayList);列表中對象的類型ofType(Java實體類);對應(yīng)的數(shù)據(jù)庫表的列名稱;
額,估計這樣說大家聽不懂,簡單的意思就是把兩張表聯(lián)系起來,用于解決一些奇怪的需求
代碼
1.定義簡單的sql片段
<!-- 基本查詢--><sql id="vo_select">SELECTvo.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.createTimeFROM p_expense voINNER JOIN sys_user_archive sua ON sua.userId=vo.expenseUserIdINNER 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<!--根據(jù)登陸人判斷流程審核步驟的人是否一樣 --><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><!-- 根據(jù)時間查詢 --><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><!-- 注釋掉是避免從我的任務(wù)中查詢顯示一條數(shù)據(jù)--><!-- <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><!-- 根據(jù)報銷人 --><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><!-- 根據(jù)報銷金額范圍查詢 --><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 folwMomentIdfrom sys_user_archive suaINNER JOIN p_expense_flow pef ON pef.checkUserId =sua.userId AND pef.expenseid=#{expenseId2}</select>低調(diào)小熊貓獨家解析
先給大家看一張圖,我就是靠這一張圖學(xué)會的,不要說圖看不清楚,自己ctrl+
ok,這樣聰明的估計就學(xué)會了,不會的看上面代碼吧
額,還是解釋幾個地方
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片段里面條件接收值的字段
總結(jié)
以上是生活随笔為你收集整理的Mybatis高级-resultMap之collection聚集的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么本地使用js或jquery操作co
- 下一篇: 群联PS3111坏硬盘修复记录