树结构的json组装
在项目中有个非常常见的需求,就是希望获得可配置的前端页面权限,一般这种需求是通过账号----角色-----页面权限这种结构的树结构完成,也可能是其他变种,但是大差不离都是多级树。
面对这种多级树组装的需求,往往第一个想到的是,递归组装。大致做法是,获得顶节点,然后根据顶节点的id去获取父节点是顶节点id的子节点,把子节点的数据递归到父节点的子数据中去,然后递归,直到节点下无子节点为止。
这种做法肯定是可行的,但是递归的时间复杂度高,而且在获取子节点数据时,不管获取子节点数据是直接通过sql在数据库中直接查询,全部查出来放在内存中,IO的开销也很大,当树不复杂和节点数量不多时还能维持速度,当总节点的数量超过一千,接口必然无法在1000ms内做出响应。
最后我的解决方案是在节点资源表中增加节点的层级字段,在查询的时候将所有节点按照节点层级反向排序,这样最小层级的数据就会排在查出的list的最前方,然后把这些数据转换成hashmap,按顺序依次把节点数据挂到父节点下边去。这样就避免了递归,树结构的组装只需要一遍循环。
下面是测试数据
create table treeSource
(
id int auto_increment comment '主键id'
primary key,
name varchar(50)null comment '资源点名称',
isShow int(1)default 1 null comment '1为显示名称,0为不显示名称',
route varchar(255)null comment '路径 如二级节点应当是一级节点的id.二级节点的id(1.2)',
no int default 0 null comment '排序',
pictureUrl varchar(255)null comment '前端图片地址名称',
fId int null comment '父id',
gradeType int null comment '级别(如1是顶级页面节点,2是下属的大区块节点)',
notes varchar(255)null comment '注释',
constraint routeId
unique (route)comment '确保路径唯一'
)
charset = utf8mb4;
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (1,'管理资产规模',0,'1',1,null,0,1,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (2,'项目基本信息',0,'1.2',2,null,1,2,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (3,'项目个数',1,'1.2.3',2,'project_icon.jpg',2,3,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (4,'覆盖量',0,'1.2.4',2,'fgl_icon.jpg',2,3,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (5,'总面积',0,'1.2.5',2,'zmj_icon.jpg',2,3,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (6,'资产规模',0,'1.2.6',2,'zcgm_icon.jpg',2,3,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (7,'各类资产比例(按建筑面积)',0,'1.7',2,null,1,2,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (8,'写字楼',0,'1.7.8',2,null,7,3,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (9,'购物中心',0,'1.7.9',2,null,7,3,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (10,'商业街',0,'1.7.10',2,null,7,3,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (11,'其他',0,'1.7.11',2,null,7,3,null);
INSERT INTO cms_data.treeSource (id,name,isShow,route,no,pictureUrl,fId,gradeType,notes)VALUES (12,'项目覆盖地图',0,'1.12',2,null,1,2,null);
对于的sql与po(getter setter未写)
<select id="selectTreeSources" resultType="com.example.tree.po.TreeSource">
SELECT*
FROM treeSource order bygradeType desc,no
</select>
public class TreeSource {
private Integerid;
private Stringname;
private IntegerisShow;
private Stringroute;
private Integerno;
private StringpictureUrl;
private IntegerfId;
private IntegergradeType;
private Stringnotes;
}
对应的vo
public class TreeSourceVo {
private Integerid;
private Stringname;
private IntegerisShow;
private Stringroute;
private Integerno;
private StringpictureUrl;
private IntegerfId;
private IntegergradeType;
private Stringnotes;
private ListsubTreeSourceVos;
public TreeSourceVo (TreeSource treeSource){
this.id = treeSource.getId();
this.name = treeSource.getName();
this.isShow = treeSource.getIsShow();
this.route = treeSource.getRoute();
this.no = treeSource.getNo();
this.pictureUrl = treeSource.getPictureUrl();
this.fId = treeSource.getFId();
this.gradeType = treeSource.getGradeType();
this.notes = treeSource.getNotes();
}
}
对于的组装语句
@Service
public class TreeSourceService {
@Resource
private TreeSourceMappertreeSourceMapper;
public List getTreeSourceVo(){
List treeSources =treeSourceMapper.selectTreeSources();
Map map =new LinkedHashMap<>();
for(TreeSource treeSource:treeSources){
TreeSourceVo treeSourceVo =new TreeSourceVo(treeSource);
map.put(treeSourceVo.getId(), treeSourceVo);
}
return this.toParentMenuVoList(map);
}
public List toParentMenuVoList(Map map ){
Iterator iter = map.keySet().iterator();
while (iter.hasNext()) {
int key = iter.next();
if(map.get(map.get(key).getFId()) ==null) {
continue;
}
if(map.get(map.get(key).getFId()).getSubTreeSourceVos() ==null) {
map.get(map.get(key).getFId()).setSubTreeSourceVos(new ArrayList<>());
}
map.get(map.get(key).getFId()).getSubTreeSourceVos().add(map.get(key));
iter.remove();
}
return new ArrayList<>(map.values());
}
}
最后组装的结果,完全符合树结构,而且很快
image.png