递归查询树形菜单

菜单表设计,一级菜单parentId都是0,二级菜单直接是插入对应得父级id即可

public List<TResource> queryResource() {
    //查询所有得一级菜单
    Example example = new Example(TResource.class);
    Example.Criteria resourceCriteria = example.createCriteria();
    resourceCriteria.andEqualTo("parentId", 0L);
    example.orderBy("resOrder").asc();
    List<TResource> parentList =  resourceMapper.selectByExample(example);
    for(TResource tResource : parentList){
        tResource.setChildren(childList(tResource.getId()));
    }
    return parentList;
}

/**
 * 查询子菜单
 * @param parentId
 * @return
 */
public List<TResource> childList(Long parentId){
    Example example = new Example(TResource.class);
    Example.Criteria resourceCriteria = example.createCriteria();
    resourceCriteria.andEqualTo("parentId", parentId);
    example.orderBy("resOrder").asc();
    List<TResource> childList = resourceMapper.selectByExample(example);
  
for(TResource tResource : childList){
     //递归查询子菜单下面是否还存在子菜单 tResource.setChildren(childList(tResource.getId())); }
return childList; }

这里我用得是tk-mybatis,和mybatis-plus是差不多得功能,都是对mybaits得进一步封装,不需要我们去手动得写单表得sql语句。

 

posted @ 2020-08-07 15:00  沐言星空  阅读(563)  评论(0编辑  收藏  举报