java实现多级菜单

在实现菜单查询目前遇到两种方式

1、根据父id依次查询该id为父id的同级菜单,一次查询一个(上下级菜单通过parentId进行关联)

    1.1、前台传入参数为父parentId(select  *  from category where parentId =#{parentId })

   ①

@GetMapping("/list")
public ResponseEntity> queryCategoryByPid(@RequestParam("pid") Long pid) {
    return  ResponseEntity.ok().body(categoryService.list(pid));
}

@Override
public List list(Long pid) {
    final Category category = new Category();
    category.setParentId(pid);
    final List categories = categoryMapper.select(category);
    if (CollectionUtils.isEmpty(categories)) {
        throw new LyException(ExceptionEnum.CATEGORY_ISNULL);
    }
    return categories;
}

2、通过sql一次全部出来所有菜单

 

你可能感兴趣的:(Java后端开发,数据库交互)