1. Ajax:Asynchronous JavaScript and XML(异步的 JavaScript 和 XML),Ajax 在浏览器与 Web 服务器之间使用异步数据传输(HTTP 请求),这样就可使网页从服务器请求少量的信息,而不是整个页面。
**2.**JSON:JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。
说明:
其中 id 必为主键,类型为 bigint unsigned、单表时自增、步长为 1。
(如果使用分库分表集群部署,则id类型为verchar,非自增,业务中使用分布式id生成器)
gmt_create, gmt_modified 的类型均为 datetime 类型,前者现在时表示主动创建,后者过去分词表示被 动更新。
单表行数超过 500 万行或者单表容量超过 2GB,才推荐进行分库分表。 说明:如果预计三年后的数据量根本达不到这个级别,请不要在创建表时就分库分表。
表达是与否概念的字段,必须使用 is_xxx 的方式命名,数据类型是 unsigned tinyint (1 表示是,0 表示否)。
说明:任何字段如果为非负数,必须是 unsigned。
注意:POJO 类中的任何布尔类型的变量,都不要加 is 前缀。数据库表示是与否的值,使用 tinyint 类型,坚持 is_xxx 的 命名方式是为了明确其取值含义与取值范围。
正例:表达逻辑删除的字段名 is_deleted,1 表示删除,0 表示未删除。
小数类型为 decimal,禁止使用 float 和 double。 说明:float 和 double 在存储的时候,存在精度损失的问题,很可能在值的比较时,得到不 正确的结果。如果存储的数据范围超过 decimal 的范围,建议将数据拆成整数和小数分开存储。
如果存储的字符串长度几乎相等,使用 char 定长字符串类型。
varchar 是可变长字符串,不预先分配存储空间,长度不要超过 5000,如果存储长度大于此值,定义字段类型为 text,独立出来一张表,用主键来对应,避免影响其它字段索 引效率。
唯一索引名为 uk_字段名;普通索引名则为 idx_字段名。
说明:uk_ 即 unique key;idx_ 即 index 的简称
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0modelVersion>
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.2.1.RELEASEversion>
<relativePath/>
parent>
<groupId>com.jqgroupId>
<artifactId>ggkt_parentartifactId>
<version>0.0.1-SNAPSHOTversion>
<name>ggkt_parentname>
<description>Demo project for Spring Bootdescription>
<properties>
<skipTests>trueskipTests>
<java.version>1.8java.version>
<cloud.version>Hoxton.RELEASEcloud.version>
<alibaba.version>2.2.0.RELEASEalibaba.version>
<mybatis-plus.version>3.4.1mybatis-plus.version>
<mysql.version>5.1.46mysql.version>
<swagger.version>2.9.2swagger.version>
<jwt.version>0.7.0jwt.version>
<fastjson.version>1.2.29fastjson.version>
<httpclient.version>4.5.1httpclient.version>
<easyexcel.version>2.2.0-beta2easyexcel.version>
<aliyun.version>4.5.14aliyun.version>
<jodatime.version>2.10.1jodatime.version>
<jwt.version>0.7.0jwt.version>
<xxl-job.version>2.3.0xxl-job.version>
<aliyun.oss.version>3.9.0aliyun.oss.version>
properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-dependenciesartifactId>
<version>${cloud.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-alibaba-dependenciesartifactId>
<version>${alibaba.version}version>
<type>pomtype>
<scope>importscope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<version>${mybatis-plus.version}version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>${mysql.version}version>
dependency>
<dependency>
<groupId>com.github.xiaoymingroupId>
<artifactId>knife4j-spring-boot-starterartifactId>
<version>2.0.8version>
dependency>
<dependency>
<groupId>io.jsonwebtokengroupId>
<artifactId>jjwtartifactId>
<version>${jwt.version}version>
dependency>
<dependency>
<groupId>org.apache.httpcomponentsgroupId>
<artifactId>httpclientartifactId>
<version>${httpclient.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<version>${fastjson.version}version>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>easyexcelartifactId>
<version>${easyexcel.version}version>
dependency>
<dependency>
<groupId>com.aliyungroupId>
<artifactId>aliyun-java-sdk-coreartifactId>
<version>${aliyun.version}version>
dependency>
<dependency>
<groupId>com.aliyun.ossgroupId>
<artifactId>aliyun-sdk-ossartifactId>
<version>${aliyun.oss.version}version>
dependency>
<dependency>
<groupId>joda-timegroupId>
<artifactId>joda-timeartifactId>
<version>${jodatime.version}version>
dependency>
<dependency>
<groupId>com.xuxueligroupId>
<artifactId>xxl-job-coreartifactId>
<version>${xxl-job.version}version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
<version>${swagger.version}version>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
<version>${swagger.version}version>
dependency>
dependencies>
dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-maven-pluginartifactId>
plugin>
plugins>
build>
project>
<dependencies>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<scope>provided scope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>easyexcelartifactId>
<scope>provided scope>
dependency>
<dependency>
<groupId>com.github.xiaoymingroupId>
<artifactId>knife4j-spring-boot-starterartifactId>
<scope>providedscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-mongodbartifactId>
<scope>provided scope>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
<scope>provided scope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-data-elasticsearchartifactId>
<scope>provided scope>
dependency>
dependencies>
<dependencies>
<dependency>
<groupId>com.atguigugroupId>
<artifactId>modelartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discoveryartifactId>
dependency>
<dependency>
<groupId>org.springframework.cloudgroupId>
<artifactId>spring-cloud-starter-openfeignartifactId>
dependency>
<dependency>
<groupId>com.alibaba.cloudgroupId>
<artifactId>spring-cloud-starter-alibaba-sentinelartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-devtoolsartifactId>
<optional>trueoptional>
dependency>
dependencies>
在service模块下面创建子模块,service_void(视频点播模块)
讲师管理模块需求:添加课程时候,需要选择所属讲师,所以要对讲师进行管理,就是基于讲师的CRUD操作
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-generatorartifactId>
<version>3.3.1version>
dependency>
<dependency>
<groupId>org.apache.velocitygroupId>
<artifactId>velocity-engine-coreartifactId>
<version>2.0version>
dependency>
package com.jq;
import com.baomidou.mybatisplus.annotation.DbType;
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.NamingStrategy;
public class CodeGet {
public static void main(String[] args) {
// 1、创建代码生成器
AutoGenerator mpg = new AutoGenerator();
// 2、全局配置
// 全局配置
GlobalConfig gc = new GlobalConfig();
String projectPath = System.getProperty("user.dir");
//gc.setOutputDir(projectPath + "/src/main/java");
gc.setOutputDir("F:\\JavaCode\\ggkt_parent\\service\\service_vod"+"/src/main/java");
gc.setServiceName("%sService"); //去掉Service接口的首字母I
gc.setAuthor("CJQ");
gc.setOpen(false);
mpg.setGlobalConfig(gc);
// 3、数据源配置
DataSourceConfig dsc = new DataSourceConfig();
dsc.setUrl("jdbc:mysql://localhost:3306/glkt_vod");
dsc.setDriverName("com.mysql.jdbc.Driver");
dsc.setUsername("root");
dsc.setPassword("123456");
dsc.setDbType(DbType.MYSQL);
mpg.setDataSource(dsc);
// 4、包配置
PackageConfig pc = new PackageConfig();
pc.setModuleName("vod"); //模块名
pc.setParent("com.jq");
pc.setController("controller");
pc.setEntity("entity");
pc.setService("service");
pc.setMapper("mapper");
mpg.setPackageInfo(pc);
// 5、策略配置
StrategyConfig strategy = new StrategyConfig();
strategy.setInclude("teacher");
strategy.setNaming(NamingStrategy.underline_to_camel);//数据库表映射到实体的命名策略
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();
}
}
# 服务端口
server.port=8301
# 服务名
spring.application.name=service-vod
# 环境设置:dev、test、prod
spring.profiles.active=dev
# mysql数据库连接
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/glkt_vod?characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
#返回json的全局时间格式
spring.jackson.date-format=yyyy-MM-dd HH:mm:ss
spring.jackson.time-zone=GMT+8
#mybatis日志
mybatis-plus.configuration.log-impl=org.apache.ibatis.logging.stdout.StdOutImpl
package com.jq.vod.controller;
import com.jq.model.vod.Teacher;
import com.jq.vod.service.TeacherService;
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 java.util.List;
/**
*
* 讲师 前端控制器
*
*
* @author CJQ
* @since 2022-07-22
*/
@RestController
@RequestMapping("/admin/vod/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
// http://localhost:8301/admin/vod/teacher/findAll
//1.查询所有讲师
@GetMapping("findAll")
public List<Teacher> findAllTeacher(){
List<Teacher> list = teacherService.list();
return list;
}
}
package com.jq.vod.config;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.jq.vod.mapper")
public class VodConfig {
}
访问http://localhost:8301/admin/vod/teacher/findAll
得到json数据
//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);
}
}
package com.jq.vod.config;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import javafx.scene.control.Pagination;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.jq.vod.mapper")
public class VodConfig {
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor(){
return new PaginationInterceptor();
}
}
//3.条件查询分页
@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);
}
}
// 4.添加讲师
@ApiOperation("添加讲师")
@PostMapping("saveTeacher")
public Result saveTeacher(@RequestBody Teacher teacher){
boolean isSuccess = teacherService.save(teacher);
if (isSuccess){
return Result.ok(null);
}else {
return Result.fail(null);
}
}
// 5.修改接口 -根据id查询
@ApiOperation("根据id查询")
@GetMapping("getTeacher/{id}")
public Result getTeacher(@PathVariable Long id){
Teacher teacher = teacherService.getById(id);
return Result.ok(teacher);
}
// 6.修改 最终实现
@ApiOperation("修改最终实现")
@PostMapping("updateTeacher")
public Result updateTeacher(@RequestBody Teacher teacher){
boolean isSuccess = teacherService.updateById(teacher);
if (isSuccess){
return Result.ok(null);
}else {
return Result.fail(null);
}
}
// 7. 批量删除讲师
// json 的数组形式[1,2,3] 用@RequestBody ListidList 接收
@ApiOperation("批量删除讲师")
@DeleteMapping("removeBatch")
public Result removeBatch(@RequestBody List<Long>idList){
boolean isSuccess = teacherService.removeByIds(idList);
if (isSuccess){
return Result.ok(null);
}else {
return Result.fail(null);
}
}
比如上面:删除讲师接口是delete提交方式,使用浏览器无法直接访问测试,可以通过工具测试,比如Postman,我们这里通过整合Swagger2进行接口测试
前后端分离开发模式中,api文档是最好的沟通方式。
Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。及时性 (接口变更后,能够及时准确地通知相关前后端开发人员)规范性 (并且保证接口的规范性,如接口的地址,请求方式,参数及响应格式和错误信息)一致性 (接口信息一致,不会出现因开发人员拿到的文档版本不一致,而出现分歧)可测性 (直接在接口文档上进行测试,以方便理解业务)
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>ggkt_parentartifactId>
<groupId>com.jqgroupId>
<version>0.0.1-SNAPSHOTversion>
parent>
<modelVersion>4.0.0modelVersion>
<artifactId>commonartifactId>
<properties>
<maven.compiler.source>8maven.compiler.source>
<maven.compiler.target>8maven.compiler.target>
properties>
<dependencies>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
<scope>provided scope>
dependency>
<dependency>
<groupId>com.baomidougroupId>
<artifactId>mybatis-plus-boot-starterartifactId>
<scope>provided scope>
dependency>
<dependency>
<groupId>org.projectlombokgroupId>
<artifactId>lombokartifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger2artifactId>
dependency>
<dependency>
<groupId>io.springfoxgroupId>
<artifactId>springfox-swagger-uiartifactId>
dependency>
<dependency>
<groupId>com.alibabagroupId>
<artifactId>fastjsonartifactId>
dependency>
dependencies>
project>
package com.jq.swagger;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
@Configuration
@EnableSwagger2
public class Swagger2Config {
@Bean
public Docket webApiConfig(){
return new Docket(DocumentationType.SWAGGER_2)
.groupName("ggkt")
.apiInfo(webApiInfo())
.select()
//只显示api路径下的页面
//.paths(Predicates.and(PathSelectors.regex("/api/.*")))
.build();
}
private ApiInfo webApiInfo(){
return new ApiInfoBuilder()
.title("网站-API文档")
.description("本文档描述了网站微服务接口定义")
.version("1.0")
.contact(new Contact("jq", "http://jq.com", "jq.com"))
.build();
}
}
<dependency>
<groupId>com.atguigugroupId>
<artifactId>service_utilsartifactId>
<version>0.0.1-SNAPSHOTversion>
dependency>
package com.jq.vod;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan("com.jq")
public class ServiceVodApplication {
public static void main(String[] args) {
SpringApplication.run(ServiceVodApplication.class);
}
}
@Api(tags = "讲师管理接口")
package com.jq.vod.controller;
import com.jq.model.vod.Teacher;
import com.jq.vod.service.TeacherService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
*
* 讲师 前端控制器
*
*
* @author CJQ
* @since 2022-07-22
*/
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping("/admin/vod/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
// http://localhost:8301/admin/vod/teacher/findAll
//1.查询所有讲师
@GetMapping("findAll")
public List<Teacher> findAllTeacher(){
List<Teacher> list = teacherService.list();
return list;
}
//remove/2
//2.逻辑删除讲师
@DeleteMapping("remove/{id}")
public boolean removeTeacher(@PathVariable Long id){
boolean isSuccess = teacherService.removeById(id);
return isSuccess;
}
}
@ApiOperation("查询所有讲师")
package com.jq.vod.controller;
import com.jq.model.vod.Teacher;
import com.jq.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
*
* 讲师 前端控制器
*
*
* @author CJQ
* @since 2022-07-22
*/
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping("/admin/vod/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
// http://localhost:8301/admin/vod/teacher/findAll
//1.查询所有讲师
@ApiOperation("查询所有讲师")
@GetMapping("findAll")
public List<Teacher> findAllTeacher(){
List<Teacher> list = teacherService.list();
return list;
}
//remove/2
//2.逻辑删除讲师
@ApiOperation("逻辑删除讲师")
@DeleteMapping("remove/{id}")
public boolean removeTeacher(@PathVariable Long id){
boolean isSuccess = teacherService.removeById(id);
return isSuccess;
}
}
@ApiParam(name="id",value = "ID",required = true)
package com.jq.vod.controller;
import com.jq.model.vod.Teacher;
import com.jq.vod.service.TeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
/**
*
* 讲师 前端控制器
*
*
* @author CJQ
* @since 2022-07-22
*/
@Api(tags = "讲师管理接口")
@RestController
@RequestMapping("/admin/vod/teacher")
public class TeacherController {
@Autowired
private TeacherService teacherService;
// http://localhost:8301/admin/vod/teacher/findAll
//1.查询所有讲师
@ApiOperation("查询所有讲师")
@GetMapping("findAll")
public List<Teacher> findAllTeacher(){
List<Teacher> list = teacherService.list();
return list;
}
//remove/2
//2.逻辑删除讲师
@ApiOperation("逻辑删除讲师")
@DeleteMapping("remove/{id}")
public boolean removeTeacher(@ApiParam(name="id",value = "ID",required = true)
@PathVariable Long id){
boolean isSuccess = teacherService.removeById(id);
return isSuccess;
}
}
建议直接复制代码class文件,不要去复制代码,自己复制代码,会出现依赖的一些错误
package com.jq.result;
import lombok.Data;
/**
* 统一返回结果类
*/
@Data
public class Result<T> {
private Integer code; //状态码
private String message; //返回状态信息(成功或者失败)
private T data;//返回数据
public Result(){}
// 成功的方法
public static<T> Result<T>ok(T data){
Result<T>result=new Result<>();
if(data!=null){
result.setData(data);
}
result.setCode(200);
result.setMessage("成功");
return result;
}
// 失败的方法
public static<T> Result<T>fail(T data){
Result<T>result=new Result<>();
if(data!=null){
result.setData(data);
}
result.setCode(201);
result.setMessage("失败");
return result;
}
public Result<T> message(String msg){
this.setMessage(msg);
return this;
}
public Result<T> code(Integer code){
this.setCode(code);
return this;
}
}
未修改之前
//1.查询所有讲师
@ApiOperation("查询所有讲师")
@GetMapping("findAll")
public List<Teacher> findAllTeacher(){
List<Teacher> list = teacherService.list();
return list;
}
修改之后
//1.查询所有讲师
@ApiOperation("查询所有讲师")
@GetMapping("findAll")
public Result findAllTeacher(){
List<Teacher> list = teacherService.list();
return Result.ok(list);
}
package com.jq.exception;
import com.jq.result.Result;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
@ControllerAdvice //aop
public class GlobalExceptionHandler {
// 全局异常处理
@ExceptionHandler(Exception.class)
@ResponseBody
public Result error(Exception e){
e.printStackTrace();
return Result.fail(null).message("执行全局异常处理");
}
}
// 特定异常处理 ArithmeticException
@ExceptionHandler(ArithmeticException.class)
@ResponseBody
public Result error(ArithmeticException e){
System.out.println("特定...");
e.printStackTrace();
return Result.fail(null).message("执行ArithmeticException异常处理");
}
自定义异常类
package com.jq.exception;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
public class GgktException extends RuntimeException{
private Integer code;
private String msg;
}
使用
// 自定义异常处理
@ExceptionHandler(GgktException.class)
@ResponseBody
public Result error(GgktException e){
System.out.println("自定义...");
e.printStackTrace();
return Result.fail(null).code(e.getCode()).message(e.getMsg());
}
手动抛出异常
//模拟异常
try {
int i=10/0;
}catch (Exception e){
//抛出异常
throw new GgktException(201,"执行自定义异常");
}