🧑🎓 个人主页:Silence Lamb
📖 本章内容:【递归方式-实现多级分类】
场景:商品三级分类
- 对于分类来说,一般包括一级分类,二级分类,三级分类–多级分类
- 一般2级分类是比较好做的,大部分网站都是左边点击二级分类,右边显示相对应商品
-
例如:
商品列表
岗位列表
部门列表
方案:递归方式实现
- 🌳数据库的结构设计
-- ----------------------------
-- 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": "研发部门"
}
]
}
]
}
]
}