Java仿淘宝首页分类列表功能的示例代码

时间:2019-04-14
本文章向大家介绍Java仿淘宝首页分类列表功能的示例代码,主要包括Java仿淘宝首页分类列表功能的示例代码使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

在之前的博文中,我们已经完成了用户模块的所有的功能,那么在接下来的几篇博文中,我们来完成分类管理功能模块。

先来看一下后台的分类管理都有哪些功能点

后台品类管理其实就是对商品的一个管理,主要分为增加品类、更新品类名称、获取同级品类结点和获取品类id及子节点品类

一、分类管理模块-增加品类功能的实现

先来看Service层

// 添加品类 
 public ServerResponse addCategory(String categoryName, Integer parentId){ 
  if(parentId == null || StringUtils.isBlank(categoryName)){ 
   return ServerResponse.createByErrorMessage("参数错误"); 
  } 
 
  Category category = new Category(); 
  category.setName(categoryName); 
  category.setParentId(parentId); 
  category.setStatus(true); 
 
  int rowCount = categoryMapper.insert(category); 
  if(rowCount > 0){ 
   return ServerResponse.createBySuceessMessage("添加品类成功"); 
  } 
  return ServerResponse.createByErrorMessage("添加品类失败"); 
 } 

添加品类相对来说还是比较简单的。和之前的注册逻辑有点相似。首先校验前端传过来的categoryName和parentId是否存在,如果不存在则提示参数错误,否则就继续使用JavaBean的实例来增加品类。同样的,在用JavaBean增加完之后,将结果插入到数据库中,如果返回的生效行数大于0,则添加品类成功,否则添加品类失败。

再来看Controller层

 /** 
  * 管理品类-增加品类 
  * @param categoryName 
  * @param parentId 
  * @param session 
  * @return 
  */ 
 @RequestMapping(value = "add_category.do") 
 @ResponseBody 
 public ServerResponse addCategory(String categoryName, @RequestParam(value = "parentId", defaultValue = "0") int parentId, HttpSession session) { 
  User user = (User) session.getAttribute(Const.CURRENT_USER); 
  if (user == null) { 
   return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录"); 
  } 
//  校验是否是管理员 
  if (iUserService.checkAdmin(user).isSuccess()) { 
   return iCategoryService.addCategory(categoryName, parentId); 
  } else { 
   return ServerResponse.createByErrorMessage("无权限操作,请登录管理员"); 
  } 
 } 

首先有一个不同的地方在与RequestMapping的value值,只有一个接口名称,而没有规定接口请求的方法,是因为品类管理模块是网站管理员进行后台管理,属于后台模块。针对于后台模块,其是公司内部员工使用,不需要对外界进行公开,所以使用默认的GET方式请求就可以。

后台功能管理的通用逻辑就是首先验证用户是否处于登录状态,如果用户处于登录状态,再来验证当前登录的是不是网站管理员,如果不是管理员,则无权进行相关的管理操作,如果是管理员,就可以进行后台的管理。在进行后台功能管理的逻辑中,一般的直接返回在Service层中方法处理结果就可以了。

  在上述方法中,需要判断用户的登录状态,所以需要引入用户服务,然后直接调用相应的方法即可。

二、分类管理模块-更新品类名称功能的实现

先来看Service层

// 更新品类名称 
 public ServerResponse updateCategoryName(String categoryName, Integer categoryId){ 
  if(categoryId == null || StringUtils.isBlank(categoryName)){ 
   return ServerResponse.createByErrorMessage("更新品类参数错误"); 
  } 
  Category category = new Category(); 
  category.setId(categoryId); 
  category.setName(categoryName); 
 
  int rowCount = categoryMapper.updateByPrimaryKeySelective(category); 
  if(rowCount > 0){ 
   return ServerResponse.createBySuceessMessage("更新品类名称成功"); 
  } 
  return ServerResponse.createByErrorMessage("更新品类名称失败"); 
 } 

和之前的处理逻辑完全一样,这里不再一一赘述。

再来看Controller层

/** 
  * 管理品类-更新品类名称 
  * @param categoryName 
  * @param categoryId 
  * @param session 
  * @return 
  */ 
 @RequestMapping(value = "update_category_name") 
 @ResponseBody 
 public ServerResponse updateCategoryName(String categoryName, Integer categoryId, HttpSession session){ 
  User user = (User)session.getAttribute(Const.CURRENT_USER); 
  if(user == null){ 
   return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录"); 
  } 
  if(iUserService.checkAdmin(user).isSuccess()){ 
   return iCategoryService.updateCategoryName(categoryName, categoryId); 
  }else{ 
   return ServerResponse.createByErrorMessage("无权限操作,请登录管理员"); 
  } 
 } 

和之前的处理逻辑完全一样,这里不再一一赘述。

三、分类管理模块-获取平级品类结点(后台商品搜索)功能的实现

Service层

// 平级查询品类结点 
 public ServerResponse<List<Category>> getChildrenParalleCategory(Integer categoryId){ 
  List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId); 
  if(CollectionUtils.isEmpty(categoryList)){ 
   logger.info("未找到当前分类的子分类"); 
  } 
  return ServerResponse.createBySuccess(categoryList); 
 } 

处理一组商品信息,往往使用集合的方式,根据集合不同种类,其适用长青也不一样。这里,我用的是List集合,一是考虑到List集合方便遍历操作,也方便管理。因为是管理商品,所以指定List集合的泛型为Category,通过categoryMapper的selectCategoryChildrenByParentId方法来进行商品id的查询。在逻辑判断上,使用Java中封装好的CollectionUtils工具类,来判断集合的返回结果是否为空,如果为空就打印一行日志,否则将执行成功的categoryList结果返回即可。这里的logger是饿哦们自己封装的日志打印工具类,关于他的用法,简单提一下

private org.slf4j.Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);

注意,这个logger使用的是slf4j包下的,不要导错包了,然后LoggerFactory.getLogger(classs),需要传递一个参数,就是当前需要打印日志的类,例如这里的CategoryServiceImpl.class。即可在控制台看到日志的打印结果。

Controller层

/** 
  * 管理品类-获取同级品类的结点 
  * @param categoryId 
  * @param session 
  * @return 
  */ 
 @RequestMapping(value = "get_category.do") 
 @ResponseBody 
 public ServerResponse getChildrenParalleCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId, HttpSession session){ 
  User user = (User)session.getAttribute(Const.CURRENT_USER); 
  if(user == null){ 
   return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录"); 
  } 
  if(iUserService.checkAdmin(user).isSuccess()){ 
   return iCategoryService.getChildrenParalleCategory(categoryId); 
  }else { 
   return ServerResponse.createByErrorMessage("无权限操作,请登录管理员"); 
  } 
 } 

出于实际情况的考虑,当商品数量为0时,不需要对商品品类进行管理,所以使用RequestParam注解的defaultValue="0"来规定一个参数的默认值。其余的逻辑处理和之前的完全一样。

四、分类管理模块-获取品类id及子结点功能的实现

看Service层

public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){ 
  Set<Category> categorySet = Sets.newHashSet(); 
  findChildCategory(categoryId, categorySet); 
  List<Integer> categoryIdList = Lists.newArrayList(); 
  if(categoryId != null){ 
   for(Category categoryItem : categorySet){ 
    categoryIdList.add(categoryItem.getId()); 
   } 
  } 
  return ServerResponse.createBySuccess(categoryIdList); 
 } 
 
// 递归算法,算出子节点 
 private Set<Category> findChildCategory(Integer categoryId, Set<Category> categorySet){ 
  Category category = categoryMapper.selectByPrimaryKey(categoryId); 
  if(category != null){ 
   categorySet.add(category); 
  } 
//  查找子节点 
  List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId); 
  for(Category categoryItem : categoryList){ 
   findChildCategory(categoryItem.getId(), categorySet); 
  } 
  return categorySet; 
 } 

主方法是selectCategoryAndChildrenById,辅助方法为findChildCategory,通过递归算出子节点。在辅助方法中,通过categoryId来查询出商品的id信息,并且加入到Set集合中,再通过foreach循环来遍历出商品的子节点,最后返回categorySet。在主方法中通过调用辅助方法,将商品的id及子节点全部查出,然后放到List集合中,再通过foreach循环遍历出我们想要的结果,最后直接返回categoryIdList即可。

Controller层

/** 
  * 管理品类-获取id及子节点品类 
  * @param categoryId 
  * @param session 
  * @return 
  */ 
 @RequestMapping(value = "get_deep_category.do") 
 @ResponseBody 
 public ServerResponse getCategoryAndDeepChildrenCategory(@RequestParam(value = "categoryId", defaultValue = "0") Integer categoryId, HttpSession session){ 
  User user = (User)session.getAttribute(Const.CURRENT_USER); 
  if(user == null){ 
   return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录"); 
  } 
  if(iUserService.checkAdmin(user).isSuccess()){ 
   return iCategoryService.selectCategoryAndChildrenById(categoryId); 
  }else{ 
   return ServerResponse.createByErrorMessage("无权限操作,请登录管理员"); 
  } 
 }

和之前的获取品类同级结点的逻辑处理完全一样,这里就不一一赘述了。

五、补充用户模块

在后台品类管理模块中,用到了校验当前登录的用户是否是管理员的方法,这个是在用户模块中写到的,之前我忘记写了,所以,在这里做一个补充。

用户模块的Service层

// 用户后台-校验是否是管理员 
 public ServerResponse checkAdmin(User user){ 
  if(user != null && user.getRole().intValue() == Const.Role.ROLE_ADMIN){ 
   return ServerResponse.createBySuccess(); 
  } 
  return ServerResponse.createByError(); 
 } 

因为是管理员相关,所以只需要在Service层中进行逻辑处理,不需要再在Controller中声明。该方法传入一个user对象,通过封装好的Role接口进行权限判定,如果返回值为ADMIN,则视为管理员,直接返回成功,否则返回失败。

写到这里,后台的品类管理模块就写完了。因为该模块的功能接口比较少,所以用了较长的篇幅全部写在一篇博文中,这样也方便大家一次性就学完后台的品类管理模块。

在接下来的博文中,继续推进项目进度,将为大家带来后台商品模块的开发,希望大家跟上进度。

如果在之前的博文中你遇到了什么问题,欢迎留言反馈,我会尽可能为大家解决问题。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。