Mybatis高级-resultMap之collection聚集

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片段里面条件接收值的字段

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/402568.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Atitit  数据存储的分组聚合 groupby的实现attilax总结

Atitit 数据存储的分组聚合 groupby的实现attilax总结 1. 聚合操作1 1.1. a、标量聚合 流聚合1 1.2. b、哈希聚合2 1.3. 所有的最优计划的选择都是基于现有统计信息来评估3 1.4. 参考资料3 1. 聚合操作 聚合也是我们在写T-SQL语句的时候经常遇到的&#xff0c;我们来分析一下一…

[java理论篇]--java的其他常用API

1、java的正则表达式&#xff1a;常用方法&#xff1a;String matches&#xff08;匹配&#xff09; &#xff1b;String split&#xff08;切割&#xff09;&#xff1b;String replaceAll(替换)&#xff1b;利用正则表达式获取字符创的核心代码&#xff1a;String str“ds…

pojo类无法注入service解决示例

From: https://blog.csdn.net/danielzhou888/article/details/83351913 本示例解决springboot中service无法注入普通jopo的问题。 不啰嗦&#xff0c;直接上代码。 如果该示例帮您解决了问题&#xff0c;请助推哦。 pojo类&#xff1a; package com.scmd.controller;import…

图测试题部分总结.ing

一个无向连通图的生成树是含有该连通图的全部顶点的&#xff08;极小连通子图&#xff09; 在有向图G的拓扑序列中&#xff0c;若顶点Vi在顶点Vj之前&#xff0c;则下列情形不可能出现的是&#xff08;D&#xff09;A&#xff0e;G中有弧<Vi&#xff0c;Vj> B&#xff0e…

mybatis动态sql中的where标签的使用

From: https://blog.csdn.net/wobuaizhi/article/details/81874664 在使用mybatis的动态sql时&#xff0c;有时候遇到根据条件判断添加where后面的筛选条件。 会出现多余的“and”或者“or”&#xff0c;如下&#xff1a; <select id"findBlog" result…

电脑公司 Ghost XP SP3 国庆特别版 v2011.10

电脑公司 Ghost XP SP3 国庆特别版 v2011.10 文件名称:DNGS_GhostXpSp3_v2011.10.iso文件大小:686.59MB &#xff08;719,945,728字节&#xff09;系统格式:NTFSCRC32:DDEBC170MD5:FA264E6241154EE7F80EFED3CFF1C6A2SHA1:FFEB1C13854AA954D9EE384153815FAF52E0747B电脑城系统 …

Linux C 预处理详解

1.预处理程序 按照ANSI标准的定义&#xff0c;预处理程序应该处理以下指令&#xff1a; #if #ifdef #ifndef #else #elif #endif #define #undef #line #error #pragma #include 显然&#xff0c;上述所有的12个预处理指令都以符号#开始&#xff0c;每条预处理指令必须独占一行…

Spring Boot 中使用 @Transactional 注解配置事务管理

From: https://blog.csdn.net/nextyu/article/details/78669997 事务管理是应用系统开发中必不可少的一部分。Spring 为事务管理提供了丰富的功能支持。Spring 事务管理分为编程式和声明式的两种方式。编程式事务指的是通过编码方式实现事务&#xff1b;声明式事务基于 AOP,将…

TCP中间件_Delphi_client

1、界面 1.1、formMain.pas 1.1.1、 object frmMain: TfrmMainLeft 191Top 103Width 542Height 466Caption frmMainColor clBtnFaceFont.Charset DEFAULT_CHARSETFont.Color clWindowTextFont.Height -11Font.Name MS Sans SerifFont.Style []OldCreateOrder False…

python为类定义构造函数

用python进行OO编程时&#xff0c; 经常会用到类的构造函数来初始化一些变量。 class FileData:def __init__(self, data, name, type):self.bits base64.encodestring(data)self.name nameself.type type其中self类似c或者c#的this指针。转载于:https://blog.51cto.com/muz…

Linux C 函数练习

学习函数主要学习的就是函数的声明、定义和调用&#xff0c;下面请看两个例子&#xff0c;来帮助我们学习函数&#xff1a; 题目一&#xff1a; 编写一个函数iswithin()&#xff0c;它接受两个参数&#xff0c;一个是字符&#xff0c;另一个是字符串指针。其功能是如果字符在字…

Java、Mysql、MyBatis 中枚举 enum 的使用

From: https://yulaiz.com/java-mysql-enum/ Java 和 MySql 中都有枚举的概念&#xff0c;合理的使用枚举&#xff0c;可以让代码阅读和数据库数据查询更加直观、高效。那么我们怎么使用呢&#xff0c;什么时候使用&#xff0c;两者之间怎么进行数据关联呢&#xff1f;&#x…

ny12 喷水装置(二)

喷水装置&#xff08;二&#xff09; 时间限制&#xff1a;3000 ms | 内存限制&#xff1a;65535 KB难度&#xff1a;4描述有一块草坪&#xff0c;横向长w,纵向长为h,在它的橫向中心线上不同位置处装有n(n<10000)个点状的喷水装置&#xff0c;每个喷水装置i喷水的效果是让…

http请求头大全

转自http://tools.jb51.net/table/http_header HTTP响应头和请求头信息对照表 HTTP请求头提供了关于请求&#xff0c;响应或者其他的发送实体的信息。HTTP的头信息包括通用头、请求头、响应头和实体头四个部分。每个头域由一个域名&#xff0c;冒号&#xff08;:&#xff09;和…

Linux C 指针练习

题目一、 已知数组内容如下 s[] {1,2,3,4,5,6,7,8,9}&#xff0c;输入一个常数 m(1<m<9)&#xff0c;使得该数组内容顺序后移n个位置。如n 3时&#xff0c;数组后移3个位置后的内容为{7,8,9,1,2,3,4,5,6} 代码如下&#xff1a; [cpp] view plaincopy #include <stdi…

Java重载遇到泛型

今天被问到一个有意思的问题&#xff0c;大家都知道重载的概念吧&#xff1a;一个类中定义同名的方法&#xff0c;参数表不同&#xff08;参数类型&#xff0c;或者参数个数不通&#xff09;&#xff1b; 但是&#xff0c;如果是下面这个两个方法呢 public static int fn(List&…

wordpress插件制作视频教程【资料分享】

2019独角兽企业重金招聘Python工程师标准>>> 一共5集&#xff0c;每一集15分钟左右&#xff0c;适合入门用哦~ 资料地址&#xff1a; http://wordpresshy.com/create-plugin 分集介绍&#xff1a; 1 【教学大纲】 1.介绍什么是插件&#xff1b; 2.插件的文件结…

Java中遍历HashMap的5种方式

From: https://blog.csdn.net/w605283073/article/details/80708943 本教程将为你展示Java中HashMap的几种典型遍历方式。 如果你使用Java8&#xff0c;由于该版本JDK支持lambda表达式&#xff0c;可以采用第5种方式来遍历。 如果你想使用泛型&#xff0c;可以参考方法3。如…

字符串按照单词为单位逆序排列

我们前面已经写过一个简单字符串逆序排序的方法&#xff0c;这里再开一个字符串排序问题&#xff1a; 给定一个字符串“I love China”&#xff0c;编写程序完成以单词为单位的逆序&#xff0c;如"China love I",并要求不使用第三方变量保存数据&#xff0c;但可以使…

博主故事:博客提升了我在口腔行业的影响力

自从我上次谈了谈个人开设博客的目的一文后&#xff0c;今天就有一位博友发来了感谢留言&#xff0c;他说他的博客让他在国内做牙齿矫正行业的有了一定影响力&#xff0c;下月被请到上海讲课&#xff0c;也有不少民营机构前来挖他&#xff0c;还被邀请参加中国口腔医学界最盛大…