001-递归方式-实现三级分类

🧑‍🎓 个人主页:Silence Lamb

📖 本章内容:【递归方式-实现多级分类


场景:商品三级分类

  • 对于分类来说,一般包括一级分类,二级分类,三级分类–多级分类
  • 一般2级分类是比较好做的,大部分网站都是左边点击二级分类,右边显示相对应商品
  • 例如:商品列表 岗位列表 部门列表

    image-20230220041753352


方案:递归方式实现

  • 🌳数据库的结构设计
-- ----------------------------
-- Table structure for dept
-- ----------------------------
DROP TABLE IF EXISTS `dept`;
CREATE TABLE `dept`  (
  `dept_id` bigint(20) NOT NULL AUTO_INCREMENT COMMENT '部门id',
  `parent_id` bigint(20) NULL DEFAULT 0 COMMENT '父部门id',
  `ancestors` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '祖级列表',
  `dept_name` varchar(30) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '部门名称',
  `order_num` int(4) NULL DEFAULT 0 COMMENT '显示顺序',
  `phone` varchar(11) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '联系电话',
  `email` varchar(50) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT NULL COMMENT '邮箱',
  `create_time` datetime NULL DEFAULT NULL COMMENT '创建时间',
  `update_by` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NULL DEFAULT '' COMMENT '更新者',
  `update_time` datetime NULL DEFAULT NULL COMMENT '更新时间',
  PRIMARY KEY (`dept_id`) USING BTREE
) ENGINE = InnoDB AUTO_INCREMENT = 109 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_general_ci COMMENT = '部门表' ROW_FORMAT = DYNAMIC;
  • 导入数据库数据
-- ----------------------------
-- Records of dept
-- ----------------------------
INSERT INTO `dept` VALUES (100, 0, '0', '千川科技', 0, '15888888888', 'miram@qq.com', '2022-10-22 07:43:33', '', '2022-10-27 10:27:24');
INSERT INTO `dept` VALUES (101, 100, '0,100', '河南总公司', 1, '15888888888', '128888888@qq.com', '2022-10-22 07:43:33', '', '2022-10-27 10:48:41');
INSERT INTO `dept` VALUES (103, 101, '0,100,101', '研发部门', 1, '15888888888', '11111111@qq.com', '2022-10-22 07:43:33', '', '2022-11-12 02:57:04');

SET FOREIGN_KEY_CHECKS = 1;	
  • 首先查询出部门集合
    /**
     * 查询部门列表
     *
     * @return 部门列表
     */
    @Override
    public List<Dept> selectDeptList() {
        return baseMapper.selectTDepList();
    }
  • 其次构建部门树信息
    /**
     * 构建部门树
     *
     * @param deptList 部门列表
     * @return 部门书列表
     */
    @Override
    public List<Dept> buildDeptTree(List<Dept> deptList) {
        List<Dept> deptTree = deptList.stream().filter(dept -> dept.getParentId() == 0)
                .map(item -> {
                    item.setChildren(getChildren(item, deptList));
                    return item;
                }).collect(Collectors.toList());
        return deptTree;
    }

    public List<Dept> getChildren(Dept root, List<Dept> deptList) {
        List<Dept> collect = deptList.stream().filter(dept -> dept.getParentId().equals(root.getDeptId()))
                .map(item -> {
                    item.setChildren(getChildren(item, deptList));
                    return item;
                }).collect(Collectors.toList());
        return collect;
    }
  • 返回信息如下
{
    "tree": [
        {
            "deptId": 100,
            "parentId": 0,
            "ancestors": "0",
            "deptName": "千川科技",
            "orderNum": 0,
            "phone": "15888888888",
            "email": "miram@qq.com",
            "createTime": "2022-10-22T07:43:33",
            "updateTime": "2022-10-27T10:27:24",
            "children": [
                {
                    "deptId": 101,
                    "parentId": 100,
                    "ancestors": "0,100",
                    "deptName": "河南总公司",
                    "orderNum": 1,
                    "phone": "15888888888",
                    "email": "128888888@qq.com",
                    "createTime": "2022-10-22T07:43:33",
                    "updateTime": "2022-10-27T10:48:41",
                    "children": [
                        {
                            "deptId": 103,
                            "parentId": 101,
                            "ancestors": "0,100,101",
                            "deptName": "研发部门",
                            "orderNum": 1,
                            "phone": "15888888888",
                            "email": "11111111@qq.com",
                            "createTime": "2022-10-22T07:43:33",
                            "updateTime": "2022-11-12T02:57:04"
                        }
                    ]
                }
            ]
        }
    ]
}

但是如果前端只需要 id lable字段则需要进行字段转换

    @ApiModelProperty("节点ID")
    private Long id;

    @ApiModelProperty("节点名称")
    private String label;
  • 创建部门树实体类
@Data
public class TreeSelect implements Serializable {

    @ApiModelProperty("节点ID")
    private Long id;

    @ApiModelProperty("节点名称")
    private String label;

    @ApiModelProperty("子节点")
    private List<TreeSelect> children;
    
   	public TreeSelect(Dept dept){
        this.id = dept.getDeptId();
        this.label= dept.getDeptName();
        this.children = dept.getChildren().stream().map(TreeSelect::new).collect(Collectors.toList());
    }
}
  • 返回信息如下
{
    "idLabel": [
        {
            "id": 100,
            "label": "千川科技",
            "children": [
                {
                    "id": 101,
                    "label": "河南总公司",
                    "children": [
                        {
                            "id": 103,
                            "label": "研发部门"
                        }
                    ]
                }
            ]
        }
    ]
}	

🏅 代码地址:📢💨递归方式-实现三级分类
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Silence Lamb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值