查询三级分类数据

  1. 表结构 pms_category
    在这里插入图片描述
  2. 实体类 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;
    }
    
  3. 查询三级分类的业务逻辑
        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;
        }
    
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值