zoukankan      html  css  js  c++  java
  • 【SSM电商项目后台开发】005-分类管理模块

    一、功能概述

    二 学习目标

    三、数据表

    四、主要接口设计

    地址:https://gitee.com/imooccode/happymmallwiki/wikis/%E5%90%8E%E5%8F%B0_%E5%93%81%E7%B1%BB%E6%8E%A5%E5%8F%A3

    (1)增加节点:/manage/category/add_category.do
    (2)修改品类名字:/manage/category/set_category_name.do
    (3).获取品类子节点(平级):/manage/category/get_category.do
    (4)获取当前分类id及递归子节点categoryId:/manage/category/get_deep_category.do

    五、DAO层

    Category.java

    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);
    }
    View Code

    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" >
            #{createTime,jdbcType=TIMESTAMP},
          </if>
          <if test="updateTime != null" >
            #{updateTime,jdbcType=TIMESTAMP},
          </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" parameterType="int" resultMap="BaseResultMap">
        SELECT
        <include refid="Base_Column_List"/>
        FROM mmall_category
        WHERE parent_id = #{parentId}
      </select>
    </mapper>
    View Code

    六、Service层

    ICategoryService.java:

    package com.mmall.service;
    
    import com.mmall.common.ServerResponse;
    
    import java.util.List;
    
    /**
     * @author GenshenWang.nomico
     * @date 2018/4/8.
     */
    public interface ICategoryService {
        ServerResponse<String> addCategory(Integer parentId, String categoryName);
    
        ServerResponse<String> updateCategoryName(Integer categoryId, String categoryName);
    
        ServerResponse getChildrenParallelCategory(Integer categoryId);
    
        ServerResponse<List<Integer>> getCategoryAndDeepChildrenCategory(Integer categoryId);
    
    }
    View Code

    CategoryServiceImpl.java:

    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.Collections;
    import java.util.List;
    import java.util.Set;
    
    /**
     * @author GenshenWang.nomico
     * @date 2018/4/8.
     */
    @Service("iCategoryService")
    public class CategoryServiceImpl implements ICategoryService {
    
        Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
    
        @Autowired
        CategoryMapper categoryMapper;
    
        @Override
        public ServerResponse<String> addCategory(Integer parentId, String categoryName){
            if (parentId == null || StringUtils.isBlank(categoryName)){
                return ServerResponse.createByErrorMsg("参数错误");
            }
    
            Category category = new Category();
            category.setParentId(parentId);
            category.setName(categoryName);
            category.setStatus(true);//这个分类是可用的
            int rowCount = categoryMapper.insert(category);
            if (rowCount > 0){
                return ServerResponse.createBySuccess("添加品类成功");
            }
            return ServerResponse.createByErrorMsg("添加品类失败");
        }
    
        @Override
        public ServerResponse<String> 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.createBySuccess("修改品类成功");
            }
            return ServerResponse.createByErrorMsg("修改品类失败");
    
        }
    
        @Override
        public ServerResponse getChildrenParallelCategory(Integer categoryId){
            if (categoryId == null){
                return ServerResponse.createByErrorMsg("参数错误");
            }
            List<Category> categoryList = categoryMapper.selectCategoryChildrenByParentId(categoryId);
            if (CollectionUtils.isEmpty(categoryList)){
                logger.info("未找到当前分类的子类");
            }
            return ServerResponse.createBySuccess(categoryList);
        }
    
        @Override
        public ServerResponse<List<Integer>> getCategoryAndDeepChildrenCategory(Integer categoryId){
            if (categoryId == null){
                return ServerResponse.createByErrorMsg("参数错误");
            }
            Set<Category> categorySet = Sets.newHashSet();
            findChildCategory(categorySet, categoryId);
            List<Integer> cayegoryList = Lists.newArrayList();
            for (Category category : categorySet) {
                cayegoryList.add(category.getId());
            }
            return ServerResponse.createBySuccess(cayegoryList);
        }
    
        //递归算法,算出子节点
        //注意:此处 用Set去重Category,需要对Category重写Hashcode和equals方法
        public 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;
        }
    }
    View Code

    七、Controller层

    package com.mmall.controller.backend;
    
    import com.mmall.common.Const;
    import com.mmall.common.ResponseCode;
    import com.mmall.common.ServerResponse;
    import com.mmall.pojo.Category;
    import com.mmall.pojo.User;
    import com.mmall.service.ICategoryService;
    import com.mmall.service.IUserService;
    import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
    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.RequestMethod;
    import org.springframework.web.bind.annotation.RequestParam;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    import javax.servlet.http.HttpSession;
    
    /**
     * @author GenshenWang.nomico
     * @date 2018/4/8.
     */
    @Controller
    @RequestMapping(value = "/manage/category")
    public class CategoryManageController {
    
        @Autowired
        IUserService iUserService;
        @Autowired
        ICategoryService iCategoryService;
    
        /**
         * 添加品类
         * @param session
         * @param parentId
         * @param categoryName
         * @return
         */
        @RequestMapping(value = "/add_category.do", method = RequestMethod.POST)
        @ResponseBody
        public ServerResponse addCategory(HttpSession session,
                                          @RequestParam(value = "parentId", defaultValue = "0") Integer parentId,
                                          String categoryName){
            // 判断用户是否登录
            User user = (User) session.getAttribute(Const.CURRENT_USER);
            if (user == null){
                return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
            }
            // 判断用户是否有权限操作
            ServerResponse<String> response = iUserService.checkAdminRole(user);
            if (response.isSuccess()){
                // 添加
                return iCategoryService.addCategory(parentId, categoryName);
            }else {
                return response;
            }
    
        }
    
        /**
         * 更改品类名称
         * @param session
         * @param categoryId
         * @param categoryName
         * @return
         */
        @RequestMapping(value = "/set_category_name.do", method = RequestMethod.POST)
        @ResponseBody
        public ServerResponse setCategoryName(HttpSession session,
                                                      Integer categoryId,
                                                      String categoryName){
            User user = (User) session.getAttribute(Const.CURRENT_USER);
            if (user == null){
                return ServerResponse.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录");
            }
    
            ServerResponse<String> response = iUserService.checkAdminRole(user);
            if (response.isSuccess()){
                return iCategoryService.updateCategoryName(categoryId, categoryName);
            }else {
                return response;
            }
    
        }
    
        /**
         * 获取当前品类节点下的所有子节点(平级)
         * @param session
         * @param categoryId
         * @return
         */
        @RequestMapping(value = "/get_category.do", method = RequestMethod.POST)
        @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.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(), "未登录,请先登录");
            }
    
            ServerResponse<String> response = iUserService.checkAdminRole(user);
            if (response.isSuccess()){
                return iCategoryService.getChildrenParallelCategory(categoryId);
            }else {
                return response;
            }
        }
    
    
        /**
         * 获取当前分类id及递归子节点categoryId
         * @param session
         * @param categoryId
         * @return
         */
        @RequestMapping(value = "/get_deep_category.do", method = RequestMethod.POST)
        @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.createByErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(), "未登录,请先登录");
            }
    
            ServerResponse<String> response = iUserService.checkAdminRole(user);
            if (response.isSuccess()){
                return iCategoryService.getCategoryAndDeepChildrenCategory(categoryId);
            }else {
                return response;
            }
    
        }
    
    
    
    
    
    
    
    
    }
    View Code
  • 相关阅读:
    开发之前的思考-UI结构设计
    UI事件监听的击穿
    实战开发中UI资源制作标准
    巧用九宫格以减少UI资源量
    UI元素的相对自适应
    UI开发核心问题-UI随屏幕自适应
    制作滚动视图(ScrollView)
    制作复选框(Toggle)
    制作下拉菜单(PopupList)
    制作输入框(Input)
  • 原文地址:https://www.cnblogs.com/noaman/p/8763361.html
Copyright © 2011-2022 走看看