分类管理模块主要是在后台进行控制,有以下四个子模块:获取节点
、增加节点、获得当前节点的所有直接子节点、获得当前节点的所有子节点。其中重点是如何设计及封装无限层级的树状数据结构,回用到递归算法的设计思想。
无限层级的树状数据结构是通过数据库来实现的,在设计的时候在分类表当中,有分类id、parent_id两个字段,当parent_id是0时表示是根结点。
1.增加分类
******************Controller********************
@ResponseBody
@RequestMapping("add_category.do")
public ServerResponse addCategory(HttpSession session,String categoryName,@RequestParam(value = "parentId",defaultValue = "0") Integer parentId){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getMsg());
}
ServerResponse response=iUserService.checkUserRole(user);
if(response.isSuccess()){
return iCategoryService.addCategory(categoryName, parentId);
}
return response;
}
*******************service*******************
//添加分类
public ServerResponse addCategory(String categoryName,Integer parentId){
if(parentId==null || StringUtils.isBlank(categoryName)){
return ServerResponse.createByErrorMsg("参数错误");
}
Category category=new Category();
category.setName(categoryName);
category.setParentId(parentId);
category.setStatus(true);
int rowCount=categoryMapper.insert(category);
if (rowCount>0){
return ServerResponse.createBySuccess(category);
}
return ServerResponse.createByErrorMsg("增加品类失败");
}
2.更新分类名
*******************Controller*******************
@ResponseBody
@RequestMapping("update_category_name.do")
public ServerResponse updateCategoryName(HttpSession session,String categoryName,Integer categoryId){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getMsg());
}
ServerResponse response=iUserService.checkUserRole(user);
if(response.isSuccess()){
return iCategoryService.updateCategoryName(categoryId, categoryName);
}
return response;
}
*******************Service*******************
//更新品类名称
public ServerResponse updateCategoryName(Integer categoryId,String categoryName){
if(categoryId==null || StringUtils.isBlank(categoryName)){
return ServerResponse.createByErrorMsg("参数错误");
}
Category category=new Category();
category.setId(categoryId);
category.setName(categoryName);
int rowCount=categoryMapper.updateByPrimaryKeySelective(category);
if(rowCount>0){
return ServerResponse.createBySuccessMsg("更新品类名称成功");
}
return ServerResponse.createBySuccessMsg("更新品类名称失败");
}
3.获得当前节点的所有直接子节点
*******************Controller*******************
//根据父节点,获取所有的平级子节点
@ResponseBody
@RequestMapping("get_children_parallel_category.do")
public ServerResponse<List<Category>> getChildrenParallelCategory(HttpSession session,Integer parentId){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getMsg());
}
ServerResponse response=iUserService.checkUserRole(user);
if(response.isSuccess()){
return iCategoryService.getChildrenParallelCategory(parentId);
}
return response;
}
*******************Service*******************
//根据父id,获取所有的平级子节点
public ServerResponse<List<Category>> getChildrenParallelCategory(Integer parentId){
if(parentId==null){
return ServerResponse.createByErrorMsg("参数错误");
}
List<Category> categoryList=categoryMapper.selectCategoryByParentId(parentId);
if(CollectionUtils.isEmpty(categoryList)){
logger.info("未找到当前分类的子分类");
}
return ServerResponse.createBySuccess(categoryList);
}
*******************xml*******************
<select id="selectCategoryByParentId" parameterType="int" resultMap="BaseResultMap">
select
<include refid="Base_Column_List"/>
from mmall_category
where parent_id=#{parentId}
</select>
4.获得当前节点的所有子节点
*******************Controller*******************
//获取当前节点和当前节点的所有子节点
@ResponseBody
@RequestMapping("get_category_deep_children_category.do")
public ServerResponse getCategoryAndDeepChildrenCategory(HttpSession session,Integer categoryId){
User user=(User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),ResponseCode.NEED_LOGIN.getMsg());
}
ServerResponse response=iUserService.checkUserRole(user);
if(response.isSuccess()){
return iCategoryService.getCategoryAndDeepChildrenCategory(categoryId);
}
return response;
}
*******************Service*******************
public ServerResponse<List<Integer>> getCategoryAndDeepChildrenCategory(Integer categoryId){
if (categoryId==null){
return ServerResponse.createByErrorMsg("参数错误");
}
Set<Category> categorySet= Sets.newHashSet();
categorySet=findChildrenCategory(categorySet,categoryId);
List<Integer> categoryIdList= Lists.newArrayList();
for (Category categoryItem:categorySet){
categoryIdList.add(categoryItem.getId());
}
return ServerResponse.createBySuccess(categoryIdList);
}
/**
* 获取到的节点不要重复展示,所以要重写Category的equals和hashCode方法
* 使用递归算法递归查询子节点
*/
private Set<Category> findChildrenCategory(Set<Category> categorySet,Integer categoryId){
Category category=categoryMapper.selectByPrimaryKey(categoryId);
if(category != null){
categorySet.add(category);
}
//查找子节点,递归算法要有推出条件
//还要注意mybatis对于返回结集合会进行处理,如果集合是null,则不会返回null
List<Category> categoryList=categoryMapper.selectCategoryByParentId(categoryId);
for(Category categoryItem:categoryList){
findChildrenCategory(categorySet,categoryItem.getId());
}
return categorySet;
}
分类管理模块的重点是获得当前节点的所有子节点时,递归算法的设计,以及为了获取到不重复的节点,重写Category的equals和hashCode方法。
网友评论