java后台实现三级查询的功能
java后台实现这种数据类型:
1 [ 2 { 3 "nextlevel1": [ 4 { 5 "nextlevel2": [ 6 { 7 "nextlevel3": [ 8 { 9 "dept_name": "深圳总公司研发部门下级部门", 10 "dept_id": 114 11 } 12 ], 13 "dept_name": "研发部门", 14 "dept_id": 103 15 }, 16 { 17 "dept_name": "运维部门", 18 "dept_id": 107 19 } 20 ], 21 "dept_name": "深圳总公司", 22 "dept_id": 101 23 }, 24 { 25 "nextlevel2": [ 26 { 27 "dept_name": "市场部门", 28 "dept_id": 108 29 }, 30 { 31 "dept_name": "财务部门", 32 "dept_id": 109 33 } 34 ], 35 "dept_name": "长沙分公司", 36 "dept_id": 102 37 }, 38 { 39 "nextlevel2": [ 40 { 41 "dept_name": "财务部门", 42 "dept_id": 127 43 }, 44 { 45 "dept_name": "财务部门11", 46 "dept_id": 131 47 } 48 ], 49 "dept_name": "日照分公司", 50 "dept_id": 123 51 }, 52 { 53 "dept_name": "弗格森市分公司的感受到跟不上", 54 "dept_id": 124 55 }, 56 { 57 "dept_name": "财务部门5", 58 "dept_id": 128 59 } 60 ], 61 "dept_name": "RuoYi科技", 62 "dept_id": 100 63 }, 64 { 65 "nextlevel1": [ 66 { 67 "nextlevel2": [ 68 { 69 "nextlevel3": [ 70 { 71 "nextlevel4": [ 72 { 73 "nextlevel5": [ 74 { 75 "dept_name": "6级", 76 "dept_id": 142 77 } 78 ], 79 "dept_name": "5级", 80 "dept_id": 141 81 } 82 ], 83 "dept_name": "4级", 84 "dept_id": 140 85 } 86 ], 87 "dept_name": "应急财务部门", 88 "dept_id": 112 89 }, 90 { 91 "dept_name": "应急市场部门", 92 "dept_id": 113 93 } 94 ], 95 "dept_name": "应急分局", 96 "dept_id": 111 97 } 98 ], 99 "dept_name": "应急局", 100 "dept_id": 110 101 } 102 ]
方法一:只实现三级查询
java代码如下:
View Code
startPage(); List<Map> levelone = wxGovUserService.selectWxGovDepList(); List<Map> list1 = new ArrayList<>(); List<Map> levelthree = new ArrayList<>(); List<Map> leveltwo = new ArrayList<>(); for (int i= 0;i<levelone.size();i++){ levelone.get(i).put("levelone_deptname",levelone.get(i).remove("dept_name")); levelone.get(i).put("levelone_deptid",levelone.get(i).remove("dept_id")); String DepId = levelone.get(i).get("levelone_deptid").toString(); leveltwo = wxGovUserService.selectWxGovDepofficeList(DepId); for(int j = 0; j < leveltwo.size(); j++){ leveltwo.get(j).put("leveltwo_deptname",leveltwo.get(j).remove("dept_name")); leveltwo.get(j).put("leveltwo_deptid",leveltwo.get(j).remove("dept_id")); String DepIdThree = leveltwo.get(j).get("leveltwo_deptid").toString(); levelthree = wxGovUserService.selectWxGovDepofficeList(DepIdThree); for(int m = 0; m < levelthree.size(); m++){ levelthree.get(m).put("levelthreedept_deptid",levelthree.get(m).remove("dept_id")); levelthree.get(m).put("levelthreedept_name",levelthree.get(m).remove("dept_name")); } leveltwo.get(j).put("levelthree_list",levelthree); } levelone.get(i).put("leveltwo_list", leveltwo); list1.add(levelone.get(i)); } return list1;
方法二:通过递归实现n级查询
//递归调用函数
private List<Map> getdeplistcircle(String DepId,int m) { List<Map> leveldown = new ArrayList<>(); int j = m; leveldown = wxGovUserService.selectWxGovDepofficeList(DepId); if(leveldown != null){ for (int i = 0; i < leveldown.size(); i++) { String DepIdDown = leveldown.get(i).get("dept_id").toString(); if(DepIdDown != null){ int x = ++j; List<Map> nextlevel = getdeplistcircle(DepIdDown,x); if(nextlevel.size() == 0){ continue; }else{ int n = 1 +j++; String nextleveljoin = "nextlevel" + m; leveldown.get(i).put(nextleveljoin,nextlevel); } }else{ continue; } } return leveldown; }else{ return null; } } //主函数 @PostMapping("/getgfgdgdlist") @ResponseBody @CrossOrigin public List getdeplist() { startPage(); List<Map> levelone = wxGovUserService.selectWxGovDepList(); List<Map> list1 = new ArrayList<>(); List<Map> levelthree = new ArrayList<>(); List<Map> leveltwo = new ArrayList<>(); int j = 2; for (int i= 0;i<levelone.size();i++){ String DepId = levelone.get(i).get("dept_id").toString(); if(DepId != null){ List<Map> leveldown = getdeplistcircle(DepId,j); if(leveldown.size() == 0){ continue; }else{ levelone.get(i).put("nextlevel1",leveldown); } } } return levelone; }
三级查询mysql语句一次查出结果
select
three.dept_id,three.dept_name,three.parent_id, three.ancestors
,jointable.dept_id,jointable.dept_name,jointable.parent_id,
until_dept_id,until_dept_name
from sys_dept three inner join
(select two.dept_id,two.dept_name,two.parent_id, two.ancestors,one.dept_id as until_dept_id,one.dept_name as until_dept_name
from sys_dept two inner join
(select dept_id ,dept_name from sys_dept where parent_id = 0) one ON two.parent_id = one.dept_id)
jointable on jointable.dept_id = three. parent_id
三级查询mysql语句一次查出结果
View Code