2023华为OD统一考试(A+B卷)题库清单-带答案(持续更新)or2023年华为OD真题机考题库大全-带答案(持续更新)
1. 先写一个登陆记录注解(//记录:XXX时间,XXX姓名,XX系统,登录成功)
@Target(ElementType.METHOD) //作用用方法上
@Retention(RetentionPolicy.RUNTIME)//在运行时可用
public @interface LoginRecords {String value();
}
2. 写一个切面对注解进行处理(业务逻辑处理,记录登陆的信息)
@Aspect
@Component
public class logLoginInfo {//针对LoginRecords注解@Pointcut("@annotation(com.example.jobtest.aspect.annotion.LoginRecords) && execution(* *(..))")public void loginRecordsPointcut() {}@Around("loginRecordsPointcut()")public void logLoginInfo(ProceedingJoinPoint joinPoint){try {//登陆人信息LoginInfo info = (LoginInfo) joinPoint.getArgs()[0];//先获取登陆结果信息,走完登录流程ResultVo resultVo = (ResultVo) joinPoint.proceed();String nowTime = nowTime("yyyy-MM-dd HH:mm:ss");//记录:XXX时间,XXX姓名,XX系统,登录成功MethodSignature signature = (MethodSignature) joinPoint.getSignature();Method method = signature.getMethod();LoginRecords annotation = method.getAnnotation(LoginRecords.class);String systemType = annotation.value();//业务处理,插入记录表中//{........}System.out.println(nowTime + ":" + info.getName()+"在" + systemType +"系统" + resultVo.getData().toString());} catch (Throwable e) {throw new RuntimeException(e);}}public String nowTime(String regex){LocalDateTime now = LocalDateTime.now();DateTimeFormatter formatter = DateTimeFormatter.ofPattern(regex);String nowTime = now.format(formatter);return nowTime;}}
3.写一个登录的控制类,这里有asystemlogin跟bsystemlogin 用来区分注解的参数
@RestController
public class Login {@PostMapping("/asystemlogin")@LoginRecords("asystemlogin")public ResultVo checkAsystemlogin(@RequestBody LoginInfo loginInfo){ResultVo resultVo;if (loginInfo.getName().equals("axiaoyi") && loginInfo.getPwd().equals("aini")){resultVo = ResultVo.builder().code("200").data("登陆成功").build();}else {resultVo = ResultVo.builder().code("500").data("登陆失败").build();}return resultVo;}@PostMapping("/bsystemlogin")@LoginRecords("bsystemlogin")public ResultVo checkBsystemlogin(@RequestBody LoginInfo loginInfo){ResultVo resultVo;if (loginInfo.getName().equals("bxiaoyi") && loginInfo.getPwd().equals("aini")){resultVo = ResultVo.builder().code("200").data("登陆成功").build();}else {resultVo = ResultVo.builder().code("500").data("登陆失败").build();}return resultVo;}
}
4. 查看返回结果