美文网首页Java从零到企业级电商项目实战
11、【分类模块管理】——修改分类名字接口开发

11、【分类模块管理】——修改分类名字接口开发

作者: CodeGroup | 来源:发表于2018-09-12 20:42 被阅读5次

接口逻辑,管理员在修改分类名字的时候,我们根据操作分类的Id来对分类名字进行修改。关于在首先我们判断登陆者是否是管理员,我们在10、【分类模块管理】——添加分类接口开发有说明

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()){
            //是管理员,修改我们处理分类的逻辑
            return iCategoryService.updateCategoryName(categoryId,categoryName);

        }else {
            return ServerResponse.createByErrorMessage("无权限操作,需要管理员权限");
        }
    }

server:

//更新品类
    ServerResponse updateCategoryName(Integer categoryId,String categoryName);

serverImpl:

 //更新修改分类
    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);
        category.setUpdateTime(new Date());
        int RowCount= categoryMapper.updateByPrimaryKeySelective(category);

        if(RowCount>0){
            return ServerResponse.createBySuccessMessage("更新品类名字成功");
        }
        return ServerResponse.createByErrorMessage("服务器错误,更新品类字失败");

    }

调用的是逆向工程生成的MapperMapper.xml
Mapper:

    int updateByPrimaryKeySelective(Category record);

Mapper.xml:

<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>

接口测试:


image.png

相关文章

网友评论

    本文标题:11、【分类模块管理】——修改分类名字接口开发

    本文链接:https://www.haomeiwen.com/subject/bjbbgftx.html