我测试更新目录,结果一直提示未登录。我登陆了,然后添加目录也成功,数据库也有。但是就是更新接口这里一直提示未登录。找了好久也没发现,能帮我看看吗

我测试更新目录,结果一直提示未登录。我登陆了,然后添加目录也成功,数据库也有。但是就是更新接口这里一直提示未登录。找了好久也没发现,能帮我看看吗

controller层

@ApiOperation("后台数据更新商品目录")
@PostMapping("/updateCategory")
public ApiResponseObj updateCategory(HttpSession httpSession,
                                     @Valid @RequestBody UpdateCategoryReq updateCategoryReq) throws MallExeception {
    User user = (User)httpSession.getAttribute(Quantity.MALL_USER);
    if (user==null) {
        //用户不存在于session,说明未登录
        return ApiResponseObj.error(ExceptionEnum.REQUIRED_USER_LOGIN);
    }
    boolean isAdmin = userService.isAdmin(user);
    if(isAdmin){
        //是管理员
        categoryService.updateCategory(updateCategoryReq);
        return ApiResponseObj.success();
    }else{
        //不是管理员
        return ApiResponseObj.error(ExceptionEnum.NOT_ADMIN_LOGIN);
    }
}

service及其实现类

package com.hw.springbootmall.service;

import com.hw.springbootmall.exception.MallExeception;
import com.hw.springbootmall.model.pojo.Category;
import com.hw.springbootmall.model.request.InsertCategoryReq;
import com.hw.springbootmall.model.request.UpdateCategoryReq;

/**
 * @Author: Aiver
 * @Date: 2023/02/06~~23:41
 * @Description:目录分类Service
 */
public interface CategoryService {
    void insertCategory(InsertCategoryReq insertCategoryReq) throws MallExeception;

    //void updateCategory(UpdateCategoryReq updateCategoryReq, Category category) throws MallExeception;

//    void updateCategory(Category category) throws MallExeception;

    void updateCategory(UpdateCategoryReq updateCategoryReq) throws MallExeception;
}
package com.hw.springbootmall.service.impl;

import com.hw.springbootmall.exception.ExceptionEnum;
import com.hw.springbootmall.exception.MallExeception;
import com.hw.springbootmall.model.dao.CategoryMapper;
import com.hw.springbootmall.model.pojo.Category;
import com.hw.springbootmall.model.request.InsertCategoryReq;
import com.hw.springbootmall.model.request.UpdateCategoryReq;
import com.hw.springbootmall.service.CategoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: Aiver
 * @Date: 2023/02/06~~23:41
 * @Description:目录分类Service实现类
 */
@Service
public class CategoryServiceImpl implements CategoryService {
    //查询数据库中目录表原先是否有与插入目录重名目录的数据
    //主键是id,自动生成mapper里面的的selectByPrimaryKey方法用不了
    //    手写一个selectByName的方法于mapper
//    @Autowired
//    CartMapper cartMapper;

    @Autowired
    CategoryMapper categoryMapper;

    /**
     *添加目录
     * @param insertCategoryReq
     * @throws MallExeception
     */
    @Override
    public void insertCategory(InsertCategoryReq insertCategoryReq) throws MallExeception {
        Category categoryNew = new Category();
        //将参数insertCategoryReq的4个属性赋值给categoryOld,下面是比较慢的做法
//        categoryNew.setName(insertCategoryReq.getName());
//        categoryNew.setType(insertCategoryReq.getType());
//        categoryNew.setParentId(insertCategoryReq.getParentId());
//        categoryNew.setOrderNum(insertCategoryReq.getOrderNum());
        //这里更快,男人一定要快!
        BeanUtils.copyProperties(insertCategoryReq,categoryNew);
        //写一个mapper查询数据库中目录表原先是否有要插入的目录对象
        //cartMapper.selectByPrimaryKey()不适合用,自己手写一个mapper查询:selectByName
        Category categoryOld = categoryMapper.selectByName(insertCategoryReq.getName());
        if(categoryOld != null){
//            数据库中原先就已经存在目录对象了
            throw new  MallExeception(ExceptionEnum.NAME_EXISTED);
        }else {
            int insertCount= categoryMapper.insertSelective(categoryNew);
            if(insertCount == 0){
//                throw  new MallExeception(ExceptionEnum.INSERT_FAILED);
                throw new MallExeception(ExceptionEnum.ADD_CATEGORY_FAILED);
            }
        }
    }

//    @Override
//    public void updateCategory(Category category) throws MallExeception {
//        Category categoryOld = categoryMapper.selectByName(category.getName());
//        /*代码中是根据name进行查询的,此时如果查询到结果时,存在两种可能性:
//        1.查询到的是自身的信息;
//        2.前台传入的name数据与数据库中已有的数据发生了冲突。
//        而是否冲突,是需要根据id来进行判断的,例如下面的情况:
//        1.前台传入的数据中,不需要对name值进行更改,而是更改其他属性,此时在数据库中一定可以查询到name相同的数据。
//        2.前台传入的数据中,对name进行了更改,但是这个name值在数据库中已经被其他id的数据使用了。
//        前台修改的name可能碰到拿到数据库一样的(maybe类似哈希碰撞小概率事件),而这时候就要加上id进一步确定
//        所以只是name值相同,不能确定是否是对自身进行更改,需要使用id来进一步判断。*/
//        if(categoryOld != null && !categoryOld.getId().equals(category.getId())){
//            throw new MallExeception(ExceptionEnum.NAME_EXISTED);
//        }
//        int updateCount = categoryMapper.updateByPrimaryKeySelective(category);
//        if (updateCount == 0) {
//            throw  new MallExeception(ExceptionEnum.UPDATE_FAILED);
//        }
//    }
    @Override
    public void updateCategory(UpdateCategoryReq updateCategoryReq) throws MallExeception {
        Category categoryNew = new Category();
        BeanUtils.copyProperties(updateCategoryReq,categoryNew);
        Category categoryOld = categoryMapper.selectByName(updateCategoryReq.getName());
        if(categoryOld != null && categoryOld.getId().equals(updateCategoryReq.getId())){
            //原先数据库有一样名字的目录而且原先的id和更新请求的id一样,那么就可以进行更新
            int updateCount = categoryMapper.updateByPrimaryKeySelective(categoryNew);
            if (updateCount==0) {
                throw new MallExeception(ExceptionEnum.UPDATE_FAILED);
            }
        }else{
            //其他情况就不更新了,管它呢
            throw new MallExeception(ExceptionEnum.NAME_EXISTED);
        }
    }
//    @Override
//    public void updateCategory(UpdateCategoryReq updateCategoryReq,Category category) throws MallExeception {
//        BeanUtils.copyProperties(updateCategoryReq,category);
//        Category categoryOld = categoryMapper.selectByName(updateCategoryReq.getName());
//        if(categoryOld != null && categoryOld.getName().equals(category.getName())){
//            int updateCount = categoryMapper.updateByPrimaryKeySelective(category);
//            if (updateCount==0) {
//                throw new MallExeception(ExceptionEnum.UPDATE_FAILED);
//            }
//        }else{
//            throw new MallExeception(ExceptionEnum.NAME_EXISTED);
//        }
//    }
}

一直提示未登录

https://img1.sycdn.imooc.com//climg/63e348d409bf55ec19411212.jpg

正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

1回答
好帮手慕小小 2023-02-08 15:28:11

同学你好,检查下UserController类中管理员登录接口中是否书写执行了session.setAttribute(),再检查下添加session与的key值获取时使用的key值是否是同一个。

https://img1.sycdn.imooc.com//climg/63e34f0609853f2c07410147.jpg

祝学习愉快~

  • 提问者 weixin_慕少5379513 #1

    https://img1.sycdn.imooc.com//climg/63e353ba09d0057412011054.jpg

    https://img1.sycdn.imooc.com//climg/63e353d60917128f08180527.jpg


    2023-02-08 15:48:43
  • 同学你好,目测代码是没有问题的则可能是由于缓存导致的,删除target目录,点击build--》build project,再清除idea缓存,重启项目后重新进行管理员登录、最后再测试更新接口试下。

    https://img1.sycdn.imooc.com//climg/63e3563a09e9ea9d01950088.jpg

    https://img1.sycdn.imooc.com//climg/63e3558d09458d4703260138.jpg

    https://img1.sycdn.imooc.com//climg/63e356630941afb002790373.jpg

    注:同学也可下载教辅区源码,测试下添加接口、更新接口是否可以正确运行。

    祝学习愉快~

    2023-02-08 16:04:33
  • 这个有要勾选什么吗

    https://img1.sycdn.imooc.com//climg/63e35a28097d635206600457.jpg

    2023-02-08 16:15:50
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师