瑞吉外卖(5) - 新增员工功能开发、测试

需求分析

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-1YQvyqHq-1653650050495)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527174713197.png)]

数据模型

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-J3R735M5-1653650050496)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527174922461.png)]

代码开发

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Sah3Q6pK-1653650050497)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527175107366.png)]

添加方法EmployeeController.java

    /**
     * 新增员工
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(Employee employee){
    
    
        log.info("新增员工,员工信息:{}",employee.toString());   //{} 是占位符,C语言
        return null;
    }

debug测试正常

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9BtJL2qP-1653650050497)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527181916358.png)]

完善EmployeeController.java中方法

/**
 * 新增员工
 * @param employee
 * @return
 */
@PostMapping
//@RequestBody返回json数据
public R<String> save(HttpServletRequest request,@RequestBody Employee employee){
    
    
    log.info("新增员工,员工信息:{}",employee.toString());   //{} 是占位符,C语言

    //设置初始密码,md5加密显示
    //employee.setPassword("123456");
    employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
    //status属性不用设置默认值,数据库给了默认值为1,默认开启

    employee.setCreateTime(LocalDateTime.now());    //直接给当前时间
    employee.setUpdateTime(LocalDateTime.now());

    //获得当前登录用户的id
    Long empID = (Long)request.getSession().getAttribute("employee");   //因为默认返回Object类型,所以我们下转Long

    employee.setCreateUser(empID);
    employee.setUpdateUser(empID);

    employeeService.save(employee);

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

运行测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BxAf9alr-1653650050499)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527183124007.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-h14ZEtCC-1653650050499)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527183150749.png)]

添加成功,查看数据库

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-FGND5TDL-1653650050500)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527183348172.png)]

运行异常

如果添加相同的“账号”

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-LTU3DjIG-1653650050501)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527183706433.png)]

异常捕获完善EmployeeController.java

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-otXV05Eu-1653650050502)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527183808863.png)]

        try {
    
    
            employeeService.save(employee);
        } catch (Exception e) {
    
    
            R.error("添加失败,账号已存在");
        }

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

全局异常处理

编写GlobalExceptionHandler.java

package com.taotao.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;

/**
 * create by 刘鸿涛
 * 2022/5/27 18:43
 */
@SuppressWarnings({
    
    "all"})
@ControllerAdvice(annotations = {
    
    RestController.class, Controller.class})
@ResponseBody   //封装json数据并返回
@Slf4j          //日志打印
public class GlobalExceptionHandler {
    
    

    /**
     * 异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
    
    
        log.error(ex.getMessage());
        return R.error("Unknown cause error");
    }
}

测试

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kRuzIMAt-1653650050502)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527190206851.png)]

完善GlobalExceptionHandler.java

package com.taotao.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;

/**
 * create by 刘鸿涛
 * 2022/5/27 18:43
 */
@SuppressWarnings({
    
    "all"})
@ControllerAdvice(annotations = {
    
    RestController.class, Controller.class})
@ResponseBody   //封装json数据并返回
@Slf4j          //日志打印
public class GlobalExceptionHandler {
    
    

    /**
     * 异常处理
     * @param ex
     * @return
     */
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R<String> exceptionHandler(SQLIntegrityConstraintViolationException ex){
    
    
        log.error(ex.getMessage());

        if(ex.getMessage().contains("Duplicate entry")){
    
        //当错误信息中包含Duplicate entry时
            //空格分隔
            String[] split = ex.getMessage().split(" ");
            String msg = split[2] + "已存在";
            return R.error(msg);
        }

        return R.error("Unknown cause error");
    }
}

功能测试

相同账号名

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-dwo9Rmk6-1653650050503)(%E7%91%9E%E5%90%89%E5%A4%96%E5%8D%96.assets/image-20220527191326750.png)]

猜你喜欢

转载自blog.csdn.net/qq_39123467/article/details/125009561
今日推荐