新增员工代码完善
处理username重复的异常:
@ExceptionHandler
public Result exceptionHandler (SQLIntegrityConstraintViolationException ex){
//Duplicate entry 'zhangsan' for key 'employee.idx_username'
String message = ex.getMessage();
if (message.contains("Duplicate entry")){
String[] split = message.split(" ");
String username = split[2];
String msg = username + MessageConstant.ALREADY_EXISTS;
return Result.error(msg);
}else{
return Result.error(MessageConstant.UNKNOWN_ERROR);
}
}
客户端发起的每一次请求,都是一个单独的线程。 用ThreadLocal解决 登录人和修改人id一致的问题。
sky-common中有BaseContext工具类,分别在以下位置调用其中的方法:
在jwtToken拦截器处,存id(set):
//2、校验令牌
try {
log.info("jwt校验:{}", token);
Claims claims = JwtUtil.parseJWT(jwtProperties.getAdminSecretKey(), token);
Long empId = Long.valueOf(claims.get(JwtClaimsConstant.EMP_ID).toString());
log.info("当前员工id:", empId);
BaseContext.setCurrentId(empId);
//3、通过,放行
return true;
}
接着程序会执行 controller、service,在service处把id取出来(get)。
//设置当前记录 创建人和修改人id
//employee.setCreateUser(BaseContext.getCurrentId());
//employee.setUpdateUser(BaseContext.getCurrentId());
浙公网安备 33010602011771号