0702~全局异常,课程服务搭建,树形结构掌握

全局异常,在每个地方都要(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()));
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值