用的是Mybatis-plus
过程
- 先查出所有的数据
- 查出所有的一级分类
- 查出所有的一级分类下的所有的子集【即先查出所有一级分类下的二级分类,再查出所有二级分类下的所有的三级分类】
代码
controller.java
package com.atguigu.gulimall.product.controller;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import com.atguigu.gulimall.product.vo.Category;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.R;
/**
* ��Ʒ��������
*
* @author dongsilian
* @email dongsilian@gmail.com
* @date 2020-07-04 18:53:37
*/
@RestController
@RequestMapping("product/category")
public class CategoryController {
@Autowired
private CategoryService categoryService;
/**
* 查出所有分类以及子分类,以树形结构组装起来
*/
@RequestMapping("/list/tree")
public R list(){
List<CategoryEntity> categoryEntity = categoryService.listWithTree();
if(categoryEntity == null){
return R.error();
}
return R.ok().put("data",categoryEntity);
}
}
Service.java
package com.atguigu.gulimall.product.service.impl;
import com.atguigu.gulimall.product.vo.Category;
import com.baomidou.mybatisplus.extension.service.additional.query.impl.QueryChainWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.product.dao.CategoryDao;
import com.atguigu.gulimall.product.entity.CategoryEntity;
import com.atguigu.gulimall.product.service.CategoryService;
import org.springframework.util.CollectionUtils;
import javax.annotation.Resource;
@Slf4j
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@Resource
private CategoryDao categoryDao;
@Override
public PageUtils queryPage(Map<String, Object> params) {
IPage<CategoryEntity> page = this.page(
new Query<CategoryEntity>().getPage(params),
new QueryWrapper<CategoryEntity>()
);
return new PageUtils(page);
}
/**
* 查出所有分类父子嵌套
* @return
*/
@Override
public List<CategoryEntity> listWithTree() {
//先查出所有的分类
List<CategoryEntity> entities = baseMapper.selectList(null);
if(CollectionUtils.isEmpty(entities)){
log.error("查询全部分类失败{}",entities);
return null;
}
//组装成父子的树形结构
//先查出一级分类
//使用stream流的时候千万不要管整体的,管一个就行了,最后用一个collect.Collectors.toList就可以了
List<CategoryEntity> levelMenus = entities.stream().filter((categoryEntity) -> {
return categoryEntity.getParentCid() == 0;
}).map((menu) -> {
//得到子菜单
menu.setChildren(getChildrens(menu, entities));
return menu;
}).sorted((menu1, menu2) -> {
return (menu1.getSort()==null?0:menu1.getSort()) - (menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
// getParent0
return levelMenus;
}
//递归查找所有菜单的子菜单
private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
List<CategoryEntity> children = all.stream().filter((categoryEntity) -> {
//拿到第二级菜单
return categoryEntity.getParentCid() == root.getCatId();
}).map((categoryEntity) -> {
//找到子菜单,拿到第三级菜单,把第三级菜单放到第二级菜单下
categoryEntity.setChildren(getChildrens(categoryEntity, all));
return categoryEntity;
}).sorted((menu1, menu2) -> {
//判断menu1.getSort是否为空,为空则置为0,否则就用不为空的本值
return (menu1.getSort()==null?0:menu1.getSort())- (menu2.getSort() == null?0:menu2.getSort());
}).collect(Collectors.toList());
return children;
}
}