sql查询结果多对多转为一对多返回前端
企业表 ent_EnterpriseArchives 有id,企业名称 entName
veh_Vehicle 车辆表,有所属企业id companyId,车辆id,车牌号licPlate
目的是查询企业和车辆的树状结果。如下图,然后返回前端。

执行如下sql得到的结果是:【根据车牌号或者企业名称模糊查询】
SELECT
ent_EnterpriseArchives.id entId,
ent_EnterpriseArchives.entName entName,
veh_Vehicle.id vehId,
veh_Vehicle.licPlate vehPlate
from ent_EnterpriseArchives
JOIN veh_Vehicle
on ent_EnterpriseArchives.id = veh_Vehicle.companyId
and (
ent_EnterpriseArchives.entName like '%杭州%' or veh_Vehicle.licPlate like '%杭州%'
)
ORDER BY ent_EnterpriseArchives.id

【上述数据结果为造的假数据】
所以要对这个sql的查询结果进行去重公司名称,返回前端树状结果。下面的代码即为处理过程:
public ResponseList enterpriseVehicleTree(String paramName, HttpSession session) {
ResponseList response = new ResponseList();
List<Map<String,List<VehVehicleVO>>> resultList = new ArrayList<Map<String,List<VehVehicleVO>>>();
if(paramName == null) {
paramName = ConstantUtil.EMPTYSTRING;
}
try {
List<EnterpriseVehicleTreeVO> tempRes = baseMapper.enterpriseVehicleTree(paramName);//上述sql的查询结果
if(tempRes == null || tempRes.size() < ConstantUtil.INTNUM1) {
return response;
}
Integer tempEntId = null;
Map<String,List<VehVehicleVO>> tempMap = new HashMap<String, List<VehVehicleVO>>();
List<VehVehicleVO> tempListStr = new ArrayList<VehVehicleVO>();
for (int i = 0 ; i < tempRes.size(); i++) {
EnterpriseVehicleTreeVO enterpriseVehicleTreeVO = tempRes.get(i);
if(i == ConstantUtil.INTNUM0) {
//第一家公司
tempEntId = enterpriseVehicleTreeVO.getEntId();
tempListStr.add( new VehVehicleVO(enterpriseVehicleTreeVO.getVehId(),enterpriseVehicleTreeVO.getVehLicplate()));
tempMap.put(enterpriseVehicleTreeVO.getEntName(), tempListStr);
if((i+1) == tempRes.size()) {
resultList.add(tempMap);
break;
}
}else {
//还是同一家公司
if(tempEntId == enterpriseVehicleTreeVO.getEntId()) {
tempListStr.add( new VehVehicleVO(enterpriseVehicleTreeVO.getVehId(),enterpriseVehicleTreeVO.getVehLicplate()));
tempMap.put(enterpriseVehicleTreeVO.getEntName(), tempListStr);
if((i+1) == tempRes.size()) {
resultList.add(tempMap);
}
}else {
//新的公司,先处理上一个公司的数据
resultList.add(tempMap);
tempMap = null; //注意,这个地方不可以直接调用clear方法去清空,必须重新创建tempMap对象,否则后面的新数据就把之前已经存到list中的上个公司的数据给覆盖掉了
tempMap = new HashMap<String, List<VehVehicleVO>>();
tempListStr = null; //原因同上
tempListStr = new ArrayList<VehVehicleVO>();
tempEntId = enterpriseVehicleTreeVO.getEntId();
tempListStr.add( new VehVehicleVO(enterpriseVehicleTreeVO.getVehId(),enterpriseVehicleTreeVO.getVehLicplate()));
tempMap.put(enterpriseVehicleTreeVO.getEntName(), tempListStr);
if((i+1) == tempRes.size()) {
resultList.add(tempMap);
}
}
}
}
response.setObj(resultList);
} catch (Exception e) {
log.info("xxxxxxxxxxxxxxxxxxxxxx异常,==>e.getMessage:" + e.getMessage() + ",==>e.getStackTrace():" + e.getStackTrace()+ ",==>e:" + e);
response.setSuccess(false).setMsg("xxxxxxxxxxxxxxxxxxxxxxxxx异常");
}
return response;
}
最终效果:
{
"success": true,
"msg": "",
"obj": [
{
"杭州科大讯飞": [
{
"id": 13,
"licPlate": "豫QA3586"
},
{
"id": 14,
"licPlate": "豫QA3585"
},
{
"id": 12,
"licPlate": "豫QA3587"
}
]
},
{
"杭州网易": [
{
"id": 8,
"licPlate": "浙A36W52"
}
]
}
]
}
sql查询结果多对多转为一对多返回前端的更多相关文章
- Servlet 中利用阿里云包fastjson-1.2.43.jar把map转为Json并返回前端
1.引入fastjson-1.2.43.jar 包到lib下面,下载地址链接: https://pan.baidu.com/s/1EgAOikoG4VJRJrnUw83SNA 密码: n2fr im ...
- Hibernate原生SQL查询多表关联,SQL语句要注意的问题
Hibernate原生SQL查询多表关联,SQL语句要注意的问题 @for&ever 2009-9-4 系统环境: MySQL5.1 Hibernate3.3 有如下的假定: 实体类 Ques ...
- Hibernate关联映射(单项多对一和一对多、双向一对多)
最近总是接触着新的知识点来扩展自己的知识面:不停的让自己在原地接触天空的感觉真的很美好!!!革命没有成功,程序员的我们怎么能不努力呢...... 一.用员工和部门来剖析关联映射的原理. 1)从这张截图 ...
- 一步步学习NHibernate(5)——多对一,一对多,懒加载(2)
请注明转载地址:http://www.cnblogs.com/arhat 通过上一章的学习,我们建立了Student和Clazz之间的关联属性,并从Student(many)的一方查看了Clazz的信 ...
- SpringData JPA进阶查询—JPQL/原生SQL查询、分页处理、部分字段映射查询
上一篇介绍了入门基础篇SpringDataJPA访问数据库.本篇介绍SpringDataJPA进一步的定制化查询,使用JPQL或者SQL进行查询.部分字段映射.分页等.本文尽量以简单的建模与代码进行展 ...
- mybatis:开发环境搭建--增删改查--多表联合查询(多对一)
什么是mybatisMyBatis是支持普通SQL查询,存储过程和高级映射的优秀持久层框架.MyBatis消除了几乎所有的JDBC代码和参数的手工设置以及结果集的检索.MyBatis使用简单的XML或 ...
- MyBatis 高级查询之多对多查询(十一)
高级查询之多对多查询 查询条件:根据玩家名,查询游戏信息 我们在之前创建的映射器接口 GameMapper.java 中添加接口方法,如下: /** * 根据玩家名查询游戏 * @param name ...
- MyBatis学习05(多对一和一对多)
8.多对一的处理 多对一的理解: 多个学生对应一个老师 如果对于学生这边,就是一个多对一的现象,即从学生这边关联一个老师! 数据库设计 CREATE TABLE `teacher` ( `id` IN ...
- Mybatis 多对一和一对多 学习总结04
1.Mybatis 组件的声明周期 声明周期是组件的重要问题,Mybatis也常用语多线程环境,错误使用会造成多线程并发问题,为正确编写Mybatis应用程序,我们要掌握Mybatis组件的声明周 ...
随机推荐
- oracle闪回存储过程
源地址:https://www.baidu.com/link?url=qgVCi_BLGOYqxJN0Fqqt-9N0SmCwtGI70SIh-TFpx1nP6oaVoMj8H6yjEqilto6TM ...
- oracle取order by的第一条数据
SELECT * FROM (SELECT * FROM TABLE_NAME ORDER BY COL1) WHERE ROWNUM = 1; 备注: 1.不能给 (SELECT * FROM TA ...
- 【FAQ】Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServlet
原因: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spr ...
- Spring AOP(面向切面编程)
一.AOP简介 1.AOP概念:Aspect Oriented Programming 面向切面编程 2.作用:本质上来说是一种简化代码的方式 继承机制 封装方法 动态代理 …… 3.情景举例 ①数学 ...
- [jvm] JVM体系结构
一.jvm 运行时数据区域 程序计数器 java虚拟机栈 本地方法栈 java堆 方法区 运行时常量池 直接内存(非JVM区域) 二.深入探讨HotSpot虚拟机在Java堆中对象分配.布局和访问的全 ...
- Java中的Date Time 与SQL Server 2005里的Datetime 之间的交互
Preface Environment:Platform: Windows XPLanguage: Java 1.5IDE: MyEclipse 6.0.1Database: SQL Server 2 ...
- CoreML的学习
在苹果官网下载模型model 链接: https://developer.apple.com/machine-learning/
- explian执行计划
MySQL为我们提供了 explain 关键字来直观的查看一条SQL的执行计划. explain显示了MySQL如何使用索引来处理select语句以及连接表,可以帮助选择更好的索引和写出更优化的查询语 ...
- ionic4 refresh组件位置变更:Ignored attempt to cancel a touchmove event with cancelable=false
io3 中 refresh组件位置可不定: io4 中 须置顶,否则报错,此外complete方法的调用位置改为target属性 参考:https://github.com/ionic-team/io ...
- Bootstrap点击弹出注册登录
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...