目录
1、此类情况使用场景
如果直接通过SQL查询,返回给所需树状结构数据,同时按照树的层级进行了排序 如下图所示:
2、实现代码
public static JSONArray treeRecursionDataList(List<Map<String,Object>> treeList, int parentId) {
JSONArray childMenu = new JSONArray();
for (Object object : treeList) {
JSONObject jsonMenu =JSONObject.parseObject(JSON.toJSONString(object));
int menuId = jsonMenu.getIntValue("id");
int pid = jsonMenu.getIntValue("parentId");
if (parentId == pid) {
JSONArray c_node = treeRecursionDataList(treeList, menuId);
jsonMenu.put("childList", c_node);
childMenu.add(jsonMenu);
}
}
return childMenu;
}
//调用函数
/**
* 获得用户岗位包含的所有分类
* @param request
* @param
* @return
*/
@RequestMapping(value = "/getUserJobRecursionCategoryList")
@ResponseBody
public Object getUserJobRecursionCategoryList(HttpServletRequest request,String status){
PermissionUser user = getCurrentUser(request);
Long ownerCompany = user.getOwnerCompany();
Long userJobId=user.getJobId();
Map<String,Object> paramMap = new HashMap<String,Object>();
paramMap.put("status", status);
paramMap.put("ownerCompany", ownerCompany);
paramMap.put("userJobId", userJobId);
List<Map<String, Object>> userJobCategoryList =null;
if(userJobId!=null){
userJobCategoryList =courseCategoryExtendMapper.getUserJobContainAllCategoryList(paramMap);
}
JSONArray returnList=treeRecursionDataList(userJobCategoryList, 0);
return ResultWrapper.create().setRows(returnList).getResultMap();
}
3、数据结果展示:
{
"rows": [
{
"ownerCompany": 10002,
"modifyTime": 1537325781000,
"createTime": 1536891501000,
"modifyId": 2592,
"categoryLevel": "1",
"creatorId": 1071,
"categoryState": "1",
"childList": [
{
"ownerCompany": 10002,
"modifyTime": 1536891514000,
"createTime": 1536891514000,
"modifyId": 1071,
"categoryLevel": "2",
"creatorId": 1071,
"categoryState": "1",
"childList": [
{
"ownerCompany": 10002,
"modifyTime": 1537240127000,
"createTime": 1536891591000,
"modifyId": 1172,
"categoryLevel": "3",
"creatorId": 1071,
"categoryState": "1",
"childList": [ ],
"id": 152,
"categoryName": "职业素养",
"parentId": 148,
"orgId": 133
}
],
"id": 148,
"categoryName": "专业理论知识",
"parentId": 147,
"orgId": 1
}
],
"id": 147,
"categoryName": "岗位资格性培训1",
"parentId": 0,
"orgId": 156
},
{
"ownerCompany": 10002,
"modifyTime": 1535097185000,
"createTime": 1534737840000,
"modifyId": 1071,
"categoryLevel": "1",
"creatorId": 1071,
"categoryState": "1",
"childList": [
{
"ownerCompany": 10002,
"modifyTime": 1536721891000,
"createTime": 1534738691000,
"modifyId": 2585,
"categoryLevel": "2",
"creatorId": 1071,
"categoryState": "1",
"childList": [
{
"ownerCompany": 10002,
"modifyTime": 1537240535000,
"createTime": 1537240489000,
"modifyId": 1172,
"categoryLevel": "3",
"creatorId": 1172,
"categoryState": "1",
"childList": [ ],
"id": 160,
"categoryName": "子分类1",
"parentId": 100,
"orgId": 133
}
],
"id": 100,
"categoryName": "工班长培训",
"parentId": 82,
"orgId": 1
},
{
"ownerCompany": 10002,
"modifyTime": 1535104574000,
"createTime": 1535104547000,
"modifyId": 1071,
"categoryLevel": "2",
"creatorId": 1071,
"categoryState": "1",
"childList": [
{
"ownerCompany": 10002,
"modifyTime": 1537240549000,
"createTime": 1537240549000,
"modifyId": 1172,
"categoryLevel": "3",
"creatorId": 1172,
"categoryState": "1",
"childList": [ ],
"id": 161,
"categoryName": "子分类2",
"parentId": 118,
"orgId": 133
}
],
"id": 118,
"categoryName": "特种设备作业培训",
"parentId": 82,
"orgId": 1
}
],
"id": 82,
"categoryName": "岗位资格性培训",
"parentId": 0,
"orgId": 1
}
],
"msgInfo": "",
"msgCode": "0",
"isSuccess": "true"
}
记录一下可参考文章:https://www.jb51.net/article/125076.htm