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组件的声明周 ...
随机推荐
- activeMq延迟消息队列
Long delay = 30 * 1000L; jmsTemplate.send(type.getValue(),new MessageCreator() { @Override public Me ...
- 用HTML,css完成的百叶窗效果,新手必看
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title> ...
- 【bzoj3309】DZY Loves Math 莫比乌斯反演+线性筛
Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b, ...
- Python之路Python内置函数、zip()、max()、min()
Python之路Python内置函数.zip().max().min() 一.python内置函数 abs() 求绝对值 例子 print(abs(-2)) all() 把序列中每一个元素做布尔运算, ...
- js 对象 浅拷贝 和 深拷贝
网上发现一个比较好的博客 阮一峰的感觉很不错推荐大家看看. http://www.ruanyifeng.com/blog/it/javascript/ 接下来看一下这两个拷贝方法 1.浅拷贝 拷贝就是 ...
- SQL里的real类型和tinyint类型在C#里分别对应类型
- qt线程睡眠
头文件 #include <QThread> 接口函数: void QThread::sleep ( unsigned long secs ) [static protected] v ...
- 用 GitLab CI 进行持续集成
简介 从 GitLab 8.0 开始,GitLab CI 就已经集成在 GitLab 中,我们只要在项目中添加一个 .gitlab-ci.yml 文件,然后添加一个 Runner,即可进行持续集成. ...
- fluent中UDF环境变量问题的三种解决方法
方法一: 这种方式最简便,首选这种,但是有时会因为不明原因而不好使,我自己电脑刚开始用这种方式是行得通的,但是后来中途装过很多乱七八糟的软件,估计环境变量改乱了,这时候只能用第二种或者第三种方法.先说 ...
- 洛谷 P3244 / loj 2115 [HNOI2015] 落忆枫音 题解【拓扑排序】【组合】【逆元】
组合计数的一道好题.什么非主流题目 题目背景 (背景冗长请到题目页面查看) 题目描述 不妨假设枫叶上有 \(n\) 个穴位,穴位的编号为 \(1\sim n\).有若干条有向的脉络连接着这些穴位. ...