mybatisplus实现无限级菜单(递归)
1. entity类
package com.cjy.mybatis_plus.entity;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
/**
* 菜单实体类
*
* @author xieyixu
* @since 2022-06-14
*/
@Data
@ApiModel(value = "Menu对象", description = "")
public class Menu implements Serializable {
private static final long serialVersionUID = 1L;
@TableId
@ApiModelProperty("主键id")
private Integer id;
@ApiModelProperty("菜单名称")
private String name;
@ApiModelProperty("图标")
private String icon;
@ApiModelProperty("路径")
private String path;
@ApiModelProperty("父级ID")
private Integer parentId;
/**
* @TableField:忽略数据库字段
* 不能这样写 private List<Menu> children
* 必须new ArrayList<>() 指明类型
*/
@TableField(exist = false)
private List<Menu> children = new ArrayList<>();
}
2. IMenuService接口
package com.cjy.mybatis_plus.service;
import com.cjy.mybatis_plus.entity.Menu;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/**
* <p>
* 服务类
* </p>
*
* @author xieyixu
* @since 2022-06-14
*/
public interface IMenuService extends IService<Menu> {
List<Menu> findAll(Integer type) ;
}
3.MenuServiceImpl类
package com.cjy.mybatis_plus.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.cjy.mybatis_plus.entity.Menu;
import com.cjy.mybatis_plus.mapper.MenuMapper;
import com.cjy.mybatis_plus.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;
/**
* <p>
* 服务实现类
* </p>
*
* @author xieyixu
* @since 2022-06-14
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
@Autowired
private MenuMapper menuMapper;
@Override
public List<Menu> findAll(Integer type) {
QueryWrapper<Menu> menuQueryWrapper = new QueryWrapper<>();
List<Menu> EndMenu = new ArrayList<>();
// 查询到父菜单
menuQueryWrapper.eq("parent_id", "0");
List<Menu> RootMenu = menuMapper.selectList(menuQueryWrapper);
// 查询所有除父菜单之外的菜单
menuQueryWrapper.clear();
menuQueryWrapper.ne("parent_id", "0");
List<Menu> menusListNeRoot = menuMapper.selectList(menuQueryWrapper);
// 去遍历父菜单 将父菜单和所有菜单比较 如果有菜单的parent_id==它的id 就将它添加为children
for (Menu menu : RootMenu) {
//查询子菜单 递归方法
EndMenu.add(getChildrenList(menu, menusListNeRoot));
}
return EndMenu;
}
public Menu getChildrenList(Menu rootMenu, List<Menu> menuChildrenList) {
for (Menu menu : menuChildrenList) {
if (rootMenu.getId().equals(menu.getParentId())) {
rootMenu.getChildren().add(getChildrenList(menu, menuChildrenList));
}
}
return rootMenu;
}
}
4.Controller类
package com.cjy.mybatis_plus.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cjy.mybatis_plus.entity.Menu;
import com.cjy.mybatis_plus.entity.User;
import com.cjy.mybatis_plus.service.IMenuService;
import com.cjy.mybatis_plus.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <p>
* 前端控制器
* </p>
*
* @author xieyixu
* @since 2022-06-14
*/
@RestController
@RequestMapping("/mybatis_plus/menu")
public class MenuController {
@Autowired
private IMenuService iMenuService;
@GetMapping("/find")
public Result find(@RequestParam(value = "userType",defaultValue = "0") Integer userType){
Result ret = new Result<Menu>();
List<Menu> menus = iMenuService.findAll(userType);
ret.setCode(200);
ret.setMsg("请求成功");
ret.setResult(menus);
return ret;
}
}
说明:
上述代码里我传了一个参数 userType 这里我用来做菜单筛选的 筛选部分我没放上来 。