记MyBaits-Plus 实现菜单的无限层关系

Mybatis-Plus父子菜单

首先来看一下实现的效果
记MyBaits-Plus 实现菜单的无限层关系_第1张图片

pojo层

@Data
@TableName("platform_role")
public class Role implements Serializable {

    private static final long serialVersionUID = 1L;

      @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private String roleName;

    private String roleKey;

    private Long createdBy;

      @TableField(fill = FieldFill.INSERT)
    private LocalDateTime createdDate;

    private Long lastModifiedBy;

    private LocalDateTime lastModified;

    private String owner;

}
@Mapper
@Repository
public interface MenuMapper extends BaseMapper {
}
public interface IMenuRoleService extends IService {
}
package com.zcx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcx.pojo.Menu;
import com.zcx.mapper.MenuMapper;
import com.zcx.service.IMenuService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 

* 菜单实现层 *

* * @author zcx * @since 2022-03-31 */ @Service public class MenuServiceImpl extends ServiceImpl implements IMenuService { @Autowired MenuMapper menuMapper; /**  * @author zhaochangxin  * @Description:查询所有菜单  * @return: java.util.List  * @date 2022/3/31 17:13  * @throws  */ @Override public List
queryAllMenu() { QueryWrapper menuQueryWrapper = new QueryWrapper<>(); List EndMenu = new ArrayList<>(); // 查询到父菜单 menuQueryWrapper.eq("parent_id","0"); List RootMenu = menuMapper.selectList(menuQueryWrapper); // 查询所有除父菜单之外的菜单 menuQueryWrapper.clear(); menuQueryWrapper.ne("parent_id","0"); List menusListNeRoot = menuMapper.selectList(menuQueryWrapper); // 去遍历父菜单 将父菜单和所有菜单比较 如果有菜单的parent_id==他的id 就将他添加为children for (Menu menu : RootMenu) { //查询子菜单 递归方法 EndMenu.add(getChildrenList(menu, menusListNeRoot)); } return EndMenu; } /**  * @author zhaochangxin  * @Description:将父菜单和所有菜单比较 如果有菜单的parent_id==他的id 就将他添加为children  * @Param childMenu: 父菜单 * @Param menuChildrenList: 所有除了父菜单的菜单  * @return: com.zcx.pojo.Menu  * @date 2022/3/31 17:11  */ public Menu getChildrenList(Menu RootMenu, List menuChildrenList) { for (Menu menu : menuChildrenList) { if (RootMenu.getId().equals(menu.getParentId())){ RootMenu.getChildren().add(getChildrenList(menu, menuChildrenList)); } } return RootMenu; } }
package com.zcx.controller;


import com.zcx.common.vo.ApiResult;
import com.zcx.pojo.Menu;
import com.zcx.service.IMenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 

* 前端控制器 *

* * @author zcx * @since 2022-03-31 */ @Controller @RequestMapping("/menu") public class MenuController { @Autowired IMenuService menuService; @ResponseBody @RequestMapping("/list") public ApiResult queryAllMenu(){ List
menus = menuService.queryAllMenu(); return ApiResult.success("查询所有菜单",menus); } }

有问题或更好的办法请留言告知,谢谢您的观看。

你可能感兴趣的:(记MyBaits-Plus 实现菜单的无限层关系)