1、部门的数据表中必须有“部门编号”和“父级部门编号”这两个字段,其余字段根据自己需要添加,如下图所示。parent_id就是父级部门编号。
2、编写Department实体类,如下代码
package com.entity; import org.joda.time.DateTime; import java.util.List; public class Department { private String id; private String name; private String parent_id; private Integer sequence; private Integer level; private Boolean enabled; private String operate_time; private List<Department> children; public List<Department> getChildren() { return children; } public void setChildren(List<Department> children) { this.children = children; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getParent_id() { return parent_id; } public void setParent_id(String parent_id) { this.parent_id = parent_id; } public Integer getSequence() { return sequence; } public void setSequence(Integer sequence) { this.sequence = sequence; } public Integer getLevel() { return level; } public void setLevel(Integer level) { this.level = level; } public Boolean isEnabled() { return enabled; } public void setEnabled(Boolean enabled) { this.enabled = enabled; } public String getOperate_time() { return operate_time; } public void setOperate_time(String operate_time) { this.operate_time = operate_time; } }View Code
3、编写接口类,代码如下:
1 package com.repository; 2 import org.apache.ibatis.annotations.Mapper; 3 import java.util.List; 4 @Mapper 5 public interface DepartmentInterface { 6 public List<com.entity.Department> getDepartmentById(String id); 7 }
4、编写mapper.xml文件,此处采用了递归法。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <mapper namespace="com.repository.DepartmentInterface"> <resultMap id="BaseResultMap" type="com.entity.Department"> <id property="id" column="id"/> <result column="name" property="name"/> <result column="parent_id" property="parent_id"/> <result column="operate_time" property="operate_time"/> </resultMap> <resultMap id="DepartmentWithChildren" type="com.entity.Department" extends="BaseResultMap"> <collection column="id" ofType="com.entity.Department" select="com.repository.DepartmentInterface.getDepartmentById" property="children"> </collection> </resultMap> <select id="getDepartmentById" resultMap="DepartmentWithChildren"> select dp.id,dp.name,dp.parent_id,dp.operate_time from sys_organization dp where dp.`parent_id`=#{id} and enabled=true order by sequence asc; </select> </mapper>View Code
5、编写控制类。
@RestController public class DepartmentController { @Autowired DepartmentInterface dep; @RequestMapping("/department") //读取所有部门及其子部门 public List<Department> getDepartment(){ List<Department> dp= dep.getDepartmentById("0"); return dp; } }
6、页面输出结果
[{"id":"0000000001","name":"总公司","parent_id":"0","operate_time":"2021-11-15 20:46:12","children":[{"id":"0100000000","name":"分公司1","parent_id":"0000000001","children":[{"id":"0101000000","name":"部门1","parent_id":"0100000000","children":[]},{"id":"0102000000","name":"部门2","parent_id":"0100000000","children":[]}]},{"id":"0200000000","name":"分公司2","parent_id":"0000000001","children":[]}]}]