Mybatis之高级映射collection (递归查出树形数据之查询部门及部门下所有人员 单节点)

第一步:创建树形数据Bean

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.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.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
            }
        ] 
    }
]
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值