无限极分类,级联。

主要就是递归的应用,此应用用于新闻类别。

代码中开始默认level为0,

通过一次for调用,把子类递归查出,在通过第二次for,把categroy在前台显示的地方,加上“折”号。

public List<Category> getCategorysList(Integer level){
        
        List<Category> ls= categoryMapper.getCategorys(level);
        List<Category> temp = new ArrayList<Category>();
        
        for (Category category : ls) {
            temp.add(category);
            List<Category> categorysList = getCategorysList(category.getId());
            
            for (Category category2 : categorysList) {
                category2.setName("└─"+category2.getName());
            }
            temp.addAll(categorysList);
        }
        return temp;

猜你喜欢

转载自www.cnblogs.com/sdgtxuyong/p/12574427.html