千家信息网

springboot构造树形结构数据并查询的方法是什么

发表于:2024-11-17 作者:千家信息网编辑
千家信息网最后更新 2024年11月17日,本篇内容主要讲解"springboot构造树形结构数据并查询的方法是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot构造树形结构数据
千家信息网最后更新 2024年11月17日springboot构造树形结构数据并查询的方法是什么

本篇内容主要讲解"springboot构造树形结构数据并查询的方法是什么",感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习"springboot构造树形结构数据并查询的方法是什么"吧!

因为项目需要,页面上需要树形结构的数据进行展示(类似下图这样),因此需要后端返回相应格式的数据。


我这里用的是springboot+mybatis-plus+mysql,示例的接口是查询一级权限以及二级权限、三级权限整个权限树…

下面是导入的maven依赖

                            org.springframework.boot            spring-boot-starter-web                            com.alibaba            druid-spring-boot-starter            1.1.21                             mysql            mysql-connector-java                                    com.baomidou            mybatis-plus-boot-starter            3.4.0                                  org.projectlombok            lombok            true                            cn.hutool            hutool-all            5.0.6        

下面是实体类Permission

@Datapublic class Permission implements Serializable {    @TableId    private String permissionId;    @NotNull(message = "权限名称不能为空")    private String permissionName;    /**     * 权限标识     */    @NotNull(message = "权限标识不能为空")    private String permissionCode;    /**     * 父菜单ID,如果是-1就表示是一级权限菜单。     */    @NotBlank(message = "父菜单ID不能为空")    private String parentId;    /**     * 前端URL访问接口路径     */    private String path;    /**     * 排序值     */    private Integer sort;    /**     * 创建时间     */    private LocalDateTime createTime;    /**     * 更新时间     */    private LocalDateTime updateTime;    /**     * 0--正常 1--删除     */    private String delFlag;    public Permission() {        this.permissionId = IdUtil.simpleUUID();    }

树形结点类

@Datapublic class TreeNode {    protected String id;    protected String parentId;    protected List children = new ArrayList();    protected boolean hasChildren;   public void addTreeNode(TreeNode node){       children.add(node);   }}

树形结点详细信息类

@Data@EqualsAndHashCode(callSuper = true)public class PermissionTree extends TreeNode implements Serializable {    private String permissionName;    private String permissionCode;    private String path;    private Integer sort;    private String label;    private boolean hasChildren;    public PermissionTree() {    }}

构建树形结点工具类(关键),在这里我用@UtilityClass注解就表示这个类中的方法都是静态方法:

@UtilityClasspublic class TreeUtil {    public  List build(List treeNodes, String root) {        List trees = new ArrayList<>();        for (T treeNode : treeNodes) {            if (root.equals(treeNode.getParentId())) {                trees.add(treeNode);            }            for (T node : treeNodes) {                if (node.getParentId().equals(treeNode.getId())) {                    treeNode.addTreeNode(node);                    treeNode.setHasChildren(true);                }            }        }        return trees;    }    /**     * 通过permission创建树形节点     *     * @param permissionList     * @param root     * @return     */    public List buildTree(List permissionList, String root) {        System.out.println(Arrays.toString(permissionList.toArray()));        List treeNodeList = new ArrayList<>();        PermissionTree treeNode = null;        for (Permission permission : permissionList) {            treeNode = new PermissionTree();            treeNode.setId(permission.getPermissionId());            treeNode.setPermissionName(permission.getPermissionName());            treeNode.setPath(permission.getPath());            treeNode.setSort(permission.getSort());            treeNode.setParentId(permission.getParentId());            treeNode.setLabel(permission.getPermissionName());            treeNode.setHasChildren(false);            treeNodeList.add(treeNode);        }        return TreeUtil.build(treeNodeList, root);    }  }

响应消息主体类

/** * 响应信息主体 * * @param  */@ToString@NoArgsConstructor@AllArgsConstructor@Accessors(chain = true)public class R implements Serializable {        private static final long serialVersionUID = 1L;        private int code;        private String msg;        private T data;        public int getCode() {                return code;        }        public void setCode(int code) {                this.code = code;        }        public String getMsg() {                return msg;        }        public void setMsg(String msg) {                this.msg = msg;        }        public T getData() {                return data;        }        public void setData(T data) {                this.data = data;        }        public static  R ok() {                return restResult(null, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS);        }        public static  R ok(T data) {                return restResult(data, CommonConstants.SUCCESS, CommonConstants.MSG_SUCCESS);        }        public static  R ok(T data, String msg) {                return restResult(data, CommonConstants.SUCCESS, msg);        }        public static  R failed() {                return restResult(null, CommonConstants.FAIL, null);        }        public static  R failed(String msg) {                return restResult(null, CommonConstants.FAIL, msg);        }        public static  R failed(T data) {                return restResult(data, CommonConstants.FAIL, null);        }        public static  R failed(T data, String msg) {                return restResult(data, CommonConstants.FAIL, msg);        }        private static  R restResult(T data, int code, String msg) {                R apiResult = new R<>();                apiResult.setCode(code);                apiResult.setData(data);                apiResult.setMsg(msg);                return apiResult;        }}

数据查询接口mapper类

@Mapperpublic interface PermissionMapper extends BaseMapper{    }

数据逻辑处理业务接口

public interface PermissionService extends IService {      /**     * 构建权限树     *     * @param lazy     * @param parentId     * @return     */    List treePermission(boolean lazy, String parentId);}

数据逻辑处理业务接口实现类

@Servicepublic class PermissionServiceImpl extends ServiceImpl implements PermissionService {/**     * 构建权限树:1、不是懒加载情况,查询全部     * 2、是懒加载,根据parentId查询     *     * @param lazy     * @param parentId     * @return     */    @Override    public List treePermission(boolean lazy, String parentId) {        if (!lazy) {            return TreeUtil.buildTree(                    baseMapper.selectList(Wrappers.lambdaQuery().orderByAsc(Permission::getSort)),                    CommonConstants.PERMISSION_ROOT_ID);        }        String parent = parentId == null ? CommonConstants.PERMISSION_ROOT_ID : parentId;        return TreeUtil.buildTree(                baseMapper.selectList(Wrappers.lambdaQuery().eq(Permission::getParentId, parent).orderByAsc(Permission::getSort)), parent        );    }}

查询权限树请求接口类

@RestController@RequestMapping("/permission")public class PermissionController {    @Autowire    private PermissionService permissionService;    /**     * 查询权限列表,并以树状结构显示     *     * @param lazy 懒加载: false时, parentId这个参数失效, 加载所有的权限; true时, 根据parentId加载     * @param parentId     * @return     */    @RequestMapping(value = "/getTree", method = RequestMethod.GET)    public R getTree(boolean lazy, String parentId) {        return R.ok(permissionService.treePermission(lazy, parentId));    } }

表中测试数据如下(注意它的parent_id

测试一:不是懒加载,查询整个权限树。 结果如下。

{    "code": 0,    "msg": "SUCCESS",    "data": [        {            "id": "1",            "parentId": "-1",            "children": [                {                    "id": "2",                    "parentId": "1",                    "children": [                        {                            "id": "3",                            "parentId": "2",                            "children": [],                            "hasChildren": false,                            "permissionName": "update",                            "permissionCode": null,                            "path": null,                            "sort": 3,                            "label": "update",                            "owned": false                        },                        {                            "id": "4",                            "parentId": "2",                            "children": [],                            "hasChildren": false,                            "permissionName": "insert_role",                            "permissionCode": null,                            "path": null,                            "sort": 4,                            "label": "insert_role",                            "owned": false                        }                    ],                    "hasChildren": true,                    "permissionName": "delete",                    "permissionCode": null,                    "path": null,                    "sort": 2,                    "label": "delete",                    "owned": false                }            ],            "hasChildren": true,            "permissionName": "add",            "permissionCode": null,            "path": null,            "sort": 1,            "label": "add",            "owned": false        },        {            "id": "5",            "parentId": "-1",            "children": [],            "hasChildren": false,            "permissionName": "role:saveRole",            "permissionCode": null,            "path": "/role/saveRole",            "sort": 5,            "label": "role:saveRole",            "owned": false        }    ]}

测试二:是懒加载,根据parent_id查询当前分支。 结果如下。

{    "code": 0,    "msg": "SUCCESS",    "data": [        {            "id": "3",            "parentId": "2",            "children": [],            "hasChildren": false,            "permissionName": "update",            "permissionCode": null,            "path": null,            "sort": 3,            "label": "update",            "owned": false        },        {            "id": "4",            "parentId": "2",            "children": [],            "hasChildren": false,            "permissionName": "insert_role",            "permissionCode": null,            "path": null,            "sort": 4,            "label": "insert_role",            "owned": false        }    ]}

到此,相信大家对"springboot构造树形结构数据并查询的方法是什么"有了更深的了解,不妨来实际操作一番吧!这里是网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

0