• 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html
  • 极客专栏正式上线!欢迎访问 https://www.jikewenku.com/topic.html

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

项目实战 Geekerstar 10个月前 (06-23) 382次浏览 已收录 0个评论 扫描二维码
文章目录[隐藏]

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<List<Category>> getChildrenParallelCategory(Integer categoryId);

ServerResponse selectCategoryAndChildrenById(Integer categoryId);

CategoryServiceImpl.java

public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){
    List<Category> 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<Category> categorySet = Sets.newHashSet();
    findChildCategory(categorySet,categoryId);

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

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

CategoryMapper.java

List<Category> selectCategoryChildrenByParentId(Integer parentId);

CategoryMapper.xml

<select id="selectCategoryChildrenByParentId" resultMap="BaseResultMap" parameterType="int">
  SELECT
  <include refid="Base_Column_List"/>
  FROM mmall_category
  WHERE parent_id = #{parentId}
</select>

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;
}

喜欢 (0)
[247507792@qq.com]
分享 (0)
Geekerstar
关于作者:
本站技术支持

您必须 登录 才能发表评论!

  • 精品技术教程
  • 编程资源分享
  • 问答交流社区
  • 极客文库知识库

客服QQ


QQ:2248886839


工作时间:09:00-23:00