gulimall adds a three-level classification tree structure

The following attributes need to be added to the third-level classification---representing data not in this table


//custom attributes
@JsonInclude(JsonInclude.Include.NON_EMPTY) //If it is not empty, spring will return this property
@TableField(exist = false)
private List<CategoryEntity> children;

Then add the code in seversimpl

@Override
public List<CategoryEntity> listWithTree() {
    //1 Check out all categories
    List<CategoryEntity> entities = baseMapper.selectList(null);
    //2 assembled into a parent-child tree structure
    //2.1 Find all 1-level categories
    List<CategoryEntity> level1Menus = 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());
    return level1Menus;
}

//Recursively find all submenus of the menu
private List<CategoryEntity> getChildrens(CategoryEntity root, List<CategoryEntity> all) {
    List<CategoryEntity> cildren = all.stream().filter(categoryEntity -> {
        return categoryEntity.getParentCid() == root.getCatId();
    }).map(categoryEntity -> {
        //1. Find the submenu
        categoryEntity.setChildren(getChildrens(categoryEntity, all));
        return categoryEntity;
    }).sorted((menu1, menu2) -> {
        //2. Menu sorting
        return (menu1.getSort() == null ? 0 : menu1.getSort()) - (menu2.getSort() == null ? 0 : menu2.getSort());
    }).collect(Collectors.toList());
    return cildren;
}

Guess you like

Origin blog.csdn.net/jiangchuan465/article/details/127695785