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; } }
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>
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":[]}]}]