- 表结构 pms_category
- 实体类 CategoryEntity
@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) private List<CategoryEntity> children; }
- 查询三级分类的业务逻辑
public List<CategoryEntity> listWithTree() { // 1. select all List<CategoryEntity> categoryEntities = baseMapper.selectList(null); // 2.1 fine the level1 final List<CategoryEntity> level1Menus = categoryEntities.stream() .filter(categoryEntity -> categoryEntity.getParentCid() == 0) .map((menu) -> { List<CategoryEntity> children = getChildrens(menu, categoryEntities); menu.setChildren(children); menu.setChildren2(children); return menu; }) .sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))) .collect(Collectors.toList()); return level1Menus; } private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) { List<CategoryEntity> children = all.stream().filter(categoryEntity -> categoryEntity.getParentCid() == root.getCatId()) .map(categoryEntity -> { List<CategoryEntity> c = getChildrens(categoryEntity, all); categoryEntity.setChildren(c); categoryEntity.setChildren2(c); return categoryEntity; }).sorted(Comparator.comparingInt(menu -> (menu.getSort() == null ? 0 : menu.getSort()))) .collect(Collectors.toList()); return children; }
查询三级分类数据
最新推荐文章于 2022-04-08 17:37:43 发布