1讲师管理模块

一. 模块:讲师管理模块

1具体功能

​ 1.1讲师增删改查(条件查询加分页)

​ 1.2swagger

​ 1.3统一返回结果

​ 1.4统一异常处理

​ 1.5前端知识

2具体知识点

​ 2.1mp代码生成器

  • 使用现成的生成entity,mapper,service,controller层内容
  • 修改其中的数据库对应表名,以及生成地方名字就行了

​ 2.2swagger测试

  • 使用现成的swagger配置文件(一般放在common模块下,这样其他模块引用common模块就行了,别忘记引入依赖)

  • 在使用的模块启动类上加上@ComponentScan(basePackages = {"com.fao"})

    • @SpringBootApplication
      //因为springboot默认扫描同一个包下 但是我们现在要使用common下的swagger所以要这个类
      //下面这样就把com.fao下面的全扫描到了
      @ComponentScan(basePackages = {"com.fao"})
      public class EduApplication {
          public static void main(String[] args) {
              SpringApplication.run(EduApplication.class,args);
          }
      }
      
  • 登录locaohost:xxxx/swagger-ui.html

  • 接口说明和参数说明

    • 定义在类上:@Api

    • 定义在方法上:@ApiOperation

    • 定义在参数上:@ApiParam

    • @Api(tags = "讲师管理接口")
      @RestController
      @RequestMapping("/admin/vod/teacher")
      //@CrossOrigin
      public class TeacherController {
      
          @Autowired
          private TeacherService teacherService;
          
          // http://localhost:8301/admin/vod/teacher/remove/1
          //2 逻辑删除讲师
          @ApiOperation("逻辑删除讲师")
          @DeleteMapping("remove/{id}")
          public Result removeTeacher(@ApiParam(name = "id", value = "ID", required = true)
                                      @PathVariable Long id) {
              boolean isSuccess = teacherService.removeById(id);
              if(isSuccess) {
                  return Result.ok(null);
              } else {
                  return Result.fail(null);
              }
          }
      }
      

​ 2.3统一返回结果

  • 创建结果类
  • 然后返回结果类

​ 2.4统一异常处理

  • 因为不处理的话就和统一返回结果不一样了

  • 第一步:先在common包下创建异常处理类

  • 第二步:添加注解使用aop,@ControllerAdvice

  • 第三步:异常处理类编写具体针对异常处理方法,方法上添加注解@ExceptionHandler

  • //全局异常处理
       @ExceptionHandler(Exception.class)//还有特定异常处理和自定义异常处理
       @ResponseBody//返回json数据 因为这里没有RestController
       public Result error(Exception e) {
           System.out.println("全局.....");
           e.printStackTrace();
           return Result.fail(null).message("执行全局异常处理");
       }
    

​ 2.5条件分页查询讲师列表接口

  • 第一步配置mp配置类

    • @Configuration
      @MapperScan("com.fao.demo.eduservice.mapper")
      public class EduConfig {
      
          //逻辑删除插件
          @Bean
          public ISqlInjector sqlInjector() {
              return new LogicSqlInjector();
          }
      
          /**
           * 分页插件
           */
          @Bean
          public PaginationInterceptor paginationInterceptor() {
              return new PaginationInterceptor();
          }
      }
      
    • //实体类对应注解
         	@ApiModelProperty(value = "逻辑删除 1(true)已删除, 0(false)未删除")
       @TableLogic
       private Integer isDeleted;
         
       @ApiModelProperty(value = "创建时间")
       @TableField(fill = FieldFill.INSERT)
       private Date gmtCreate;
         
       @ApiModelProperty(value = "更新时间")
       @TableField(fill = FieldFill.INSERT_UPDATE)
       private Date gmtModified;
      
  • controller具体实现

    • //3 条件查询分页
        //@RequestBody(required = false)不加也行,加了就是
        //a提交参数以json格式 bflase表示条件值可以为空 c一定要和post提交一起用
        @ApiOperation("条件查询分页")
        @PostMapping("findQueryPage/{current}/{limit}")
        public Result findPage(@PathVariable long current,
                               @PathVariable long limit,
                               @RequestBody(required = false) TeacherQueryVo teacherQueryVo) {
            //创建page对象
            Page<Teacher> pageParam = new Page<>(current,limit);
            //判断teacherQueryVo对象是否为空
            if(teacherQueryVo == null) {//查询全部
                IPage<Teacher> pageModel =
                        teacherService.page(pageParam,null);
                return Result.ok(pageModel);
            } else {
                //获取条件值,
                String name = teacherQueryVo.getName();
                Integer level = teacherQueryVo.getLevel();
                String joinDateBegin = teacherQueryVo.getJoinDateBegin();
                String joinDateEnd = teacherQueryVo.getJoinDateEnd();
                //进行非空判断,条件封装
                QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
                if(!StringUtils.isEmpty(name)) {
                    wrapper.like("name",name);
                }
                if(!StringUtils.isEmpty(level)) {
                    wrapper.eq("level",level);
                }
                if(!StringUtils.isEmpty(joinDateBegin)) {
                    wrapper.ge("join_date",joinDateBegin);
                }
                if(!StringUtils.isEmpty(joinDateEnd)) {
                    wrapper.le("join_date",joinDateEnd);
                }
                //调用方法分页查询
                IPage<Teacher> pageModel = teacherService.page(pageParam, wrapper);
                //返回
                return Result.ok(pageModel);
            }
        }
      
  • 接口返回

    • 需要注意@RestController返回是json格式

    • 可以在application.properties中设置json时间格式

      • spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
        spring.jackson.time-zone=GMT+8
        
    • 分页@RequestBody(required = false) TeacherQueryVo teacherQueryVo表示

      • 提交参数以json格式
      • false表示条件值可以为空
      • 一定要和post提交一起用
    • 前端注意

      • import request from '@/utils/request'
        
        const api_name = '/admin/vod/teacher'
        
        export default {
            //讲师条件查询分页
            // current当前页  limit每页记录数 searchObj条件对象
            pageList(current,limit,searchObj) {
                return request({
                    //下面是模板字符串方式
                    url: `${api_name}/findQueryPage/${current}/${limit}`,
                    method: 'post',
                    //使用json格式传递  写法 data:searchObj
                    //使用普通格式传递  写法 params:searchObj
                    //因为后台是@RequestBody所以这里用json格式传递
                    data:searchObj
                  })
            }   
        }
        

​ 2.6前端知识

  • JS

    • ECMAScript:语句语法规范
    • BOM:浏览器对象模型
    • DOM:文档对象模型
  • es6语法

    • 定义变量let
    • 定义常量const
    • 解构赋值
    • 模板字符串``
    • 方法简写
    • 对象拓展运算符
    • 箭头函数
  • vue指令

    • v-bind/:单向绑定
    • v-model双向绑定
    • v-on/@绑定事件
    • v-if条件判断
    • v-for循环
  • vue生命周期

    • created:在页面渲染之前执行
    • mounted:在页面渲染之后执行
  • vue组件

    • 定义组件

      • var app = new Vue({
            el: '#app',
            // 定义局部组件,这里可以定义多个局部组件
            components: {
                //组件的名字
                'Navbar': {
                    //组件的内容
                    template: '<ul><li>首页</li><li>学员管理</li></ul>'
                }
            }
        })
        
    • 使用组件

      • <div id="app">
            <Navbar></Navbar>
        </div>
        
  • vue路由

    • <el-button class="btn-add" @click="add()" style="margin-left: 10px;">添加</el-button>
      
    • //跳转到添加表单页面
          add() {
            this.$router.push({path:'/vod/teacher/create'})
          }
      
  • axios

    • var app = new Vue({
          el: '#app',
          data: {
              memberList: []//数组
          },
          created() {
              this.getList()
          },
          methods: {
              getList(id) {
                  //vm = this
                  axios.get('data.json')
                  .then(response => {
                      console.log(response)
                      this.memberList = response.data.data.items
                  })
                  .catch(error => {
                      console.log(error)
                  })
              }
          }
      })
      
  • nodejs

    • nodejs是js运行环境,类似于java里面的jdk,不需要浏览器通过nodejs直接运行js文件
    • nodejs作为服务端使用
  • npm

    • 是nodejs包管理根据,类似于Maven
  • babel

    • 是一个广泛使用的转码器,可以将ES6转为ES5,从而在现有环境下运行执行

2.7maven加载xml问题

  • 出现invalid bound statement报错

    • 先检查xml是否名字有错
    • 第二就是maven加载机制
  • maven加载机制

    • maven默认情况下,在src-main-java目录下面,只会加载java类型文件,其他类型不会加载
  • 解决

    • 1直接复制

    • 2把xml放到resources目录下

    • 3修改pom.xml和application

      • <build>
              <plugins>
                  <plugin>
                      <groupId>org.springframework.boot</groupId>
                      <artifactId>spring-boot-maven-plugin</artifactId>
                  </plugin>
              </plugins>
              <resources>
                  <resource>
                      <directory>src/main/java</directory>
                      <includes>
                          <include>**/*.yml</include>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>false</filtering>
                  </resource>
                  <resource>
                      <directory>src/main/resources</directory>
                      <includes> <include>**/*.yml</include>
                          <include>**/*.properties</include>
                          <include>**/*.xml</include>
                      </includes>
                      <filtering>false</filtering>
                  </resource>
              </resources>
          </build>
        
      • mybatis-plus.mapper-locations=classpath:com/fao/ggkt/vod/mapper/xml/*.xml
        
posted @ 2022-07-14 15:41  fao99  阅读(1374)  评论(0)    收藏  举报