员工管理模块:
员工登录:
从前端拿到数据,md5加密后使用mybatis获取到数据库用户密码进行对比,登录成功后使用Jwt生成Token放入Vo对象中返回前端。
log.info("执行 员工登录 接口:{}", employeeLoginDTO); Employee employee = employeeService.login(employeeLoginDTO); //登录成功后,生成jwt令牌 Map<String, Object> claims = new HashMap<>(); claims.put(JwtClaimsConstant.EMP_ID, employee.getId()); String token = JwtUtil.createJWT( jwtProperties.getAdminSecretKey(), jwtProperties.getAdminTtl(), claims); EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder() .id(employee.getId()) .userName(employee.getUsername()) .name(employee.getName()) .token(token) .build(); return Result.success(employeeLoginVO);
员工分页:
mybatis整合pageHelper,yml配置:
pagehelper: helper-dialect: mysql reasonable: true support-methods-arguments: true page-size-zero: true params: count=countSql
使用pegeHelper:
PageHelper.startPage();
返回Page对象到前端.
其他都是基础的增删改查。
基础的自动填充功能,不需要自己实现:
createTime,createUser,updateTime,updateUser使用自动填充
首先: 创建操作的枚举类OperationType 有两种,UPDATE,INSERT
接着: 创建注解annotation AutoFill
最后: 运用aop,创建切面、切点继而运用反射代理 :
@Aspect @Component @Slf4j public class AutoFillAspect { @Pointcut("execution(* com.sky.mapper.*.*(..)) && @annotation(com.sky.annotation.AutoFill)") public void autoFillPointCut(){} @Before("autoFillPointCut()") public void autoFill(JoinPoint joinPoint){ log.info("开始进行公共字段自动填充....."); MethodSignature signature = (MethodSignature) joinPoint.getSignature(); AutoFill autoFill = signature.getMethod().getAnnotation(AutoFill.class); OperationType type = autoFill.value(); Object[] args = joinPoint.getArgs(); if (args == null || args.length ==0 ){ return; } Object entity = args[0]; LocalDateTime now = LocalDateTime.now(); Long currentId = BaseContext.getCurrentId(); if (type == OperationType.INSERT){ try { Method setCreateTime = entity.getClass().getDeclaredMethod("setCreateTime", LocalDateTime.class); Method setCreateUser = entity.getClass().getDeclaredMethod("setCreateUser", Long.class); Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class); Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class); setCreateTime.invoke(entity,now); setUpdateTime.invoke(entity,now); setCreateUser.invoke(entity,currentId); setUpdateUser.invoke(entity,currentId); } catch (Exception e) { e.printStackTrace(); } }else if (type == OperationType.UPDATE){ try { Method setUpdateTime = entity.getClass().getDeclaredMethod("setUpdateTime", LocalDateTime.class); Method setUpdateUser = entity.getClass().getDeclaredMethod("setUpdateUser", Long.class); setUpdateTime.invoke(entity,now); setUpdateUser.invoke(entity,currentId); } catch (Exception e) { e.printStackTrace(); } } }