Mybatis之高级映射collection (递归查出树形数据之查询部门及部门下所有人员 单节点)
2017-04-11 14:57
549 查看
第一步:创建树形数据Bean
第二步:mybatis相应的sql.xml文件配置
第三步:service业务层(递归)
第四步:控制层
第五步:树形数据展示(每个部门的子部门和该部门下的人员信息都放在childrenList中,实现单节点)
ps:部门和人员信息都以同一个bean来存储,以type字段来区分;type=0,表示是部门,type=1表示是人员。
public class DeptTree { private String id; private String name; private List<DeptTree> childrenList;//当前部门下的子部门集合 private String type="0";//type=0 表示是部门;type=1 表示是人员 private String deptId;//部门id 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; } }
第二步: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> <!-- 根据前端页面传入的dept_id,查出所有部门 --> <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>
<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=#{deptId} </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); } }
第四步:控制层
/** * 查询部门组织架构树(查询部门和部门人员) * @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("查询部门树数据失败"); } }
第五步:树形数据展示(每个部门的子部门和该部门下的人员信息都放在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:[ //子部门 //‘部门1-1’ 的部门人员 ] }, { id: 1, name: '部门1-2', type: '0', childrenList:[ //子部门 //‘部门1-2’ 的部门人员 ] }, { id: 1, name: '部门1-3', type: '0', childrenList:[ //子部门 //‘部门1-3’ 的部门人员 ] }, { 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 } ] } ]
相关文章推荐
- Mybatis之高级映射collection (递归查出树形数据之查询部门及部门下所有人员 单节点)
- Mybatis之高级映射collection (递归查出树形数据之查询部门及部门下所有人员 二叉树)
- Mybatis之高级映射collection (递归查出树形数据 )
- Mybatis之高级映射collection (递归查出树形数据 )
- 7. Orcle树形结构(类似数据字典有父子类关系),查询末节点的所有记录
- 用递归的方法查询树形结构下的所有子节点
- ORACLE 树形结构数据 查询某结点下全部子节点无限递归的前2个数据
- mybatis collection高级映射 一对多管理查询只能查询出一条
- 【MyBatis框架】高级映射-多对多查询
- Mybatis 高级结果映射 ResultMap Association Collection
- 【MyBatis框架】高级映射-一对多查询
- SqlServer找出所有的子节点,并按树形结构、层次查询
- MySql的sql语句中添加存储过程或者存储函数来实现Oracle中的start with ……connect by prior……递归(树形结构数据)查询
- T-SQL查询:WITH AS 递归计算某部门的所有上级机构或下级机构
- Mybatis 高级结果映射 ResultMap Association Collection
- Mybatis 高级结果映射 ResultMap Association Collection
- 【MyBatis框架】高级映射-一对一查询
- oracle数据库查询:查询父节点数据下的所有终结子节点
- 【MyBatis框架】高级映射-多对多查询
- 递归改进(在左侧树形不仅可以显示目录还可以显示跟要查询子目录统计的所有文件)