分类管理模块
Posted 虚极静笃
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了分类管理模块相关的知识,希望对你有一定的参考价值。
CategoryManageController:
package com.mmall.controller.backend; import com.mmall.common.Const; import com.mmall.common.ResponseCode; import com.mmall.common.ServerResponse; import com.mmall.pojo.User; import com.mmall.service.ICategoryService; import com.mmall.service.IUserService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpSession; /** * Created by geely */ @Controller @RequestMapping("/manage/category") public class CategoryManageController { @Autowired private IUserService iUserService; @Autowired private ICategoryService iCategoryService; @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("无权限操作,需要管理员权限"); } } @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()){ //更新categoryName return iCategoryService.updateCategoryName(categoryId,categoryName); }else{ return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限"); } } @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("无权限操作,需要管理员权限"); } } }
CategoryServiceImpl:
package com.mmall.service.impl; import com.google.common.collect.Lists; import com.google.common.collect.Sets; import com.mmall.common.ServerResponse; import com.mmall.dao.CategoryMapper; import com.mmall.pojo.Category; import com.mmall.service.ICategoryService; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.List; import java.util.Set; /** * Created by geely */ @Service("iCategoryService") public class CategoryServiceImpl implements ICategoryService { private Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class); @Autowired private CategoryMapper categoryMapper; 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("添加品类失败"); } public ServerResponse updateCategoryName(Integer categoryId,String categoryName){ 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.createBySuccess("更新品类名字成功"); } return ServerResponse.createByErrorMessage("更新品类名字失败"); } public ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId){ List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId); if(CollectionUtils.isEmpty(categoryList)){ logger.info("未找到当前分类的子分类"); } return ServerResponse.createBySuccess(categoryList); } /** * 递归查询本节点的id及孩子节点的id * @param categoryId * @return */ public ServerResponse<List<Integer>> 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; } }
ICategoryService:
package com.mmall.service; import com.mmall.common.ServerResponse; import com.mmall.pojo.Category; import java.util.List; /** * Created by geely */ public interface ICategoryService { ServerResponse addCategory(String categoryName, Integer parentId); ServerResponse updateCategoryName(Integer categoryId, String categoryName); ServerResponse<List<Category>> getChildrenParallelCategory(Integer categoryId); ServerResponse<List<Integer>> selectCategoryAndChildrenById(Integer categoryId); }
Category(这里考虑到set去重,如果set的元素是非基本类型和String,需要以id重写equals和hashcode):
package com.mmall.pojo; import java.util.Date; public class Category { private Integer id; private Integer parentId; private String name; private Boolean status; private Integer sortOrder; private Date createTime; private Date updateTime; public Category(Integer id, Integer parentId, String name, Boolean status, Integer sortOrder, Date createTime, Date updateTime) { this.id = id; this.parentId = parentId; this.name = name; this.status = status; this.sortOrder = sortOrder; this.createTime = createTime; this.updateTime = updateTime; } public Category() { super(); } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public Integer getParentId() { return parentId; } public void setParentId(Integer parentId) { this.parentId = parentId; } public String getName() { return name; } public void setName(String name) { this.name = name == null ? null : name.trim(); } public Boolean getStatus() { return status; } public void setStatus(Boolean status) { this.status = status; } public Integer getSortOrder() { return sortOrder; } public void setSortOrder(Integer sortOrder) { this.sortOrder = sortOrder; } public Date getCreateTime() { return createTime; } public void setCreateTime(Date createTime) { this.createTime = createTime; } public Date getUpdateTime() { return updateTime; } public void setUpdateTime(Date updateTime) { this.updateTime = updateTime; } @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; } }
CategoryMapper.xml:
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd" > <mapper namespace="com.mmall.dao.CategoryMapper" > <resultMap id="BaseResultMap" type="com.mmall.pojo.Category" > <constructor > <idArg column="id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="parent_id" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="name" jdbcType="VARCHAR" javaType="java.lang.String" /> <arg column="status" jdbcType="BIT" javaType="java.lang.Boolean" /> <arg column="sort_order" jdbcType="INTEGER" javaType="java.lang.Integer" /> <arg column="create_time" jdbcType="TIMESTAMP" javaType="java.util.Date" /> <arg column="update_time" jdbcType="TIMESTAMP" javaType="java.util.Date" /> </constructor> </resultMap> <sql id="Base_Column_List" > id, parent_id, name, status, sort_order, create_time, update_time </sql> <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" > select <include refid="Base_Column_List" /> from mmall_category where id = #{id,jdbcType=INTEGER} </select> <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" > delete from mmall_category where id = #{id,jdbcType=INTEGER} </delete> <insert id="insert" parameterType="com.mmall.pojo.Category" > insert into mmall_category (id, parent_id, name, status, sort_order, create_time, update_time) values (#{id,jdbcType=INTEGER}, #{parentId,jdbcType=INTEGER}, #{name,jdbcType=VARCHAR}, #{status,jdbcType=BIT}, #{sortOrder,jdbcType=INTEGER}, now(), now()) </insert> <insert id="insertSelective" parameterType="com.mmall.pojo.Category" > insert into mmall_category <trim prefix="(" suffix=")" suffixOverrides="," > <if test="id != null" > id, </if> <if test="parentId != null" > parent_id, </if> <if test="name != null" > name, </if> <if test="status != null" > status, </if> <if test="sortOrder != null" > sort_order, </if> <if test="createTime != null" > create_time, </if> <if test="updateTime != null" > update_time, </if> </trim> <trim prefix="values (" suffix=")" suffixOverrides="," > <if test="id != null" > #{id,jdbcType=INTEGER}, </if> <if test="parentId != null" > #{parentId,jdbcType=INTEGER}, </if> <if test="name != null" > #{name,jdbcType=VARCHAR}, </if> <if test="status != null" > #{status,jdbcType=BIT}, </if> <if test="sortOrder != null" > #{sortOrder,jdbcType=INTEGER}, </if> <if test="createTime != null" > now(), </if> <if test="updateTime != null" > now(), </if> </trim> </insert> <update id="updateByPrimaryKeySelective" parameterType="com.mmall.pojo.Category" > update mmall_category <set > <if test="parentId != null" > parent_id = #{parentId,jdbcType=INTEGER}, </if> <if test="name != null" > name = #{name,jdbcType=VARCHAR}, </if> <if test="status != null" > status = #{status,jdbcType=BIT}, </if> <if test="sortOrder != null" > sort_order = #{sortOrder,jdbcType=INTEGER}, </if> <if test="createTime != null" > create_time = #{createTime,jdbcType=TIMESTAMP}, </if> <if test="updateTime != null" > update_time = now(), </if> </set> where id = #{id,jdbcType=INTEGER} </update> <update id="updateByPrimaryKey" parameterType="com.mmall.pojo.Category" > update mmall_category set parent_id = #{parentId,jdbcType=INTEGER}, name = #{name,jdbcType=VARCHAR}, status = #{status,jdbcType=BIT}, sort_order = #{sortOrder,jdbcType=INTEGER}, create_time = #{createTime,jdbcType=TIMESTAMP}, update_time = now() where id = #{id,jdbcType=INTEGER} </update> <select id="selectCategoryChildrenByParentId" resultMap="BaseResultMap" parameterType="int"> select <include refid="Base_Column_List"/> from mmall_category where parent_id = #{parentId} </select> </mapper>
CategoryMapper:
package com.mmall.dao; import com.mmall.pojo.Category; import java.util.List; public interface CategoryMapper { int deleteByPrimaryKey(Integer id); int insert(Category record); int insertSelective(Category record); Category selectByPrimaryKey(Integer id); int updateByPrimaryKeySelective(Category record); int updateByPrimaryKey(Category record); List<Category> selectCategoryChildrenByParentId(Integer parentId); }
以上是关于分类管理模块的主要内容,如果未能解决你的问题,请参考以下文章