我测试更新目录,结果一直提示未登录。我登陆了,然后添加目录也成功,数据库也有。但是就是更新接口这里一直提示未登录。找了好久也没发现,能帮我看看吗
来源:5-7 更新目录接口开发
weixin_慕少5379513
2023-02-08 15:02:12
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); // } // } }
一直提示未登录
1回答
好帮手慕小小
2023-02-08
同学你好,检查下UserController类中管理员登录接口中是否书写执行了session.setAttribute(),再检查下添加session与的key值获取时使用的key值是否是同一个。
祝学习愉快~
相似问题
回答 2
回答 1