Mybatis-Plus父子選單
首先來看一下實現的效果
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;
}
MenuMapper
@Mapper
@Repository
public interface MenuMapper extends BaseMapper<Menu> {
}
MenuService
public interface IMenuRoleService extends IService<MenuRole> {
}
MenuServiceImpl
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;
/**
* <p>
* 選單實現層
* </p>
*
* @author zcx
* @since 2022-03-31
*/
@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements IMenuService {
@Autowired
MenuMapper menuMapper;
/**
* @author zhaochangxin
* @Description:查詢所有選單
* @return: java.util.List<com.zcx.pojo.Menu>
* @date 2022/3/31 17:13
* @throws
*/
@Override
public List<Menu> queryAllMenu() {
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;
}
/**
* @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<Menu> menuChildrenList) {
for (Menu menu : menuChildrenList) {
if (RootMenu.getId().equals(menu.getParentId())){
RootMenu.getChildren().add(getChildrenList(menu, menuChildrenList));
}
}
return RootMenu;
}
}
MenuController
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;
/**
* <p>
* 前端控制器
* </p>
*
* @author zcx
* @since 2022-03-31
*/
@Controller
@RequestMapping("/menu")
public class MenuController {
@Autowired
IMenuService menuService;
@ResponseBody
@RequestMapping("/list")
public ApiResult queryAllMenu(){
List<Menu> menus = menuService.queryAllMenu();
return ApiResult.success("查詢所有選單",menus);
}
}
有問題或更好的辦法請留言告知,謝謝您的觀看。