从mysql查询菜单表用java生成菜单树

做的时候采用的框架是springboot+jpa

菜单表如下:
菜单表t_sys_menu
菜单实体:

@Entity
@Table(name = "t_sys_menu")
public class SysMenuDTO{
    private Long id; 
    private String name; //菜单名称
    private Long parentId; //父菜单id
    @Transient
    private List<SysMenuDTO> childMenu = new ArrayList<SysMenuDTO>();
    //setter/getter略
}

Controller层(运用了swagger-ui):

@RestController
@RequestMapping("/api/sys/menu")
public class MenuController extends BaseController{
    @Autowired
    private MenuBO menuBO;

/**
     * 菜单树,根据传入的菜单id查询该菜单下的所有子菜单
     * @param menuId
     * @return
     */
    @RequestMapping(value = "/getMenuTree",method = RequestMethod.GET)
    @ApiOperation(value = "菜单树",notes="菜单树")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "menuId",value = "菜单id",dataType = "Long",paramType = "query")
    })
    @ApiResponses(value = {@ApiResponse(code = 200,message = "菜单树")})
    public JsonMessage<String> getMenuTree(@RequestParam(value = "menuId",required = false) Long menuId)      {
      return JsonMessage.successed(JsonUtil.toDefaultJSONString(menuBO.generateTreeNode(menuId),null));
    }
}

Service层:

@Service
public class MenuBo{
  @Autowired
    private SysMenuBO sysMenuBO;
    /**
     * 生成菜单树
     * @param rootId
     * @return
     */
    public SysMenuDTO generateTreeNode(Long rootId){
        SysMenuDTO root = null;
        List<SysMenuDTO> childrenTreeNode = new ArrayList<>();
        if (rootId != null){
            root = sysMenuBO.findOne(rootId); //根据id查询父菜单对象
            childrenTreeNode = this.getChildNode(rootId); //查询子菜单对象
        }else {
            root = sysMenuBO.findOne(new NumberCondition("parentId",0,NumberCondition.Handler.EQUAL));
            childrenTreeNode = this.getChildNode(root.getId());
        }
        for (SysMenuDTO item:childrenTreeNode){
            SysMenuDTO node = this.generateTreeNode(item.getId());
            root.getChildMenu().add(node);
        }
        return root;
    }

    /**
     * 查询所有子菜单
     * @param nodeId
     * @return
     */
    public List<SysMenuDTO> getChildNode(Long nodeId){
    //根据父id查询所有子菜单
        List<SysMenuDTO> childNodeList = sysMenuBO.findAll(new NumberCondition("parentId",nodeId,NumberCondition.Handler.EQUAL));
       if(childNodeList .size()==0){
            return null;
        }
         return childNodeList ;
    }
}

SysMenuBO继承自BaseBO,BaseBO里面有封装好的查询方法

public class BaseBO<T extends AbstractBaseDTO,K extends Serializable> {
    @PersistenceContext
    public EntityManager em;

    @Autowired
    public BaseDAO<T,K> repository;

    @Transactional
    public void deleteAll(){
        repository.deleteAll();
    }
     public List<T> findAll(BaseCondition condition){
        Specification<T> specification= ConditionUtil.getSpecificationByCondition(condition);
        return repository.findAll(specification);
    }
     public T findOne(K k){
        return repository.findOne(k);
    }
    public T findOne(BaseCondition condition){
        Specification<T> specification= ConditionUtil.getSpecificationByCondition(condition);
        return repository.findOne(specification);
    }
}

当然也可以自己在DAO层里写查询方法。
这样返回的结果就是:

  • 首页
    • 系统管理
      • 用户管理
        • 新增用户
      • 角色管理

这样返回的是一个对象

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页