java代码实现部门树以及任意树结构的获取

java代码实现部门树以及任意树结构的获取,第1张

java代码实现部门树以及任意树结构的获取

 表结构如下:


SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ----------------------------
-- Table structure for my_dept
-- ----------------------------
DROP TABLE IF EXISTS `my_dept`;
CREATE TABLE `my_dept`  (
  `dept_id` int NOT NULL AUTO_INCREMENT COMMENT 'id值',
  `parent_id` int NULL DEFAULT NULL COMMENT '上级部门',
  `dept_name` varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT NULL COMMENT '名称',
  `sort` int NULL DEFAULT NULL COMMENT '排序',
  `status` bit(1) NOT NULL COMMENT '状态',
  `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 8 CHARACTER SET = utf8 COLLATE = utf8_general_ci ROW_FORMAT = DYNAMIC;

-- ----------------------------
-- Records of my_dept
-- ----------------------------
INSERT INTO `my_dept` VALUES (1, 0, '成都总公司', 1, b'1', '2022-01-30 11:01:09', '2022-01-30 18:21:26');
INSERT INTO `my_dept` VALUES (2, 1, '研发部门', 1, b'1', '2022-01-30 11:01:28', '2022-01-30 11:01:30');
INSERT INTO `my_dept` VALUES (3, 1, '市场部门', 2, b'1', '2022-01-30 11:01:47', '2022-01-30 11:01:48');
INSERT INTO `my_dept` VALUES (4, 1, '运维部门', 3, b'1', '2022-01-30 11:02:01', '2022-01-30 11:02:04');
INSERT INTO `my_dept` VALUES (5, 0, '宜宾分公司', 2, b'1', '2022-01-30 11:07:36', '2022-01-30 14:18:48');
INSERT INTO `my_dept` VALUES (6, 5, '营销部门', 1, b'1', '2022-01-30 11:08:40', '2022-01-30 20:32:40');
INSERT INTO `my_dept` VALUES (7, 5, '运维部门', 2, b'1', '2022-01-30 11:08:56', '2022-01-30 18:03:56');
INSERT INTO `my_dept` VALUES (8, 5, '公关部门', 3, b'1', '2022-01-30 20:47:32', '2022-01-30 20:47:29');

SET FOREIGN_KEY_CHECKS = 1;

对应的实体类:
package com.chunwai.entity;

import com.baomidou.mybatisplus.annotation.IdType;

import java.util.ArrayList;
import java.util.Date;

import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.EqualsAndHashCode;
import org.springframework.format.annotation.DateTimeFormat;


@Data
@EqualsAndHashCode(callSuper = false)
public class MyDept implements Serializable {

    private static final long serialVersionUID = 1L;

    
    @TableId(value = "dept_id", type = IdType.AUTO)
    private Integer deptId;

    
    private Integer parentId;

    
    private String deptName;

    
    private Integer sort;

    
    private Boolean status;

    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date createTime;

    
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private Date updateTime;

    
    @TableField(exist = false)
    private List children = new ArrayList<>();

}
 业务层逻辑:
package com.chunwai.service.impl;

import com.chunwai.entity.MyDept;
import com.chunwai.mapper.MyDeptMapper;
import com.chunwai.service.IMyDeptService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.velocity.runtime.directive.Break;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Service
public class MyDeptServiceImpl extends ServiceImpl implements IMyDeptService {

    @Override
    public List treeData() {
        List myDeptList = list();
        Map myDeptMap = myDeptList.stream()
                .collect(Collectors.toMap(MyDept::getDeptId, MyDept -> MyDept));
        List arrayList = new ArrayList<>();
        myDeptList.forEach(myDept -> {
            if (myDept.getParentId() == 0 ){
                arrayList.add(myDept);
            }else {
                MyDept dept = myDeptMap.get(myDept.getParentId());
                dept.getChildren().add(myDept);
            }
        });
        return arrayList;
    }
}
控制层调用返回给前端:
package com.chunwai.controller;


import com.chunwai.entity.CourseType;
import com.chunwai.entity.MyDept;
import com.chunwai.service.ICourseTypeService;
import com.chunwai.service.IMyDeptService;
import com.chunwai.utils.AjaxResult;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/myDept")
@Api(tags = "部门树")
public class MyDeptController {
    @Autowired
    private IMyDeptService myDeptService;

    @GetMapping
    public AjaxResult treeData(){
        List treeData = myDeptService.treeData();
        return AjaxResult.me().setResultObj(treeData);

    }
}

返回结果如下:

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5715530.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存