【外卖系统】新增员工

需求分析和数据模型

  • 新增员工就是将新增页面录入的员工数据插入到emoloyee表,username字段约束是唯一的,即员工的登录账号是唯一的
  • status字段默认值为1,表示状态正常

前端界面

在这里插入图片描述报错信息:
在这里插入图片描述

代码开发分析

  • 页面发送ajax请求,将新增员工页面输入的数据以json形式提交到服务端
  • 服务端Controller接收页面提交的数据并调用Service将数据进行保存
  • Service调用Mapper操作数据库,保存数据

账号:
zhangsan
员工姓名:
张三
手机号:
13112345678
性别:
身份证号:
123456789123456789

新鲜的报错

在这里插入图片描述

代码

package com.springboot.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.springboot.reggie.common.R;
import com.springboot.reggie.entity.Employee;
import com.springboot.reggie.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.xml.crypto.dsig.DigestMethod;
import java.nio.charset.StandardCharsets;
import java.security.SecureRandom;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {
    @Autowired
    private EmployeeService employeeService;
    /**
     * 员工登录
     * @param request
     * @param employee
     * @return
     */

    @PostMapping("/login")//接受前端发来的Post请求
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee)
    {
        //1.将页面提交的代码password进行md5加密处理
        String password = employee.getPassword();
        //调用DigestUtils类中的生成MD5加密密码的类
       password =  DigestUtils.md5DigestAsHex(password.getBytes());//将哈希后的密码以十六进制字符串的形式打印出来
        //2.将页面提交的用户名username查询数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        //等值查询
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);//此处employee表中 员工id是唯一的 使用getOne()方法查出来唯一的数据
        //3.查看查询到数据的情况 如果没有查询到就返回登录失败的结果
        if(emp == null)
        {
            //登录失败 返回失败的结果 R类保存返回结果 有成功和失败两种
            return R.error("没有查询到数据,登录失败");
        }
        //4.密码比对,如果不一致返回登录失败结果
        if(!emp.getPassword().equals(password))
        {
            return R.error("密码不一致,登录失败");
        }
        //5.查看员工状态
        if(emp.getStatus() == 0)//0表示禁用
        {
            return R.error("账号已禁用");
        }
        //6.登录成功 将员工id存入Session并返回登录结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);
        //return null;


    }
    /**
     * 员工退出
     * @param request
     * @return
     */

    @PostMapping("logout")
    public R<String> logout(HttpServletRequest request)
    {
        //清理当前登录员工的id
        request.getSession().removeAttribute(("employee"));
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param employee
     * @return
     */

    @PostMapping
    public R<String> save(HttpServletRequest request,@RequestBody Employee employee)
    {
        log.info("新增员工,员工信息:{}",employee.toString());
        //设置初始密码123456 需要进行md5加密处理
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        //将实体类中的一些字段 初始化
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        //获得当前登录用户的id
        Long empId = (Long)request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);

        employeeService.save(employee);

        return R.success("新增员工成功");

    }
}

在这里插入图片描述
睡了个午觉,啥也没干,它自己正常了
在这里插入图片描述

全局异常

如果添加一条和已存在的员工信息完全相同的信息,就会报错,因为违反了唯一约束

报错信息:

==> Parameters: 1684090752251990017(Long), zhangsan(String), 张三(String), e10adc3949ba59abbe56e057f20f883e(String), 13112345678(String), 1(String), 123456789123456789(String), 2023-07-26T14:38:18.895(LocalDateTime), 2023-07-26T14:38:18.896(LocalDateTime), 1(Long), 1(Long)
Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3d91e16e]
2023-07-26 14:38:19.042 ERROR 22012 --- [nio-8060-exec-9] c.s.r.common.GlobalExceptionHandler      : Duplicate entry 'zhangsan' for key 'employee.idx_username'

代码

package com.springboot.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody //把结果封装成json数据返回
@Slf4j
public class GlobalExceptionHandler {
    //异常处理方法
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex)
    {
        log.error(ex.getMessage());
        //如果插入的数据有重复 违反了唯一约束 报错的信息是有空格将字段隔开的

        if(ex.getMessage().contains("Duplicate entry"))
        {
            //把用户名:zhangsan动态地截出来
            //Duplicate entry 'zhangsan'
            String[] split = ex.getMessage().split(" ");//根据空格进行分隔
            String msg = split[2]+"已存在";//用户名在字符串数组的第二位
            R.error(msg);
            //这里忘记加返回值了
        }
        return R.error("未知错误...");
    }
}

又发癫,未知错误
在这里插入图片描述
很好,又是上午的错误,很玄学(睡一觉就好了的那种),不去管它了
在这里插入图片描述
idea给出的报错信息是:创建的用户名不能为空。
实际代码是没有问题,解决方法:退出系统重新登录,再添加。
看视频看到后面发现是,没在if分支里面写返回值

package com.springboot.reggie.common;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.SQLIntegrityConstraintViolationException;

/**
 * 全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class, Controller.class})
@ResponseBody //把结果封装成json数据返回
@Slf4j
public class GlobalExceptionHandler {
    //异常处理方法
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex)
    {
        log.error(ex.getMessage());
        //如果插入的数据有重复 违反了唯一约束 报错的信息是有空格将字段隔开的

        if(ex.getMessage().contains("Duplicate entry"))
        {
            //把用户名:zhangsan动态地截出来
            //Duplicate entry 'zhangsan'
            String[] split = ex.getMessage().split(" ");//根据空格进行分隔
            String msg = split[2]+"已存在";//用户名在字符串数组的第二位
            return R.error(msg);
        }
        return R.error("未知错误...");
    }
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值