主要就是递归的应用,此应用用于新闻类别。
代码中开始默认level为0,
通过一次for调用,把子类递归查出,在通过第二次for,把categroy在前台显示的地方,加上“折”号。
实践中,出现重叠的折号,每点击一次,多一个折号,解决办法,需要把mybatis的这条sql,关闭缓存,flushCache="true"
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;