Hutool树结构工具-TreeUtil
- 加入依赖
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.10</version>
</dependency>
- 表结构及测试数据
-- sys.city definition
CREATE TABLE `city` (
`id` int(11) NOT NULL DEFAULT '0',
`pid` int(11) DEFAULT NULL,
`cityname` varchar(255) DEFAULT NULL,
`type` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(1, 0, '中国', 0);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(2, 1, '北京', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(3, 1, '安徽', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(4, 1, '福建', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(5, 1, '甘肃', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(6, 1, '广东', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(7, 1, '广西', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(8, 1, '贵州', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(9, 1, '海南', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(10, 1, '河北', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(11, 1, '河南', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(12, 1, '黑龙江', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(13, 1, '湖北', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(14, 1, '湖南', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(15, 1, '吉林', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(16, 1, '江苏', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(17, 1, '江西', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(18, 1, '辽宁', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(19, 1, '内蒙古', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(20, 1, '宁夏', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(21, 1, '青海', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(22, 1, '山东', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(23, 1, '山西', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(24, 1, '陕西', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(25, 1, '上海', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(26, 1, '四川', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(27, 1, '天津', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(28, 1, '西藏', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(29, 1, '新疆', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(30, 1, '云南', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(31, 1, '浙江', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(32, 1, '重庆', 1);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(36, 3, '安庆', 2);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(37, 3, '蚌埠', 2);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(38, 3, '巢湖', 2);
INSERT INTO city
(id, pid, cityname, `type`)
VALUES(39, 3, '池州', 2);
service层代码
package com.demo.service.impl;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.demo.dao.MenuMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
/**
* 使用Huttol 工具类来实现菜单树
*
* @Author lhj
* @Date 2023-09-10 0:31
*/
@Service
public class MenuServiceImpl {
private static final Logger logger = LoggerFactory.getLogger(MenuServiceImpl.class);
@Autowired
private MenuMapper menuMapper;
/**
* 使用Huttol 工具类来实现菜单树
* <p>
* .setIdKey("id"); ----- 设置树结构 主键key
* .setParentIdKey("parentId"); ----- 设置树结构 父类key
* .setChildrenKey("children"); ----- 设置树结构 子类key
* .setNameKey("name"); ----- 设置数据 名称key
* .setWeightKey("created_time"); ----- 设置数据 排序 依据字段
* @return
*/
public List<Tree<String>> listMenu() {
List<Map<String, Object>> list = menuMapper.listMenu();
TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
treeNodeConfig.setIdKey("id");
treeNodeConfig.setParentIdKey("parentId");
treeNodeConfig.setChildrenKey("children");
treeNodeConfig.setNameKey("name");
treeNodeConfig.setWeightKey("createdTime");
// 假设父节点为0
List<Tree<String>> treeNodes = TreeUtil.build(list, "0", treeNodeConfig,
(treeNode, tree) -> {
tree.setId(treeNode.get("id").toString()); // 设置树结构 主键key
tree.setName(treeNode.get("cityname").toString()); // 设置数据 名称key
tree.setParentId(treeNode.get("pid").toString()); // 设置树结构 父类key
tree.setWeight(treeNode.get("id").toString()); // 设置数据 排序 依据字段
});
return treeNodes;
}
}