From: https://aodeng.cc/archives/mybatisgaoji

简介

聚集元素用来处理“一对多”的关系。需要指定映射的Java实体类的属性,属性的javaType(一般为ArrayList);列表中对象的类型ofType(Java实体类);对应的数据库表的列名称;
额,估计这样说大家听不懂,简单的意思就是把两张表联系起来,用于解决一些奇怪的需求

代码

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<!--根据登陆人判断流程审核步骤的人是否一样 --><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 folwMomentIdfrom sys_user_archive suaINNER 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高级-resultMap之collection聚集相关推荐

  1. mybatis resultMap之collection聚集两种实现方式

    最近做得项目用到了MyBatis处理一对多的映射关系,下面的两个方法中用到了集合的嵌套查询方法,下面仔细学习一下这两种方式 聚集元素用来处理"一对多"的关系.需要指定映射的Java ...

  2. mybatis高级映射(一对一,一对多,多对多)

    http://www.cnblogs.com/selene/p/4627446.html 阿赫瓦里 生命对于某些人来说,一直都是美丽的,因为这些人的一生都在为某个梦想而奋斗!!! 博客园 首页 新随笔 ...

  3. 关于mybatis中mapper文件resultMap中collection和association的使用

    mybatis mapper配置文件结果集映射resultMap中collection属性(一对多关系结果集映射)和association属性(多对一关系结果集映射)理解: collection的使用 ...

  4. Mybatis的ResultMap的使用

    本篇文章通过一个实际工作中遇到的例子开始吧: 工程使用Spring+Mybatis+Mysql开发.具体的业务逻辑很重,对象之间一层一层的嵌套.和数据库表对应的是大量的model类,而和前端交互的是V ...

  5. SpringBoot整合Mybatis(高级)

    SpringBoot整合Mybatis(高级) 文章目录 SpringBoot整合Mybatis(高级) 前言 基础环境配置 增删改查 ResultMap 复杂查询 多对一 一对多 动态SQL if ...

  6. Mybatis高级应用 延迟加载

    通过前面的内容,可以知道通过resultMap元素可以实现mybatis的高级映射输出,association可以实现一对一高级映射,collection可以实现一对多高级映射,而associatio ...

  7. 2.4.3 Mybatis 高级查询, 复杂映射, 返回主键, 动态SQL if, set, foreach, 核心配置文件深入,plugins标签, 多表查询, 嵌套查询

    目录 Mybatis 复杂映射&配置文件深入 一 Mybatis高级查询 1.1 ResutlMap属性 1.2 多条件查询(三种) 1.3 模糊查询 二 Mybatis映射文件深入 2.1 ...

  8. mybatis高级映射多对多查询(二)

    在这篇博客中,我来介绍下mybatis中的多对多查询的案例,在mybatis中,如何使用ResultMap来实现多对多的查询? 案例:一个user可以有很多role,一个role可以有很多entitl ...

  9. 【Mybatis高级映射】一对一映射、一对多映射、多对多映射

    前言 当我们学习heribnate的时候,也就是SSH框架的网上商城的时候,我们就学习过它对应的高级映射,一对一映射,一对多映射,多对多映射.对于SSM的Mybatis来说,肯定也是差不多的.既然开了 ...

最新文章

  1. C# 如何创建Excel多级分组
  2. for循环嵌套 简单优化
  3. Qt Creator如何恢复默认布局
  4. html文件上传协议,HTTP 上传文件的协议格式
  5. asp.net学习历程。
  6. maven web项目保存log4j日志到WEB-INF
  7. 19、Java并发编程:线程间协作的两种方式:wait、notify、notifyAll和Condition
  8. Pytorch基础(八)——正则化
  9. 前端学习(2465):ajax发送请求
  10. 使用mit协议的编程语言_从使用诺基亚功能手机进行编程到如何为MIT初创公司工作的过程如何
  11. Hackathons 101-以及为什么要考虑参加
  12. 2012《Linux杂志》读者选择奖 (Readers' Choice Awards 2012- Linux Journal)
  13. filter和sessionListener实现session超时退出功能,过滤掉轮询请求
  14. 【服务器】服务器安全防护、防止服务器攻击和保护措施
  15. RAID 磁盘阵列详解,RAID分类及优缺点
  16. linux 微信安装
  17. style是什么意思
  18. Java学习决心计划书
  19. IPA 包不经过APP Store直接发布到网站供用户下载安装
  20. 2019FME博客大赛——【零编码】利用FME实现城市高德路况抓取及增量更新——以深圳为例

热门文章

  1. python记录日志_5分钟内解释日志记录—使用Python演练
  2. 数据预处理 泰坦尼克号_了解泰坦尼克号数据集的数据预处理
  3. 282. 给表达式添加运算符
  4. leetcode 493. 翻转对(分治算法)
  5. leetcode1424. 对角线遍历 II(排序)
  6. leetcode面试题 10.03. 搜索旋转数组(二分法)
  7. leetcode784. 字母大小写全排列(回溯)
  8. 为什么不应该使用(长期存在的)功能分支
  9. javascript 分号_让我们谈谈JavaScript中的分号
  10. 使用AxiosJavaScript中的简单HTTP请求