文章目录
需求分析
数据模型
代码开发
添加方法EmployeeController.java
/**
* 新增员工
* @param employee
* @return
*/
@PostMapping
public R<String> save(Employee employee){
log.info("新增员工,员工信息:{}",employee.toString()); //{} 是占位符,C语言
return null;
}
debug测试正常
完善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("新增员工成功");
}
运行测试
添加成功,查看数据库
运行异常
如果添加相同的“账号”
异常捕获完善EmployeeController.java
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");
}
}
测试
完善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");
}
}
功能测试
相同账号名