从零到企业级SSM电商项目实战教程(二十五)查询节点和递归查找功能开发

CategoryManageController

@RequestMapping("get_category.do")
@ResponseBody
public ServerResponse getChildrenParallelCategory(HttpSession session, @RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
    User user =(User)session.getAttribute(Const.CURRENT_USER);
    if(user == null){
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户名未登录,请登录");
    }
    if(iUserService.checkAdminRole(user).isSuccess()){

        //查询子节点的category信息,并且不递归,保持平级
        return iCategoryService.getChildrenParallelCategory(categoryId);
    }else{
        return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
    }

}

@RequestMapping("get_deep_category.do")
@ResponseBody
public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session, @RequestParam(value = "categoryId",defaultValue = "0") Integer categoryId){
    User user =(User)session.getAttribute(Const.CURRENT_USER);
    if(user == null){
        return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户名未登录,请登录");
    }
    if(iUserService.checkAdminRole(user).isSuccess()){
        //查询当前节点的id和递归子节点的id
        //0-->10000-->100000
        return iCategoryService.selectCategoryAndChildrenById(categoryId);

    }else{
        return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
    }
}

ICategoryService.java

ServerResponse> getChildrenParallelCategory(Integer categoryId);

ServerResponse selectCategoryAndChildrenById(Integer categoryId);

CategoryServiceImpl.java

public ServerResponse> getChildrenParallelCategory(Integer categoryId){
    List categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
    if(CollectionUtils.isEmpty(categoryList)){
        logger.info("未找到当前分类的子分类");
    }
    return ServerResponse.createBySuccess(categoryList);
}

/*
 * @Description: 递归查询本节点的id及孩子节点的id
 *
 * @auther: Geekerstar(jikewenku.com)
 * @date: 2018/6/23 13:29
 * @param: [categoryId]
 * @return: com.mmall.common.ServerResponse
 */
public ServerResponse selectCategoryAndChildrenById(Integer categoryId){
    Set categorySet = Sets.newHashSet();
    findChildCategory(categorySet,categoryId);

    List categoryIdList = Lists.newArrayList();
    if(categoryId != null){
        for (Category categoryItem : categorySet){
            categoryIdList.add(categoryItem.getId());
        }
    }
    return ServerResponse.createBySuccess(categoryIdList);
}

//递归算法,算出子节点
private Set findChildCategory(Set categorySet,Integer categoryId){
    Category category = categoryMapper.selectByPrimaryKey(categoryId);
    if(category != null){
        categorySet.add(category);
    }
    //查找子节点,递归算法一定要有一个退出的条件
    List categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
    for (Category categoryItem : categoryList){
        findChildCategory(categorySet,categoryItem.getId());
    }
    return categorySet;
}

CategoryMapper.java

List selectCategoryChildrenByParentId(Integer parentId);

CategoryMapper.xml


Category.java

重写equals方法和hashcode方法

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;

    Category category = (Category) o;

    return id != null ? id.equals(category.id) : category.id == null;
}

@Override
public int hashCode() {
    return id != null ? id.hashCode() : 0;
}
本站所有文章均由网友分享,仅用于参考学习用,请勿直接转载,如有侵权,请联系网站客服删除相关文章。若由于商用引起版权纠纷,一切责任均由使用者承担
极客文库 » 从零到企业级SSM电商项目实战教程(二十五)查询节点和递归查找功能开发

Leave a Reply

欢迎加入「极客文库」,成为原创作者从这里开始!

立即加入 了解更多