数据库层级关系转换为树结构的json
原文链接:https://blog.csdn.net/Tonysdyp/article/details/80987959
首先是数据库设计:
通过查询数据库,将全局数据作为一个ArrayList<HashMap<String,Object>>获取
每单个节点都是一个HashMap,如
”cid”:2001,”cname”:华为,”pid”:1001
如下图
数据准备完成,接下来开始进行树结构转换
因为每一个节点现在都是以HashMap的格式来存储的,所以可以在HashMap里面添加一个key,用来存储子节点的信息,也就是baby,然后将拥有本节点cid为父节点pid的节点信息存入此节点的key为baby的ArrayList<HashMap<String,Object>>里面,同时加入递归,一层一层查下去,由此可以生成一个树结构的数据,然后使用json转换工具,就可以直接返回给浏览器
public List TreeGood(ArrayList<HashMap<String, Object>> notes, String cid) throws SQLException {
ArrayList<HashMap<String, Object>> treeGoods = new ArrayList<HashMap<String, Object>>();
for (HashMap<String, Object> good : notes) {
System.out.println(cid);
String pid = good.get("pid") == null ? "" : good.get("pid") + "";
if (cid.equals(pid)) {
HashMap<String, Object> map = new HashMap<String, Object>();
String ccid = (String) good.get("cid");
map = good;
map.put("baby", TreeGood(notes, ccid));
treeGoods.add(map);
}
}
return treeGoods;
}
扩展
默认查出所有分类字段,设置的根节点的父类id 为0
//根据classfiyId,查询分类列表
public List selectFindList(String itemId,String classfiyId){
//根据项目id,查询所有分类
ArrayList<HashMap<String, Object>> list= classfiyMapper.selectList(itemId);
List list1=new ArrayList();
try {
list1= TreeGood(list,classfiyId);
System.out.println(list1);
} catch (SQLException e) {
e.printStackTrace();
}
return list1 ;
}
/*递归储存为树状图结构*/
public List TreeGood(ArrayList<HashMap<String, Object>> notes, String cid) throws SQLException {
ArrayList<HashMap<String, Object>> treeGoods = new ArrayList<>(); for (HashMap<String, Object> good : notes) { String pid = good.get("parentId") .equals("0") ? "0" : good.get("parentId") + "";
//cid 为规定父类id,可查询规定的分支为根目录
if (cid.equals(pid)) {
HashMap<String, Object> map;
String ccid = (String) good.get("classfiyId");
map = good;
map.put("children", TreeGood(notes, ccid));
treeGoods.add(map);
}
}
return treeGoods;
}
结果:
[{
"showState": 1,
"children": [{
"showState": 0,
"children": [{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 4,
"classfiyId": "3b9254f8-ac0d-43e9-a205-18350d4a0f9b",
"title": "铸造生铁",
"parentId": "57f5289a-8dc7-4aa3-96be-b8776291603c"
},
{
"showState": 0,
"children": [],
"classfiyType": 4,
"classfiyId": "bd5adfcb-22e7-4c20-92c9-0f2b202d21b2",
"title": "钛合金",
"parentId": "57f5289a-8dc7-4aa3-96be-b8776291603c"
}
],
"classfiyType": 3,
"classfiyId": "57f5289a-8dc7-4aa3-96be-b8776291603c",
"title": "生铁",
"parentId": "cae3ee7b-fac5-48f2-bc4d-690dad581d51"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "c74eb1db-536d-4bad-a79d-3fc79fdc5e26",
"title": "方钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "459a9b33-573d-4ab4-821f-70d8aff58015",
"title": "扁钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "6e7d2aa6-9449-484c-939d-d1dc433646b2",
"title": "槽钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "5cf501ac-fba2-4a00-b3da-2f575916a331",
"title": "角钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "85ceea86-5382-4c1c-8eb0-d5da8d63a013",
"title": "工字钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "41eb40db-7d4d-40e8-a7aa-77c2dce820fd",
"title": "六角钢",
"parentId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd"
}
],
"classfiyType": 4,
"classfiyId": "ba595ced-cc2d-42b6-b004-78e9a4ada2fd",
"title": "型钢",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "44d3808d-0043-45d2-8da0-ed5d63041736",
"title": "无缝管",
"parentId": "ff70bc9a-f2c5-4287-8ff4-68d85eb3bd1c"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "a0b9cbab-b09f-445a-bf2b-caeb3541c19e",
"title": "焊管",
"parentId": "ff70bc9a-f2c5-4287-8ff4-68d85eb3bd1c"
}
],
"classfiyType": 4,
"classfiyId": "ff70bc9a-f2c5-4287-8ff4-68d85eb3bd1c",
"title": "管材",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "dd608087-8176-466b-8a9c-b5976d064b1d",
"title": "薄钢板",
"parentId": "c8ba64ee-bf9a-44a6-9358-f674952e25a3"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "87359155-131c-4f53-a9c4-a1fa2df36c08",
"title": "中厚钢板",
"parentId": "c8ba64ee-bf9a-44a6-9358-f674952e25a3"
}
],
"classfiyType": 4,
"classfiyId": "c8ba64ee-bf9a-44a6-9358-f674952e25a3",
"title": "钢板",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "ea2ae294-0c55-4bc5-93c8-34667c40def9",
"title": "钢丝绳",
"parentId": "206b9822-bf2e-4684-bec1-7fe5d5eab45b"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "20571539-8aa1-4f9d-8b54-021320c80062",
"title": "钢绞线",
"parentId": "206b9822-bf2e-4684-bec1-7fe5d5eab45b"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "362999ed-564a-4245-8ce8-ce25a8a45124",
"title": "镀锌钢绞线",
"parentId": "206b9822-bf2e-4684-bec1-7fe5d5eab45b"
}
],
"classfiyType": 4,
"classfiyId": "206b9822-bf2e-4684-bec1-7fe5d5eab45b",
"title": "钢丝绳,绞线",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "f0439782-86bd-4529-8932-db31f017f99c",
"title": "圆钢",
"parentId": "f60dbd72-5537-4371-aed0-52b742785c9e"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "da855976-03eb-4b24-b3f9-6ee2c09fa52a",
"title": "螺纹钢",
"parentId": "f60dbd72-5537-4371-aed0-52b742785c9e"
},
{
"showState": 0,
"children": [],
"classfiyType": 5,
"classfiyId": "93d55bd6-dd41-4230-b4d5-cb5dc0f8684f",
"title": "线材",
"parentId": "f60dbd72-5537-4371-aed0-52b742785c9e"
}
],
"classfiyType": 4,
"classfiyId": "f60dbd72-5537-4371-aed0-52b742785c9e",
"title": "钢筋",
"parentId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3"
}
],
"classfiyType": 3,
"classfiyId": "aaf6fc46-8d21-4d4f-9394-0e9b4a4a8df3",
"title": "钢材",
"parentId": "cae3ee7b-fac5-48f2-bc4d-690dad581d51"
}
],
"classfiyType": 2,
"classfiyId": "cae3ee7b-fac5-48f2-bc4d-690dad581d51",
"title": "黑色金属",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
},
{
"showState": 0,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 3,
"classfiyId": "7a67f4a1-c5b1-44ce-84ed-2c2d990bbc93",
"title": "铝及铝合金",
"parentId": "eff8cc03-8475-4f0b-8a55-b05c027b6794"
},
{
"showState": 0,
"children": [],
"classfiyType": 3,
"classfiyId": "4b0f83ed-0064-42ad-81a2-95c76793b37f",
"title": "铜及铜合金",
"parentId": "eff8cc03-8475-4f0b-8a55-b05c027b6794"
}
],
"classfiyType": 2,
"classfiyId": "eff8cc03-8475-4f0b-8a55-b05c027b6794",
"title": "有色金属",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "40de0d21-39f5-458c-a0fc-5b3afb244db3",
"title": "祖母绿",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "fb8a6f2a-e30c-4dc2-8772-86d259578ed8",
"title": "未命名",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
},
{
"showState": 0,
"children": [],
"classfiyType": 3,
"classfiyId": "57ca6782-b070-47a8-9917-86ed5c8f52ae",
"title": "未命名",
"parentId": "94011477-40fe-4870-ad09-dce762153319"
}
],
"classfiyType": 1,
"classfiyId": "94011477-40fe-4870-ad09-dce762153319",
"title": "金属",
"parentId": "0"
},
{
"showState": 1,
"children": [{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "a0b6867d-2018-4add-89dd-9745d600acfd",
"title": "普通水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "0a5b35e9-f1fa-47eb-bc33-d803b450986c",
"title": "快硬高强水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "c7cba38d-58c0-49e4-b136-6424a0be5dd6",
"title": "耐侵蚀水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "2b160f60-d3f0-4963-a5a5-069a6565d180",
"title": "膨胀水泥",
"parentId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b"
}
],
"classfiyType": 1,
"classfiyId": "cf788cc1-faa5-4a88-9c02-770424cf5b7b",
"title": "水泥",
"parentId": "0"
},
{
"showState": 1,
"children": [],
"classfiyType": 1,
"classfiyId": "f6cb44b3-b282-4730-b0e2-e3c313bbe52e",
"title": "木材",
"parentId": "0"
},
{
"showState": 0,
"children": [],
"classfiyType": 1,
"classfiyId": "a12e7bf2-b9ed-475c-8f5b-f14def130d46",
"title": "测试",
"parentId": "0"
},
{
"showState": 0,
"children": [],
"classfiyType": 2,
"classfiyId": "4b72ce4a-3923-4f77-abc3-6eed659af041",
"title": "未命名",
"parentId": "0"
}
]
数据库层级关系转换为树结构的json的更多相关文章
- Web中树形数据(层级关系数据)的实现—以行政区树为例
在Web开发中常常遇到树形数据的操作,如菜单.组织机构.行政区(省.市.县)等具有层级关系的数据. 以下以行政区为例说明树形数据(层级关系数据)的存储以及实现,效果如图所看到的. 1 数据库表结构设计 ...
- 树状结构Java模型、层级关系Java模型、上下级关系Java模型与html页面展示
树状结构Java模型.层级关系Java模型.上下级关系Java模型与html页面展示 一.业务原型:公司的组织结构.传销关系网 二.数据库模型 很简单,创建 id 与 pid 关系即可.(pid:pa ...
- 【吐血分享】SQL Server With As 递归获取层级关系数据
纯洁的一周又开始了,今天看到一则新闻,笑尿了,和袁友们一起娱乐下 最近两月在做基于Saas模式的人力资源管理产品,平常数据库设计我经常会遇到如下需求场景: 以前商城类网站在设计类型表的时候,设计成单表 ...
- php解析出带层级关系的mpp文件
本来要使用DHX gantt插件自带的API做导入,可是做完后,又发现不稳定,不能访问了 可能是屏蔽掉了 所以又想起可以使用javaBridge,借用java的MPXJ php解析mpp的 上一篇介绍 ...
- NorthWind 数据库整体关系
http://blog.csdn.net/bergn/article/details/1502150 今天看到一张非常有用的图,说明有关Northwind数据库整体关系的图,以前一直在用,但是没有一个 ...
- NGUI 层级关系控制
NGUI元素的遮挡情况是不依赖空间关系,所以在NGUI上添加特效有时候特别蛋疼,特别是美术同学还要依赖空间关系来控制特效效果,那先看看看NGUI的层级是怎么处理的,不过下面的描述都是针对单个相机下的P ...
- CTE计算层级关系
推广渠道表有ParentID字段,代表上下层级关系.现要统计每个推广员,推广了多少人? --创建表结构,插入测试数据 USE DBA_Monitor GO CREATE TABLE [dbo].[TG ...
- 使用SchemaSpy逆向工程生成数据库依赖关系使用SchemaSpy工具可以快速的从数据库中得到
使用SchemaSpy逆向工程生成数据库依赖关系 使用SchemaSpy工具可以快速的从数据库中得到表的依赖关系,同时生成一个生动的“表图”结合的报告.方便快速了解数据库中的数据库对象间关系,类 ...
- Unity NGUI和UGUI与模型、特效的层级关系
目录 1.介绍两大UI插件NGUI和UGUI 2.unity渲染顺序控制方式 3.NGUI的控制 4.UGUI的控制 5.模型深度的控制 6.粒子特效深度控制 7.NGUI与模型和粒子特效穿插层级管理 ...
随机推荐
- ref和out的使用及区别
1. ref的使用:使用ref进行参数的传递时,该参数在创建时,必须设置其初始值,且ref侧重于修改: 2. out的使用: 采用out参数传递时,该参数在创建时,可以不设置初始值,但是在方法中必须 ...
- 浅谈hash
hash 算法介绍 hash说得通俗一点,就是给一个变量编上一个马甲 比如说一个人聪明可爱,举世无双,天资聪慧.活泼机灵...,那么就是叫我了(真不要脸 但是这样是不是显得些许麻烦? 于是人类发明了名 ...
- 一.django初识
1.创建django项目:[vagrant@CentOS7 vagrant]$ django-admin startproject devops [vagrant@CentOS7 vagrant]$ ...
- nodejs 本地压缩jpg,png图片(nodejs)
使用nodejs实现本地压缩jpg,png图片. 使用到的包 1.images 用于压缩jpg npm install images yarn add images 2.imagemin 用于压缩 ...
- Golden Tiger Claw,题解
题目链接 题目: 题意: 找到和最小的两个序列a,b满足对于任意i,j有a[i]+b[j]>=c[i][j](矩阵c给出). 分析: 首先很容易看出来要使这题要用KM算法,为啥呢?因为要最小化a ...
- python分块读取大数据,避免内存不足
- JavaScript学习 Ⅵ (正则表达式)
十三. 正则表达式 正则表达式用于定义一些字符串的规则,计算机可以根据正则表达式,来检查一个字符串是否符合规则,将字符串中符合规则的内容提取出来. 创建正则表达式对象 构造函数 var reg = n ...
- JavaScript学习 Ⅳ
八. 批量创建对象 使用工厂方法创建对象 function creatPerson(name, age, gender='男'){ var obj = new Object(); obj.name = ...
- Ubuntu下编译安装postgreSQL 10.5
Ubuntu下编译安装postgreSQL 10.5 ubuntu 16.04 LTS系统postgreSQL 10.5 安装包准备 1.从PostgreSQL官网下载PostgreSQL的安装包 安 ...
- Mysql基础(四):库、表、记录的详细操作、单表查询
目录 数据库03 /库.表.记录的详细操作.单表查询 1. 库的详细操作 3. 表的详细操作 4. 行(记录)的详细操作 5. 单表查询 数据库03 /库.表.记录的详细操作.单表查询 1. 库的详细 ...