环境:springboot jpa mybatisPlus vue springcloud(alibaba)
1.pojo对象准备
package com.lifei.gulimall.product.entity;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import java.io.Serializable;
import java.util.Date;
import java.util.List;
import lombok.Data;
/**
* 商品三级分类
*
* @author lifei
* @email sunlightcs@gmail.com
* @date 2020-04-28 22:39:30
*/
@Data
@TableName("pms_category")
public class CategoryEntity implements Serializable {
private static final long serialVersionUID = 1L;
/**
* 分类id
*/
@TableId
private Long catId;
/**
* 分类名称
*/
private String name;
/**
* 父分类id
*/
private Long parentCid;
/**
* 层级
*/
private Integer catLevel;
/**
* 是否显示[0-不显示,1显示]
*/
private Integer showStatus;
/**
* 排序
*/
private Integer sort;
/**
* 图标地址
*/
private String icon;
/**
* 计量单位
*/
private String productUnit;
/**
* 商品数量
*/
private Integer productCount;
//这是定义在此菜单下的子菜单的列表
//@TableField(exist = false) 说明在数据库的字段中不存在这个字段
@TableField(exist = false)
private List<CategoryEntity> children;
}
//这是定义在此菜单下的子菜单的列表
//@TableField(exist = false) 说明在数据库的字段中不存在这个字段
@TableField(exist = false)
private List<CategoryEntity> children;
因为前两级分类都有自己的子菜单,所以需要定义一个集合容器乘载自己的子菜单
2.service
package com.lifei.gulimall.product.service.impl;
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.lifei.common.utils.PageUtils;
import com.lifei.common.utils.Query;
import com.lifei.gulimall.product.dao.CategoryDao;
import com.lifei.gulimall.product.entity.CategoryEntity;
import com.lifei.gulimall.product.service.CategoryService;
@Service("categoryService")
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, CategoryEntity> implements CategoryService {
@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);
}
//三级分类方法
@Override
public List<CategoryEntity> listWithTree() {
//查出所有分类
List<CategoryEntity> entityList = baseMapper.selectList(null);
//组装成父子结构
/* List<CategoryEntity> level1Menus = entityList.stream().filter((categoryEntity) -> {
return (categoryEntity.getParentCid() == 0);
}).collect(Collectors.toList());*/
//filter 过滤 map 映射 sorted排序 collect(Collectors.toList());封装成列表
List<CategoryEntity> level1Menus = entityList.stream().filter(categoryEntity ->
categoryEntity.getParentCid() == 0
).map((menu)->{
menu.setChildren(getChildrens(menu,entityList));
return menu;
}).sorted((menu1,menu2)->{
return (menu1.getSort()==null?0:menu1.getSort())-(menu2.getSort()==null?0:menu2.getSort());
}).collect(Collectors.toList());
return level1Menus;
}
//递归查询子菜单 并映射到pojo的List<CategoryEntity> children属性
private List<CategoryEntity> getChildrens(CategoryEntity root,List<CategoryEntity> all){
List<CategoryEntity> collect = all.stream().filter(categoryEntity -> {
return categoryEntity.getParentCid() == root.getCatId();
}).map((categoryEntity)->{
categoryEntity.setChildren(getChildrens(categoryEntity,all));
return categoryEntity;
}).sorted((menu1,menu2)-> {
return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
}).collect(Collectors.toList());
return collect;
}
}