目录
第一步:在common模块下创建子模块common-utils
一、统一返回结果
1、统一返回数据格式
项目中我们会将响应封装成json返回,一般我们会将所有接口的数据格式统一, 使前端(iOS Android,Web)对数据的操作更一致、轻松;
一般情况下,统一返回数据格式没有固定的格式,只要能描述清楚返回的数据状态以及要返回的具体数据就可以。但是一般会包含状态码、返回消息、数据这几部分内容;
例如,我们的系统要求返回的基本数据格式如下:
列表:
{
"success": true,
"code": 20000,
"message": "成功",
"data": {
"items": [{
"id": "1",
"name": "刘德华",
"intro": "毕业于师范大学数学系,热爱教育事业,执教数学思维6年有余"
}]
}
}
分页:
{
"success": true,
"code": 20000,
"message": "成功",
"data": {
"total": 17,
"rows": [{
"id": "1",
"name": "刘德华",
"intro": "毕业于师范大学数学系,热爱教育事业,执教数学思维6年有余"
}]
}
}
没有返回数据:
{
"success": true,
"code": 20000,
"message": "成功",
"data": {}
}
失败:
{
"success": false,
"code": 20001,
"message": "失败",
"data": {}
}
因此,我们定义统一结果:
{
"success": 布尔, //响应是否成功
"code": 数字, //响应码
"message": 字符串, //返回消息
"data": HashMap //返回数据,放在键值对中
}
2、创建统一结果返回类
第一步:在common模块下创建子模块common-utils
第二步:创建接口定义返回码
创建包com.zibo.commonutils,创建接口 ResultCode.java
package com.zibo.commonutils;
public interface ResultCode {
//成功状态码
Integer SUCCESS = 20000;
//失败状态码
Integer ERROR = 20001;
}
第三步:创建结果类
package com.zibo.commonutils;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.util.HashMap;
import java.util.Map;
@Data
public class R {
@ApiModelProperty(value = "是否成功")
private Boolean success;
@ApiModelProperty(value = "返回码")
private Integer code;
@ApiModelProperty(value = "返回消息")
private String message;
@ApiModelProperty(value = "返回数据")
private Map<String, Object> data = new HashMap<>();
//私有化构造方法
private R() {
}
//成功
public static R ok() {
R r = new R();
r.setSuccess(true);
r.setCode(ResultCode.SUCCESS);
r.setMessage("成功");
return r;
}
//失败
public static R error() {
R r = new R();
r.setSuccess(false);
r.setCode(ResultCode.ERROR);
r.setMessage("失败");
return r;
}
//下面是链式编程
public R success(Boolean success) {
this.setSuccess(success);
return this;
}
public R message(String message) {
this.setMessage(message);
return this;
}
public R code(Integer code) {
this.setCode(code);
return this;
}
public R data(String key, Object value) {
this.data.put(key, value);
return this;
}
public R data(Map<String, Object> map) {
this.setData(map);
return this;
}
}
3、统一返回结果的使用
第一步:在service引入common_utils
<dependency>
<groupId>com.zibo</groupId>
<artifactId>common_utils</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
第二步:修改Controller中的返回结果
package com.zibo.edu.controller;
import com.zibo.commonutils.R;
import com.zibo.edu.entity.Teacher;
import com.zibo.edu.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.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <p>
* 讲师 前端控制器
* </p>
*
* @author zibo
* @since 2020-11-30
*/
@RestController
@RequestMapping("/edu/teacher")
@Api(tags = {"教师管理模块TeacherController"})
public class TeacherController {
@Autowired
private TeacherService teacherService;
//查询所有
@ApiOperation("查询所有")
@RequestMapping("/findall")
public R findAll(){
return R.ok().data("items", teacherService.list(null));
}
//逻辑删除
@ApiOperation("逻辑删除")
@RequestMapping("{id}")//id通过路径传过来
// @DeleteMapping("{id}")
public R hide(@ApiParam("要删除教师的ID") @PathVariable("id")String id){
boolean flag = teacherService.removeById(id);
if(flag){
return R.ok();
}else {
return R.error();
}
}
}
第三步:测试
通过swagger测试:
二、分页查询
1、MyBatisPlusConfig中配置分页插件
package com.zibo.edu.config;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;
import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
@MapperScan("com.zibo.edu.mapper")
public class EduConfig {
/**
* 逻辑删除插件
*/
@Bean
public ISqlInjector sqlInjector() {
return new LogicSqlInjector();
}
/**
* 分页插件
*/
@Bean
public PaginationInterceptor paginationInterceptor() {
return new PaginationInterceptor();
}
}
2、分页Controller方法
package com.zibo.edu.controller;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zibo.commonutils.R;
import com.zibo.edu.entity.Teacher;
import com.zibo.edu.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.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
/**
* <p>
* 讲师 前端控制器
* </p>
*
* @author zibo
* @since 2020-11-30
*/
@RestController
@RequestMapping("/edu/teacher")
@Api(tags = {"教师管理模块TeacherController"})
public class TeacherController {
@Autowired
private TeacherService teacherService;
//查询所有
@ApiOperation("查询所有")
@RequestMapping("/findall")
public R findAll(){
return R.ok().data("items", teacherService.list(null));
}
//逻辑删除
@ApiOperation("逻辑删除")
@RequestMapping("{id}")//id通过路径传过来
// @DeleteMapping("{id}")
public R hide(@ApiParam("要删除教师的ID") @PathVariable("id")String id){
boolean flag = teacherService.removeById(id);
if(flag){
return R.ok();
}else {
return R.error();
}
}
@ApiOperation(value = "分页讲师列表")
@RequestMapping("{page}/{limit}")
public R pageList(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit){
Page<Teacher> pageParam = new Page<>(page, limit);
teacherService.page(pageParam, null);
List<Teacher> records = pageParam.getRecords();
long total = pageParam.getTotal();
return R.ok().data("total", total).data("rows", records);
}
}
3、在Swagger测试