第一步:创建树形数据Bean
public class DeptTree { private String id; private String name; private List<DeptTree> childrenList; private String type="0"; private String deptId; private String deptName; public String getDeptName() { return deptName; } public void setDeptName(String deptName) { this.deptName = deptName; } public String getDeptId() { return deptId; } public void setDeptId(String deptId) { this.deptId = deptId; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public List<DeptTree> getChildrenList() { return childrenList; } public void setChildrenList(List<DeptTree> childrenList) { this.childrenList = childrenList; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
第二步:mybatis相应的sql.xml文件配置
<resultMap type="com.lilosoft.cospace.sys.bean.DeptTree" id="deptTree"> <result column="DEPT_ID" property="id" javaType="java.lang.String" /> <result column="DEPT_NAME" property="name" javaType="java.lang.String" /> <result column="deptId" property="deptId" javaType="java.lang.String" /> <result column="deptName" property="deptName" javaType="java.lang.String" /> <collection column="DEPT_ID" property="childrenList" ofType="DeptTree" javaType="java.util.ArrayList" select="selectDeptChildrenById"/> </resultMap> <select id="queryDeptTreeList" resultMap="deptTree" > select dept_id,dept_name,dept_id as "deptId",dept_name as "deptName" from sys_dept where is_use='0' <if test="deptId != null and deptId != ''"> AND dept_id=#{deptId} </if> <if test="deptId == null or deptId == ''"> AND dept_id='1' </if> </select> <select id="selectDeptChildrenById" resultMap="deptTree" parameterType="string"> select dept_id,dept_name,dept_id as "deptId",dept_name as "deptName" from sys_dept where is_use='0' and parent_id= #{DEPT_ID} </select>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
<resultMap type="com.lilosoft.cospace.sys.bean.DeptTree" id="deptTreeV"> <result column="userId" property="id" javaType="java.lang.String" /> <result column="userName" property="name" javaType="java.lang.String" /> <result column="deptId" property="deptId" javaType="java.lang.String" /> <result column="deptName" property="deptName" javaType="java.lang.String" /> </resultMap> <! <select id="selectUserByDeptId" resultMap="deptTreeV" parameterType="string"> SELECT b.user_id as "userId",c.user_name as "userName",a.dept_id as "deptId",a.dept_name as "deptName" FROM sys_dept a left join sys_deptmember b on a.dept_id =b.dept_id left join sys_user c on b.user_id =c.user_id where a.is_use='0' AND a.dept_id= </select>
第三步:service业务层(递归)
/** * 查询部门组织架构树(含部门下的人员) * @return */ public List<DeptTree> queryDeptUserTreeList(Map<String, Object> paramsMap){ List<DeptTree> dList=dao.queryDeptTreeList(paramsMap); for(DeptTree dt:dList){ getTreeNodeData(dt); } return dList; }
/** * 为每个部门插入相关人员 * @param dList * @return */ public void getTreeNodeData(DeptTree dt){ List<DeptTree> userList=dao.selectUserByDeptId(dt.getId()); if(userList!=null && userList.size()>0){ for(DeptTree u:userList){ u.setType("1"); } } List<DeptTree> t=dt.getChildrenList(); if(t!=null && t.size()>0){ t.addAll(userList); for(DeptTree t1:t){ getTreeNodeData(t1); } }else{ dt.setChildrenList(userList); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
第四步:控制层
/** * 查询部门组织架构树(查询部门和部门人员) * @param deptId * @return */ @RequestMapping("/queryDeptUserTreeList") @ResponseBody @LogMethodRemark(moduleName="组织机构",remark="查询部门组织架构树(含部门和部门下的人员信息,使用此方法)") public JsonResult queryDeptUserTreeList(@RequestParam HashMap<String, Object> paramsMap) { try { List<DeptTree> dList=deptService.queryDeptUserTreeList(paramsMap); return renderSuccess(dList); } catch (Exception e) { logger.error("查询部门树数据失败:", e); return renderError("查询部门树数据失败"); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
第五步:树形数据展示(每个部门的子部门和该部门下的人员信息都放在childrenList中,实现单节点)
ps:部门和人员信息都以同一个bean来存储,以type字段来区分;type=0,表示是部门,type=1表示是人员。
this.treeData = [ { id: 0, name: '总部门', type: 'dept', childrenList: [ { id: 1, name: '部门1', type: '0', childrenList:[ { id: 1, name: '部门1-1', type: '0', childrenList:[ ] }, { id: 1, name: '部门1-2', type: '0', childrenList:[ ] }, { id: 1, name: '部门1-3', type: '0', childrenList:[ ] }, { id:r1, name:'jay' type:1 }, { id:r2, name:'xxm' type:1 }, { id:r3, name:'alen' type:1 } ], }, { id:r3, name:'alen' type:1 }, { id:r3, name:'jak' type:1 }, { id:r3, name:'mac' type:1 } ] }]
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73