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

来源:互联网 发布:java 重定向 redirect 编辑:程序博客网 时间:2024/05/21 11:14

第一步:创建树形数据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;    }}
  • 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>    <!-- 根据前端页面传入的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>
  • 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=#{deptId}    </select>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

第三步:service业务层(递归)

    /**     * 查询部门组织架构树(含部门下的人员)     * @return     */    public List<DeptTree> queryDeptUserTreeList(Map<String, Object> paramsMap){        List<DeptTree> dList=dao.queryDeptTreeList(paramsMap);        for(DeptTree dt:dList){            getTreeNodeData(dt);        }        return dList;    }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
/**     * 为每个部门插入相关人员     * @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:[                            //子部门                            //‘部门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            }        ]     }]
  • 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
阅读全文
0 0
原创粉丝点击