ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

部门架构的树状结构数据表设计及读取(Spring boot+mybatis+mysql)

2021-11-16 16:02:10  阅读:129  来源: 互联网

标签:children String parent Spring boot id 数据表 public name


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

 

标签:children,String,parent,Spring,boot,id,数据表,public,name
来源: https://www.cnblogs.com/wwwzgy/p/15561613.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有