版权声明:转载需声明本人出品 https://blog.csdn.net/weixin_40288381/article/details/88833426
后台
后台中模块中的请求都需先管理员先登录
添加分类
Controller层
@RequestMapping("add_category.do")
@ResponseBody
public ServerResponse addCategory(HttpSession session,String categoryName,@RequestParam(value = "parentId",defaultValue = "0") int parentId){
User user = (User)session.getAttribute(Const.CURRENT_USER);
if(user == null){
return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录,请先登录");
}
//校验是否为管理员
if(iUserService.checkAdminRole(user).isSuccess()){
//是管理员
return iCategoryService.addCategory(categoryName,parentId);
}else{
//非管理员
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
}
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.createBySuccess("添加品类成功");
}
return ServerResponse.createByErrorMessage("添加品类失败");
}
表单
<h3>Add Category</h3>
<form name="AddCategory" action="/manage/category/add_category.do" method="post">
categoryName <input type="text" name="categoryName"/><br>
parentId <input type="text" name="parentId"/><br>
<input type="submit" value="submit"/>
</form>
返回JSON对象
{“status”:0,“msg”:“登录成功”,“data”:{“id”:6,“username”:“Admin”,“password”:"",“email”:“[email protected]”,“phone”:“13553382811”,“question”:“question8”,“answer”:“answer8”,“role”:1,“createtime”:1553612265000,“updatetime”:1553612265000}}
修改分类名
Controller层
@RequestMapping("set_category_name.do")
@ResponseBody
public ServerResponse setCategoryName(HttpSession session,Integer categoryId,String categoryName){
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.updateCategoryName(categoryId,categoryName);
}
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
Service层
public ServerResponse updateCategoryName(Integer categoryId,String categoryName){
if(categoryId == null || StringUtils.isBlank(categoryName)){
return ServerResponse.createByErrorMessage("更新品类参赛错误");
}
//new Category对象插入对应的数据
Category category = new Category();
category.setId(categoryId);
category.setName(categoryName);
int rowCount = categoryMapper.updateByPrimaryKeySelective(category);
if(rowCount >0){
return ServerResponse.createBySuccess("更新品类成功");
}
return ServerResponse.createByErrorMessage("更新品类失败");
}
表单
<h3>Set CategoryName</h3>
<form name="SetCategoryName" action="/manage/category/set_category_name.do" method="post">
categoryId <input type="text" name="categoryId"/><br>
categoryName <input type="text" name="categoryName"/><br>
<input type="submit" value="submit"/>
</form>
返回JSON对象
{“status”:0,“data”:“更新品类成功”}
返回当前节点所有的子节点(不包括更下一层)
Controller层
@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);
}
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
Service层
public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){
//新写categoryMapper中selectCategoryChildrenByParentId方法
List<Category> list = categoryMapper.selectCategoryChildrenByParentId(categoryId);
if(CollectionUtils.isEmpty(list)){
logger.info("未找到当前分类的子分类");
}
return ServerResponse.createBySuccess(list);
}
Mapper层
<select id="selectCategoryChildrenByParentId" resultMap="BaseResultMap" parameterType="int">
SELECT
<include refid="Base_Column_List"/>
FROM mall_category
where parent_id=#{parent_id}
</select>
表单
<h3>Get Category</h3>
<form name="GetCategory" action="/manage/category/get_category.do" method="post">
categoryId <input type="text" name="categoryId"/><br>
<input type="submit" value="submit"/>
</form>
返回JSON对象
{“status”:0,“data”:[{“id”:7,“parentId”:1,“name”:“å°é£Žæ‰‡”,“status”:true,“sortOrder”:null,“createtime”:1553607998000,“updatetime”:1553608002000},{“id”:8,“parentId”:1,“name”:“å°å†°ç®±”,“status”:true,“sortOrder”:null,“createtime”:1553608022000,“updatetime”:1553608027000}]}
返回当前节点的所有子节点(递归到更下一层)
Controller
@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和递归子节点的Category信息
return iCategoryService.selectCategoryAndChildrenById(categoryId);
}
return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
}
Service层
public ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId){
Set<Category> categorySet = Sets.newHashSet();
findChildCategory(categorySet,categoryId);
//将Set中的节点添加到list
List<Integer> categotyList = Lists.newArrayList();
if(categoryId != null){
for(Category categoryItem : categorySet){
categotyList.add(categoryItem.getId());
}
}
return ServerResponse.createBySuccess(categotyList);
}
//递归算法,算出子节点
private Set<Category> findChildCategory(Set<Category> categorySet,Integer categoryId){
//根据Id返回Category对象,若存在则添加进入参的Set
Category category = categoryMapper.selectByPrimaryKey(categoryId);
if(category != null){
categorySet.add(category);
}
//先得到当前节点的所有子节点Id-只包括一层
//将所有子节点Id封装到List中,继续进行获取"子节点直到子节点没有子节点"
List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
for (Category categoryItem : categoryList){
findChildCategory(categorySet,categoryItem.getId());
}
return categorySet;
}
表单
<h3>Get DeepCategory</h3>
<form name="GetDeepCategory" action="/manage/category/get_deep_category.do" method="post">
categoryId <input type="text" name="categoryId"/><br>
<input type="submit" value="submit"/>
</form>
返回JSON对象
{“status”:0,“data”:[1,7,8]}