全局异常,在每个地方都要(try~catch)异常处理,会有很多重复的操作,会显得很麻烦;
全局异常+自定义异常使用步骤:
1.自定义异常,创建一个类继承RuntimeException,添加构造器;
/** 自定义异常类 * @author liuliang * @date 2022-07-02 14:58 */ public class MyException extends RuntimeException{ public MyException(String message) { super(message); } }
2.全局异常处理,类上添加注解@RestControllerAdvice,方法上添加注解@ExceptionHander(Exceprion.class);
/** 全局异常类 */ @RestControllerAdvice public class GlobalExceptionHandler { //捕获自定义异常 @ExceptionHandler(MyException.class) public JSONResult MyException(MyException e) { return JSONResult.error(e.getMessage()); } //捕获空指针的异常 @ExceptionHandler(NullPointerException.class) public JSONResult Exception(NullPointerException e){ return JSONResult.error("空指针异常"); } //捕获其他的全部的异常 @ExceptionHandler(Exception.class) public JSONResult Exception(Exception e){ return JSONResult.error("系统出错,请稍后再试---"); }
课程微服务搭建,树形结构重点掌握;
思路步骤:
1.通过查询sql查找出所有课程数据;
2.通过所有的课程信息找出父节点(pid=0)的数据;
3.找出自己所有的父节点数据;
@Override public JSONResult treeData() { //1.通过一条sql查出所有课程信息; List<CourseType> allCourseTypeList = courseTypeMapper.selectList(null); //用来装一级节点 List<CourseType> courseList = new ArrayList<>(); //2.通过所有课程信息,找出所有的父节点(pid=0的数据) for(int i=0; i<allCourseTypeList.size(); i++){ CourseType courseType = allCourseTypeList.get(i); //如果pid==0 则为父节点 if(courseType.getPid()==0){ courseList.add(courseType); }else{ //如果pid!=0 则为子节点 for(int j=0; j<allCourseTypeList.size(); j++){ CourseType chilrdenType = allCourseTypeList.get(j); if(chilrdenType.getId().longValue()==courseType.getPid().longValue()){ chilrdenType.getChildren().add(courseType); break; } } } } return JSONResult.success(courseList); }
mybatisplus的高级查询;需要用 new Entitywrapper();封装 查询对象;
示例:
@PostMapping(value = "/pagelist") public JSONResult pageList(@RequestBody CourseTypeQuery query) { Page<CourseType> page = new Page<CourseType>(query.getPage(),query.getRows()); EntityWrapper entityWrapper = new EntityWrapper(); entityWrapper.eq("pid",query.getPid()).and().like("name",query.getKeyword()); page = courseTypeService.selectPage(page,entityWrapper); return JSONResult.success(new PageList<CourseType>(page.getTotal(), page.getRecords())); }