IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> JavaScript知识库 -> vue-element-admin-master 在线教育 -【2】后端讲师管理 -> 正文阅读

[JavaScript知识库]vue-element-admin-master 在线教育 -【2】后端讲师管理

哈喽!我是话里人,相信伙伴们通过上篇Blog的步骤,后端环境搭建已经完成,话不多说,那咱们书接上回~

介绍

  • 在线教育,是一个B2C模式的职业技能在线教育系统,分为前台用户系统和后台运营平台。
    • B2C是指电子商务的一种模式,也是直接面向消费者销售产品和服务商业的零售模式。
    • Business-to-Consumer :企业对客户

4.实现讲师列表接口

4.1 pojo

  • 在zx-domain中创建实体类EduTeacher
package com.czxy.zx.domain;

import com.baomidou.mybatisplus.annotation.*;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.util.Date;

/**
 * @author txt
 * @email tantintong9968@163.com
 */
@Data//lombok注解
@ApiModel(value = "EduTeacher对象",description = "讲师")//swagger注解
public class EduTeacher {

    @TableId(value = "id",type = IdType.AUTO)//mybatis-plus的注解
    @ApiModelProperty(value = "讲师ID")//swagger注解
    private String id;
    @ApiModelProperty(value = "讲师姓名")
    private String name;
    @ApiModelProperty(value = "讲师资历,一句话说明讲师")
    private String intro;
    @ApiModelProperty(value = "讲师简介")
    private String career;
    @ApiModelProperty(value = "头衔 1高级讲师 2首席讲师")
    private Integer level;
    @ApiModelProperty(value = "讲师头像")
    private String avatar;
    @ApiModelProperty(value = "讲师排序")
    private Integer sort;
    @ApiModelProperty(value = "逻辑删除 1已删除, 0未删除")
    @TableField(value = "is_deleted",fill = FieldFill.INSERT)
    @TableLogic//逻辑删除
    private Integer isDeleted;
    @ApiModelProperty(value = "创建时间")
    @TableField(value = "gmt_create",fill = FieldFill.INSERT)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date gmtCreate;
    @ApiModelProperty(value = "更新时间")
    @TableField(value = "gmt_modified",fill = FieldFill.INSERT_UPDATE)
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date gmtModified;

}

4.2 mapper

package com.czxy.zx.teacher.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.czxy.zx.domain.EduTeacher;
import org.apache.ibatis.annotations.Mapper;

/**
 * @author txt
 * @email tantintong9968@163.com
 */
@Mapper
public interface EduTeacherMapper extends BaseMapper<EduTeacher> {
}

4.3 service

  • 接口
package com.czxy.zx.teacher.service;

import com.baomidou.mybatisplus.extension.service.IService;
import com.czxy.zx.domain.EduTeacher;

/**
 * @author txt
 * @email tantintong9968@163.com
 */
public interface EduTeacherService extends IService<EduTeacher> {
}

  • 实现类
package com.czxy.zx.teacher.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.zx.domain.EduTeacher;
import com.czxy.zx.teacher.mapper.EduTeacherMapper;
import com.czxy.zx.teacher.service.EduTeacherService;
import org.springframework.stereotype.Service;

/**
 * @author txt
 * @email tantintong9968@163.com
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
}

4.4 Controller

TeacherController

package com.czxy.zx.teacher.controller;

import com.czxy.zx.domain.EduTeacher;
import com.czxy.zx.teacher.service.EduTeacherService;
import com.czxy.zx.vo.BaseResult;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
* @author txt
* @email tantintong9968@163.com
*/
@RestController
@RequestMapping("/teacher")
public class EduTeacherController {
   
   @Resource
   private EduTeacherService eduTeacherService;

   @GetMapping
   private BaseResult<List<EduTeacher>> getTeacherList(){
       List<EduTeacher> list = eduTeacherService.list(null);
       return BaseResult.ok("查询成功", list);
   }
}

4.5 拷贝配置类

4.5.1 拷贝配置类:config-MyBatisPlusConfig

package com.czxy.zx.teacher.config;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

// MyBatisPlus 分页插件
@Component
public class MyBatisPlusConfig {
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor(){
        MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
        mybatisPlusInterceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return mybatisPlusInterceptor;
    }
}

4.5.2 拷贝配置类:config-MyBatisPlusConfig

package com.czxy.zx.teacher.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * Swagger2 配置类,
 * 访问路径:swagger-ui.html
 * 自动注册:
 *     位置:resources/META-INF/spring.factories
 *     内容:
 *        org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
 *          com.czxy.changgou4.config.Swagger2Configuration
 */
@Configuration
@EnableSwagger2
public class Swagger2ConfigurationV3 {

    @Bean
    public Docket createRestApi() {
        // 1 确定文档Swagger版本
        Docket docket = new Docket(DocumentationType.SWAGGER_2);
        // 2 设置 api基本信息
        docket.apiInfo(apiInfo());
        // 3 设置自定义加载路径
        docket = docket.select()
                .apis(RequestHandlerSelectors.basePackage("com.czxy"))
                .paths(PathSelectors.any())
                .build();
        //4 设置权限
        docket.securitySchemes(securitySchemes());
        docket.securityContexts(securityContexts());

        return docket;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("API")
                .description("基于swagger接口文档")
                .contact(new Contact("梁桐","http://www.javaliang.com","liangtong@itcast.cn"))
                .version("1.0")
                .build();
    }

    private List<ApiKey> securitySchemes() {
        List<ApiKey> list = new ArrayList<>();
        // name 为参数名  keyname是页面传值显示的 keyname, name在swagger鉴权中使用
        list.add(new ApiKey("Authorization", "Authorization", "header"));
        return list;
    }

    private List<SecurityContext> securityContexts() {
        List<SecurityContext> list = new ArrayList<>();
        list.add(SecurityContext.builder()
                .securityReferences(defaultAuth())
                .forPaths(PathSelectors.regex("^(?!auth).*$"))
                .build());
        return list;
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[1];
        authorizationScopes[0] = authorizationScope;
        List<SecurityReference> list = new ArrayList();
        list.add(new SecurityReference("Authorization", authorizationScopes));
        return list;
    }

}

4.6 处理Swagger接口

  • 修改controller
package com.czxy.zx.teacher.controller;

import com.czxy.zx.domain.EduTeacher;
import com.czxy.zx.teacher.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author txt
 * @email tantintong9968@163.com
 */
@RestController
@RequestMapping("/teacher")
@Api(tags = "老师接口", description = "老师接口描述,完成增删改查操作")
public class EduTeacherController {

    @Resource
    private EduTeacherService eduTeacherService;

    @ApiOperation(value = "查询所有老师")
    @GetMapping
    private BaseResult<List<EduTeacher>> getTeacherList(){
        List<EduTeacher> list = eduTeacherService.list(null);
        return BaseResult.ok("查询成功", list);
    }
}

4.7 测试

http://localhost:9000/swagger-ui.html
http://localhost:10010/teacher-service/swagger-ui.html

5.讲师逻辑删除

5.1 在Controller中添加删除方法

    @ApiOperation(value = "根据ID删除讲师")
    @ApiImplicitParams(value = {
//            @ApiImplicitParam(name = "id", value = "讲师ID", required = true,paramType = "path")//方式一
    })
    @DeleteMapping("/{id}")
    public boolean removeById(
            @ApiParam(name = "id", value = "讲师ID", required = true)//方式二
            @PathVariable("id") String id){
        return eduTeacherService.removeById(id);
    }

5.2 在EduTeacher.java文件中

@ApiModelProperty(value = "逻辑删除 1(true)已删除, 0(false)未删除")
@TableLogic
@TableField(fill = FieldFill.INSERT, value = "is_deleted")
private Boolean isDeleted;

5.3 完善:配置Handler

  • 创建 com.czxy.zx.teacher.handler.TeacherMetaObjectHandler.java

package com.czxy.zx.teacher.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

/**
 * @author txt
 * @email tantintong9968@163.com
 */
@Component
public class TeacherMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 添加时,填充的内容
        // this.setFieldValByName("字段名", 字段值 , metaObject);
        this.setFieldValByName("isDeleted",0 , metaObject);

    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 修改时,填充的内容

    }
}

5.4 完善:配置application.yaml文件

  • 配置指定删除和不删除的状态

mybatis-plus:
  configuration:
    log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
  global-config:
    db-config:
      logic-delete-value: 1
      logic-not-delete-value: 0

5.5 讲师逻辑删除测试

ok

6.分页和条件查询

6.1 基本查询

6.1.1 创建 vo

package com.czxy.zx.teacher.vo;

import lombok.Data;

/**
 * @author txt
 * @email tantintong9968@163.com
 */
@Data
public class TeacherVo {

}

6.1.2 controller

@ApiOperation(value = "所有老师:条件 + 分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "size",value = "每页个数",required = true,paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "current",value = "当前页",required = true,paramType = "path", dataType = "int")
    })
    @PostMapping("/condition/{size}/{current}")
    public BaseResult<Page<EduTeacher>> condition(
            @PathVariable("size") Integer size,
            @PathVariable("current") Integer current,
            @RequestBody TeacherVo teacherVo
            ){
        Page<EduTeacher> page = eduTeacherService.condition(size,current,teacherVo);

        return BaseResult.ok("查询成功", page);
    }

6.1.3 service

  • 接口

    package com.czxy.zx.teacher.service;
    
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.IService;
    import com.czxy.zx.domain.EduTeacher;
    import com.czxy.zx.teacher.vo.TeacherVo;
    
    /**
     * @author txt
     * @email tantintong9968@163.com
     */
    public interface EduTeacherService extends IService<EduTeacher> {
        /**
         * 条件分页查询
         * @param size
         * @param current
         * @param teacherVo
         * @return
         */
        Page<EduTeacher> condition(Integer size, Integer current, TeacherVo teacherVo);
    }
    
    
  • 实现类

    package com.czxy.zx.teacher.service.impl;
    
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.czxy.zx.domain.EduTeacher;
    import com.czxy.zx.teacher.mapper.EduTeacherMapper;
    import com.czxy.zx.teacher.service.EduTeacherService;
    import com.czxy.zx.teacher.vo.TeacherVo;
    import org.springframework.stereotype.Service;
    
    
     /**
       * @author txt
       * @email tantintong9968@163.com
       */
    @Service
    public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
        @Override
        public Page<EduTeacher> condition(Integer size, Integer current, TeacherVo teacherVo) {
            return null;
        }
    }
    
    

6.2 分页

  • 修改service,完成分页查询
package com.czxy.zx.teacher.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czxy.zx.domain.EduTeacher;
import com.czxy.zx.teacher.mapper.EduTeacherMapper;
import com.czxy.zx.teacher.service.EduTeacherService;
import com.czxy.zx.teacher.vo.TeacherVo;
import org.springframework.stereotype.Service;

/**
 * @author txt
 * @email tantintong9968@163.com
 */
@Service
public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
    @Override
    public Page<EduTeacher> condition(Integer size, Integer current, TeacherVo teacherVo) {
        //1 条件查询
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        //2 分页条件
        Page<EduTeacher> page = new Page<>(current,size);

        //3 查询
        this.baseMapper.selectPage(page, queryWrapper);

        //4 关联查询

        //5 返回
        return page;
    }
}

6.3 条件查询

  • 需求:

    • 根据讲师姓名模糊查询
    • 根据讲师头衔精准查询
    • 根据时间范围查询(开始时间、结束时间)
  • 完善vo

    package com.czxy.zx.teacher.vo;
    
    import io.swagger.annotations.ApiModel;
    import io.swagger.annotations.ApiModelProperty;
    import lombok.Data;
    
     /**
       * @author txt
       * @email tantintong9968@163.com
       */
    @Data
    @ApiModel(value = "封装了Teacher条件查询的参数")
    public class TeacherVo {
        @ApiModelProperty(value = "讲师姓名,模糊查询")
        private String name;
        @ApiModelProperty(value = "讲师头衔:1 高级讲师  2 首席讲师")
        private String level;
        @ApiModelProperty(value = "查询开始时间",example = "2020-02-01 10:12:30")
        private String beginDate;
        @ApiModelProperty(value = "查询截止时间",example = "2020-02-01 10:12:30")
        private String endDate;
    }
    
    
  • 完善service

    package com.czxy.zx.teacher.service.impl;
    
    import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
    import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
    import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
    import com.czxy.zx.domain.EduTeacher;
    import com.czxy.zx.teacher.mapper.EduTeacherMapper;
    import com.czxy.zx.teacher.service.EduTeacherService;
    import com.czxy.zx.teacher.vo.TeacherVo;
    import org.apache.commons.lang.StringUtils;
    import org.springframework.stereotype.Service;
    
    /**
     * @author txt
     * @email tantintong9968@163.com
     */
    @Service
    public class EduTeacherServiceImpl extends ServiceImpl<EduTeacherMapper, EduTeacher> implements EduTeacherService {
        @Override
        public Page<EduTeacher> condition(Integer size, Integer current, TeacherVo teacherVo) {
            //1 条件查询
            QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();
            queryWrapper.like(StringUtils.isNotBlank(teacherVo.getName()), "name", teacherVo.getName());
            queryWrapper.eq(StringUtils.isNotBlank(teacherVo.getLevel()), "level", teacherVo.getLevel());
            queryWrapper.ge(StringUtils.isNotBlank(teacherVo.getBeginDate()), "gmt_create", teacherVo.getBeginDate()+ " 00:00:00" );
            queryWrapper.le(StringUtils.isNotBlank(teacherVo.getEndDate()), "gmt_create", teacherVo.getEndDate() + " 23:59:59");
    
            //2 分页条件
            Page<EduTeacher> page = new Page<>(current,size);
    
            //3 查询
            this.baseMapper.selectPage(page, queryWrapper);
    
            //4 关联查询
    
            //5 返回
            return page;
        }
    }
    
    

6.4 测试

ok

7.讲师新增和修改

7.1 新增

在Controller中编写代码

    @ApiOperation(value = "新增讲师")
    @PostMapping
    public BaseResult save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher){

        boolean save = eduTeacherService.save(teacher);
        if(save) {
            return BaseResult.ok("添加成功");
        }
        return BaseResult.error("添加失败");
    }
  • 测试数据
{
  "avatar": "https://czxy-lt.oss-cn-shanghai.aliyuncs.com/avatar/449972ccd28a41d6a8317c97a9c2319e.png",
  "career": "讲师简介",
  "gmtCreate": "2022-06-27 08:16:32",
  "gmtModified": "2022-06-27 08:16:32",
  "intro": "讲师资历",
  "level": 1,
  "name": "张三",
  "sort": 0
}

7.2 根据id查询

    @ApiOperation(value = "根据ID查询讲师")
    @GetMapping("{id}")
    public BaseResult getById(
            @ApiParam(name = "id", value = "讲师ID", required = true)
            @PathVariable String id){

        EduTeacher teacher = eduTeacherService.getById(id);
        return BaseResult.ok("查询成功", teacher);
    }

7.3 根据id查询 + 通过id修改

  • 通过id查询

    /**
     * 通过id查询详情
     * @param teacherId
     * @return
     */
    @GetMapping("/{teacherId}")
    public BaseResult<EduTeacher> findById(@PathVariable("teacherId") Integer teacherId ) {
        // 查询
        EduTeacher eduTeacher = eduTeacherService.getById(teacherId);
        if(eduTeacher != null) {
            // 返回
            return BaseResult.ok("查询成功", eduTeacher);
        }
        return BaseResult.error("查询失败");
    }
    
  • 通过id修改

        @ApiOperation(value = "根据ID修改讲师")
        @PutMapping
        public BaseResult updateById(
                @ApiParam(name = "teacher", value = "讲师对象", required = true)
                @RequestBody EduTeacher teacher){
    
            boolean update = eduTeacherService.updateById(teacher);
            if(update) {
                return BaseResult.ok("更新成功");
            }
            return BaseResult.error("更新失败");
        }
    

7.4 批量删除

	/**
     * 批量删除
     * @return
     */
    @PostMapping("/batchDelete")
    public BaseResult batchDelete(@RequestBody List<Integer> ids) {
        boolean result = eduTeacherService.removeByIds(ids);
        if(result) {
            return BaseResult.ok("批量删除成功");
        }
        return  BaseResult.error("批量删除失败");
    }

7.5 修改处理类:自动填充

package com.czxy.zx.teacher.handler;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.util.Date;

  /**
   * @author txt
   * @email tantintong9968@163.com
   */
@Component
public class TeacherMetaObjectHandler implements MetaObjectHandler {
    @Override
    public void insertFill(MetaObject metaObject) {
        // 给javabean默认值
        // 1 逻辑删除的默认值
        this.setFieldValByName("isDeleted", 0 , metaObject);
        // 2 创建、修改时间
        this.setFieldValByName("gmtCreate", new Date(), metaObject);
        this.setFieldValByName("gmtModified", new Date(), metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        // 2 修改时间
        this.setFieldValByName("gmtModified", new Date(), metaObject);
    }
}

8.统一异常处理

8.1 测试系统对错误的响应

  • 在更新操作中,应该输入{}填写更新数据,如果填写成[],程序将抛出异常。

8.2 全局异常处理

  • 我们想让异常结果也统一,并且在集中的地方处理系统的异常信息,那么需要统一异常处理。

  • spring mvc 提供 @ControllerAdvice就可以完成此需求。

  • @ControllerAdvice 是对Controller进行增强的注解,主要作用有三个:

    1. 全局异常处理【重点】

    2. 全局数据绑定

    3. 全局数据预处理

  • zx-common中创建统一异常处理类:

package com.czxy.zx.exception;

import com.czxy.zx.vo.BaseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

  /**
   * @author txt
   * @email tantintong9968@163.com
   */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)//拦截什么异常
    @ResponseBody
    public BaseResult error(Exception e){
        e.printStackTrace();
        return BaseResult.error("系统错误");
    }
}

  • 注意:启动类的位置

    • 启动类必须放在 com.czxy.zx包下面才可以加载到com.czxy.zx.exception包下面的内容
    • 如果放在com.czxy.zx.teacher下面,将无法加载到com.czxy.zx.exception包下面的内容

  • 测试,返回统一错误结果

8.3 特殊异常配置

如果是除数为0的异常;那么需要如下配置,精确匹配异常:

package com.czxy.zx.exception;

import com.czxy.zx.vo.BaseResult;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

  /**
   * @author txt
   * @email tantintong9968@163.com
   */
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)//拦截什么异常
    @ResponseBody
    public BaseResult error(Exception e){
        e.printStackTrace();
        return BaseResult.error("系统错误");
    }

    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody
    public BaseResult error(ArithmeticException e){
        e.printStackTrace();
        return BaseResult.error("除数不能为0");
    }
}

8.4 自定义异常

8.4.1 EduException通用异常类

  • 在zx-common中创建Exception的异常类

package com.czxy.zx.exception;

import io.swagger.annotations.ApiModel;

  /**
   * @author txt
   * @email tantintong9968@163.com
   */
@ApiModel(value = "自定义异常")
public class EduException extends RuntimeException {

    public EduException(String message) {
        super(message);
    }

    public EduException(String message, Throwable cause) {
        super(message, cause);
    }

    public EduException(Throwable cause) {
        super(cause);
    }

    protected EduException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
        super(message, cause, enableSuppression, writableStackTrace);
    }
}

8.4.2 创建捕获自定义异常类

  • 在 GlobalExceptionHandler类中添加自定义处理方法
    @ExceptionHandler(EduException.class)
    @ResponseBody
    public BaseResult error(EduException e){
        e.printStackTrace();
        if(e.getMessage() == null) {
            return BaseResult.error("空指针异常");
        }
        return BaseResult.error(e.getMessage());
    }
  • 测试:在业务中需要的位置抛出EduException,举例子在查询列表中出错:
    @ApiOperation(value = "查询所有老师")
    @GetMapping
    private BaseResult<List<EduTeacher>> getTeacherList(){
        List<EduTeacher> list = eduTeacherService.list(null);
        //测试自定义异常
        if(list.size() > 0) {
            throw new EduException("数据异常");
        }
        return BaseResult.ok("查询成功", list);
    }

8.4.3 异常工具类

  • 编写 ExceptionUtils 类,方便异常的抛出

    package com.czxy.zx.exception;
    
    /**
     * @author txt
     * @email tantintong9968@163.com
     */
    public class ExceptionUtils {
        /**
         * 抛出异常
         * @param message
         */
        public static void cast(String message) {
            throw new EduException(message);
        }
    }
    
    
  • 测试:

        @ApiOperation(value = "查询所有老师")
        @GetMapping
        private BaseResult<List<EduTeacher>> getTeacherList(){
            List<EduTeacher> list = eduTeacherService.list(null);
            //测试自定义异常
            if(list.size() > 0) {
                //throw new EduException("数据异常");
                ExceptionUtils.cast("数据异常");
            }
            return BaseResult.ok("查询成功", list);
        }
    

9.附:创建MP代码生成器

        <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-generator</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.velocity</groupId>
            <artifactId>velocity-engine-core</artifactId>
            <version>2.0</version>
        </dependency>

在test/java目录下创建包com.zx.edu,创建代码生成器:CodeGenerator.java

package com.zx;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import org.junit.Test;

public class CodeGenerator {
    @Test
    public void run() {
        // 1、创建代码生成器
        AutoGenerator mpg = new AutoGenerator();
        // 2、全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        //有的电脑系统,如果代码发现生成有问题,projectPath直接写成项目名称
        //gc.setOutputDir("edu_eduservice" + "/src/main/java");
        gc.setOutputDir(projectPath + "/src/main/java");
        gc.setAuthor("lt");
        gc.setOpen(false); //生成后是否打开资源管理器

        gc.setFileOverride(false); //重新生成时文件是否覆盖
        //IUserServcie
        gc.setServiceName("%sService");    //去掉Service接口的首字母I
        gc.setIdType(IdType.ID_WORKER_STR); //主键策略
        gc.setDateType(DateType.ONLY_DATE);//定义生成的实体类中日期类型
        gc.setSwagger2(true);//开启Swagger2模式
        mpg.setGlobalConfig(gc);
        // 3、数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://localhost:3306/zx_edu_teacher");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("1234");
        dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);

        // 4、包配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("teacher"); //模块名
        pc.setParent("com.zx");
        pc.setController("controller");
        pc.setEntity("entity");
        pc.setService("service");
        pc.setMapper("mapper");
        mpg.setPackageInfo(pc);
        // 5、策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("edu_teacher");//表名称
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
        strategy.setTablePrefix(pc.getModuleName() + "_"); //生成实体时去掉表前缀

        strategy.setColumnNaming(NamingStrategy.underline_to_camel);//数据库表字段映射到实体的命名策略
        strategy.setEntityLombokModel(true); // lombok 模型 @Accessors(chain = true) setter链式操作
        strategy.setRestControllerStyle(true); //restful api风格控制器
        strategy.setControllerMappingHyphenStyle(true); //url中驼峰转连字符
        mpg.setStrategy(strategy);
        // 6、执行
        mpg.execute();
    }
}

执行代码生成器方法

说明:

XxxServiceImpl 继承了 ServiceImpl 类,并且MP为我们注入了 XxxMapper

这样可以使用 service 层默认为我们提供的很多方法,当然也可以调用我们自己在 dao 层编写的方法。

end


ok,以上就是后端讲师管理的全部内容了,
如果各位看官觉得满意的话,求各位点赞收藏转发
您的支持就是话里人前进的巨大动力~

我是话里人,我们下期见~

  JavaScript知识库 最新文章
ES6的相关知识点
react 函数式组件 & react其他一些总结
Vue基础超详细
前端JS也可以连点成线(Vue中运用 AntVG6)
Vue事件处理的基本使用
Vue后台项目的记录 (一)
前后端分离vue跨域,devServer配置proxy代理
TypeScript
初识vuex
vue项目安装包指令收集
上一篇文章      下一篇文章      查看所有文章
加:2022-10-08 20:32:10  更:2022-10-08 20:34:02 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/17 17:38:11-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码