当前位置:网站首页>三级分类/菜单的查询——树形结构
三级分类/菜单的查询——树形结构
2022-07-24 05:40:00 【胡尚】
本文以商品的分类来举例,实体类如下:
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import java.io.Serializable;
import java.util.List;
import com.baomidou.mybatisplus.annotation.TableName;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.experimental.Accessors;
/** * <p> * 商品三级分类 * </p> * * @author 胡尚 * @since 2022-07-21 */
@Data
@EqualsAndHashCode(callSuper = false)
@Accessors(chain = true)
@ApiModel(value="Category对象", description="商品三级分类")
@TableName("pms_category")
public class PmsCategoryDO implements Serializable {
private static final long serialVersionUID = 1L;
@ApiModelProperty(value = "分类id")
@TableId(value = "cat_id", type = IdType.AUTO)
private Long catId;
@ApiModelProperty(value = "分类名称")
private String name;
@ApiModelProperty(value = "父分类id")
private Long parentCid;
@ApiModelProperty(value = "层级")
private Integer catLevel;
@ApiModelProperty(value = "是否显示[0-不显示,1显示]")
private Integer showStatus;
@ApiModelProperty(value = "排序")
private Integer sort;
@ApiModelProperty(value = "图标地址")
private String icon;
@ApiModelProperty(value = "计量单位")
private String productUnit;
@ApiModelProperty(value = "商品数量")
private Integer productCount;
@ApiModelProperty(value = "子分类")
@TableField(exist = false)
private List<PmsCategoryDO> children;
}
业务方法,本文的重点如下,其他控制层与持久化层的代码就省略了
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.gulimall.mapper.PmsCategoryMapper;
import com.hs.gulimall.pojo.PmsCategoryDO;
import com.hs.gulimall.service.PmsCategoryService;
import org.springframework.stereotype.Service;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/** * <p> * 商品三级分类 服务实现类 * </p> * * @author 胡尚 * @since 2022-07-21 */
@Service
public class PmsCategoryServiceImpl extends ServiceImpl<PmsCategoryMapper, PmsCategoryDO> implements PmsCategoryService {
@Override
public List<PmsCategoryDO> listWithTree() {
// 查询所有的分类
List<PmsCategoryDO> categoryList = baseMapper.selectList(null);
// 查询所有的一级分类,然后再查询二级三级分类
List<PmsCategoryDO> categoryTreeList = categoryList.stream()
.filter(categoryEntity -> categoryEntity.getCatLevel() == 1)
.map(category -> {
// 查询出当前商品分类的子分类,调用下方的getChildrens()方法
category.setChildren(getChildrens(category, categoryList));
return category;
})
.sorted((o1, o2) -> {
// 根据数据库中存储的排序字段 进行排序,但是该字段可能为null,所以先进行一些处理
o1.setSort(o1.getSort() == null ? 0 : o1.getSort());
o2.setSort(o2.getSort() == null ? 0 : o2.getSort());
return o1.getSort() - o2.getSort();
})
.collect(Collectors.toList());
return categoryTreeList;
}
/** * 获取当前父商品分类信息的子分类 * @param root 父商品分类 * @param categoryList 所有商品分类 * @return 当前商品分类的所有子分类信息 */
private List<PmsCategoryDO> getChildrens(PmsCategoryDO root, List<PmsCategoryDO> categoryList){
List<PmsCategoryDO> childrenList = categoryList.stream()
// 根据父分类id进行比较
.filter(category -> Objects.equals(root.getCatId(), category.getParentCid()))
.map(category -> {
// 可能二级分类 还有第三级分类,所以继续递归
category.setChildren(getChildrens(category, categoryList));
return category;
})
.sorted((o1, o2) -> {
// 根据数据库中存储的排序字段 进行排序,但是该字段可能为null,所以先进行一些处理
o1.setSort(o1.getSort() == null ? 0 : o1.getSort());
o2.setSort(o2.getSort() == null ? 0 : o2.getSort());
return o1.getSort() - o2.getSort();
})
.collect(Collectors.toList());
return childrenList;
}
}
最终结果如下图所示:
边栏推荐
- [lvgl] API functions for setting, changing and deleting styles of components
- ES10 subtotal flat and flatmap
- Redis特殊数据类型-GEO
- [lvgl layout] flexible layout
- Take you to understand the inventory deduction principle of MySQL database
- Today, let's talk about the underlying architecture design of MySQL database. How much do you know?
- kubernetes简介和架构及其原理
- SSH Remote Access and control
- 创建WPF项目
- 记录PHPSerializer工具类反序列化遇到的坑
猜你喜欢

Detailed explanation of class loader and parental delegation mechanism

【LVGL(2)】LVGL入门,在CodeBlock上进行模拟以及移植STM32

File system and log analysis
![[audio decoding chip] Application of vs1503 audio decoding chip](/img/ee/0d5f95fba647592cc95f1e9f410bc9.png)
[audio decoding chip] Application of vs1503 audio decoding chip

Solution: exit status 1 and exit status 145 appear when the console uses NVM to control the node version

I have seven schemes to realize web real-time message push, seven!

Special effects - bubble tailing occurs when the mouse moves
![[lvgl (1)] a brief introduction to lvgl](/img/2e/2e155f1d3669c27ad1b090ca954224.png)
[lvgl (1)] a brief introduction to lvgl

Several common problems of SQL server synchronization database without public IP across network segments

kubernetes简介和架构及其原理
随机推荐
js和ts学习总结
Write cookies, sessionstorage, localstorage and session at will
String question
[lvgl (6)] display Chinese settings and make Chinese font
Talk about strong cache and negotiation cache
Redis data type - list list
PyQt5入门——学生管理系统
Special effects - cobweb background effects
Redis基本类型-哈希Hash
LM393 voltage comparator and its typical circuit introduction
类的加载器 和 双亲委派机制详解
【学习笔记】网页出现白屏可能的原因与优化方法
Redis data type -string (string type)
Detailed explanation of class loader and parental delegation mechanism
ES10 subtotal flat and flatmap
Promise (try to implement a promise by yourself) more detailed comments and other interfaces are not completed yet. Let's continue next time.
Account and authority management
广度优先搜索(模板使用)
【学习笔记】Web页面渲染的流程
【LVGL(4)】对象的事件及事件冒泡