Java转树形结构案例(菜单管理)

实体类(一) 映射数据库

@Entity
@Table
public class SysMenu  {

    private Long id;

     /** 菜单名称 */
    @NotBlank(message="菜单名称不能为空")
    private String name;

    /** 菜单URL */
    @NotBlank(message="菜单url不能为空")
     private String url;

    /** 父菜单ID */
    private Long parentId;

    /** 是否可见(0:否,1:是) */
    private Integer displayFlag;
}

实体类(二) 转换结构使用

public class SysMenuVO {

    private Long Id;

    /** 菜单名称 */
    private String name;

    /** 菜单URL */
    private String url;

    /** 父菜单ID */
    private Long parentId;

    /** 是否可见(0:否,1:是) */
    private Integer displayFlag;

    private List childMenus;
}

Controller层

@Controller
@RequestMapping("sys/menu")
public class SystemMenuController extends BaseController {

private Logger logger = LoggerFactory.getLogger(ContractArchiveController.class);

@Autowired
private SystemMenuService systemMenuService;

    @RequestMapping("findAll")
    @ResponseBody
    public ResultBean> findAllMenu(){
        List allMenu = systemMenuService.findAllMenu();
        ResultBean> menuResult = new ResultBean<>(allMenu);
        menuResult.setCode(ResultBean.SUCCESS);
        menuResult.setMessages("all menu return successful");
        return menuResult;
    }
}

Service层

@Service
public class SystemMenuService {

    @Autowired
    private SystemMenuRepository systemMenuRepository;

    //获取所有菜单
    public List findAllMenu(){
        return this.getMenusFromDb();
    }

    private List getMenusFromDb(){
        SysMenu searchStandard = new SysMenu();
        //这里采用SpringDataJpa的Specification查询,不懂的可以百度
        Specification specification = getSpecification(searchStandard);
        //按父级菜单的id排序获取查询结果集
        Sort orders = new Sort(Sort.Direction.ASC,"parentId");
        List resultMenu =(List)systemMenuRepository.findAll(specification,orders);
        //获取树形结构的结果
        List menuTree = genMenuTree(resultMenu);
        return menuTree;
    }

    private Specification getSpecification(SysMenu sysMenu){
        Specification specification = new Specification() {
             @Override
            public Predicate toPredicate(Root root, CriteriaQuery cq, CriteriaBuilder cb) {
                List predicates = new ArrayList();
                //只获取没有删除的菜单
                predicates.add(cb.equal(root.get("delFlag"), Constants.DEL_FLAG_ZERO));
                if(null != sysMenu.getParentId()) {
                    predicates.add(cb.equal(root.get("parentId"), sysMenu.getParentId()));
                }
                return cq.where(predicates.toArray(new Predicate[predicates.size()])).getRestriction();
            }
        };
        return specification;
    }

    private List genMenuTree(List sysMenus) {
        //将数据转成树形结构
        Map> menuMap = new HashMap<>();
        for(SysMenu menu : sysMenus) {
            Long parentId = menu.getParentId();
            List tmpList = menuMap.get(parentId) == null ? new ArrayList() : menuMap.get(parentId);
            SysMenuVO vo = new SysMenuVO();
            BeanUtils.copyProperties(menu, vo);
            tmpList.add(vo);
            menuMap.put(parentId, tmpList);
        }
        //设置一个根节点
        SysMenuVO vo = new SysMenuVO();
        vo.setId(Constants.ROOT_MENU_PARENT_ID);
        this.setMenuChidren(vo, menuMap);
        return vo.getChildMenus();
    }

    private void setMenuChidren(SysMenuVO sysMenuVO, Map> menuMap) {
        //获取根节点下面子菜单集合
        List childMenus = menuMap.get(sysMenuVO.getId());
        if(CollectionUtils.isNotEmpty(childMenus)) {
            //将子菜单集合set进根菜单
            sysMenuVO.setChildMenus(childMenus);
            for(SysMenuVO vo : childMenus) {
                //设置每一个子菜单下的子菜单集合
                setMenuChidren(vo, menuMap);
            }
        }
    }
}

注:

本例使用SpringDataJpa 和 Hibernate 仅供自己学习使用

你可能感兴趣的:(Java转树形结构案例(菜单管理))