JSON(JavaScript Object Notation, JS对象简谱)是一种轻量级的数据交换格式。它基于 ECMAScript(European Computer Manufacturers Association, 欧洲计算机协会制定的js规范)的一个子集,采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成,并有效地提升网络传输效率。
package com.attendance.common;
import com.github.pagehelper.PageInfo;
import lombok.Data;
import java.util.List;
@Data
public class CommonPage<T> {
//当前页
private int pageNum;
//每页的数量
private int pageSize;
//当前页的数量
private int size;
//总记录数
private long total;
//总页数
private int pages;
//是否为第一页
private boolean isFirstPage = false;
//是否为最后一页
private boolean isLastPage = false;
//是否有前一页
private boolean hasPreviousPage = false;
//是否有下一页
private boolean hasNextPage = false;
//结果集
private List<T> list;
public static <T> CommonPage<T> restPage(List<T> list) {
CommonPage<T> result = new CommonPage();
PageInfo<T> pageInfo = new PageInfo(list);
result.setPages(pageInfo.getPages());
result.setPageNum(pageInfo.getPageNum());
result.setPageSize(pageInfo.getPageSize());
result.setSize(pageInfo.getSize());
result.setTotal(pageInfo.getTotal());
result.setFirstPage(pageInfo.isIsFirstPage());
result.setHasNextPage(pageInfo.isHasNextPage());
result.setHasPreviousPage(pageInfo.isHasPreviousPage());
result.setLastPage(pageInfo.isIsLastPage());
result.setList(pageInfo.getList());
return result;
}
}
// 根据条件查学生信息
@Override
public CommonResult selectStudent(Student student, Pages pages) {
PageHelper.startPage(pages.getPage(),pages.getNum());
return CommonResult.success(CommonPage.restPage(studentMapper.selectStudent(student)));
}
package com.attendance.pojo;
import lombok.Data;
@Data
public class Pages {
private int page = 1;//第几页
private int num = 5;//每页几条数据
}
package com.attendance.common;
import lombok.Data;
@Data
public class CommonResult<T> {
private long code;
private String message;
private boolean flag;
private T data;
protected CommonResult() {
}
protected CommonResult(long code, boolean flag, String message, T data) {
this.code = code;
this.flag = flag;
this.message = message;
this.data = data;
}
public CommonResult(long code, boolean flag, String message) {
this.code = code;
this.flag = flag;
this.message = message;
}
public static <T> CommonResult<T> success(T data) {
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), ResultCode.SUCCESS.getMessage(), data);
}
public static <T> CommonResult<T> success(T data, String message) {
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), message, data);
}
public static <T> CommonResult<T> success(String message) {
return new CommonResult(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getFlag(), message);
}
public static <T> CommonResult<T> failed(IErrorCode errorCode) {
return new CommonResult(errorCode.getCode(), errorCode.getFlag(), errorCode.getMessage(), (Object)null);
}
public static <T> CommonResult<T> failed(IErrorCode errorCode, String message) {
return new CommonResult(errorCode.getCode(), errorCode.getFlag(), message, (Object)null);
}
public static <T> CommonResult<T> failed(String message) {
return new CommonResult(ResultCode.FAILED.getCode(), ResultCode.FAILED.getFlag(), message, (Object)null);
}
public static <T> CommonResult<T> failed() {
return failed((IErrorCode)ResultCode.FAILED);
}
public static <T> CommonResult<T> validateFailed() {
return failed((IErrorCode)ResultCode.VALIDATE_FAILED);
}
public static <T> CommonResult<T> validateFailed(String message) {
return new CommonResult(ResultCode.VALIDATE_FAILED.getCode(), ResultCode.VALIDATE_FAILED.getFlag(), message, (Object)null);
}
public static <T> CommonResult<T> unauthorized(T data) {
return new CommonResult(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getFlag(), ResultCode.UNAUTHORIZED.getMessage(), data);
}
public static <T> CommonResult<T> forbidden(T data) {
return new CommonResult(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getFlag(), ResultCode.FORBIDDEN.getMessage(), data);
}
}
package com.attendance.common;
public interface IErrorCode {
long getCode();
boolean getFlag();
String getMessage();
}
package com.attendance.common;
public enum ResultCode implements IErrorCode {
SUCCESS(200L, true, "操作成功"),
FAILED(500L, false, "操作失败"),
VALIDATE_FAILED(404L, false, "参数检验失败"),
UNAUTHORIZED(401L, false, "暂未登录或token已经过期"),
FORBIDDEN(403L, false, "没有相关权限");
private long code;
private boolean flag;
private String message;
private ResultCode(long code, boolean flag, String message) {
this.code = code;
this.flag = flag;
this.message = message;
}
public long getCode() {
return this.code;
}
public boolean getFlag() {
return this.flag;
}
public String getMessage() {
return this.message;
}
}
就是让方法的返回值变成CommonResult
// 新增学生信息
@Override
public CommonResult selectStudentById(int id) {
Student student = studentMapper.selectStudentById(id);
return CommonResult.success(student,"查询成功!");
}
// 更改学生状态
@Override
public CommonResult updateStudentInfo(Student student) {
int i = studentMapper.updateStudentInfo(student);
if (i==1){
return CommonResult.success("修改信息成功!");
}else {
return CommonResult.failed("修改数据失败!");
}
}
// 更新学生基本信息
@PostMapping("/updateStudentInfo")
public CommonResult updateStudentInfo(@RequestBody Student student){
return studentService.updateStudentInfo(student);
}
// 更改学生状态
@PostMapping("/modifyStudentStatus")
public CommonResult modifyStudentStatus(@RequestBody Student student){
return studentService.modifyStudentStatus(student);
}
{
"code": 0,
"message": "查询成功!",
"flag": true,
"data": [
{
"id": 1,
"stuName": "苏文绩",
"stuNative": "重庆市铜梁区",
"stuSex": "男",
"stuPhone1": "17782299402",
"stuPhone2": "19112799705",
"classType": "文科",
"stuIdentity": "500224200012050053",
"stuAddress": "重庆市铜梁区东城街道玉泉花苑5-12-3",
"specialType": "单亲家庭",
"stuScore": "502",
"stuMajor1": "软件工程",
"stuMajor2": "计算机科学与技术",
"status": 0,
"contactContent": "",
"version": 1,
"createTime": "2022-06-23T09:50:26.000+00:00",
"updateTime": "2022-06-27T03:55:51.000+00:00",
"deleted": false
},
{
"id": 2,
"stuName": "谢汝雄",
"stuNative": "重庆市江津区",
"stuSex": "男",
"stuPhone1": "17353192303",
"stuPhone2": "11651561651",
"classType": "文科",
"stuIdentity": "500244200007031125",
"stuAddress": "重庆市江津区",
"specialType": "孤儿",
"stuScore": "600",
"stuMajor1": "自动化",
"stuMajor2": "艺术与科技",
"status": 1,
"contactContent": "adasd",
"version": 1,
"createTime": "2022-06-23T09:52:18.000+00:00",
"updateTime": "2022-06-27T04:42:21.000+00:00",
"deleted": false
},
{
"id": 3,
"stuName": "张幸",
"stuNative": "重庆市万州区",
"stuSex": "男",
"stuPhone1": "173151515",
"stuPhone2": "12.1.21.1.15.15",
"classType": "理科",
"stuIdentity": "56156165165",
"stuAddress": "重庆市万州区",
"specialType": "孤儿,优扶家庭子女,家庭突发重大变故",
"stuScore": "504",
"stuMajor1": "计算机科学与技术",
"stuMajor2": "艺术与科技",
"status": 0,
"contactContent": "",
"version": 1,
"createTime": "2022-06-23T09:53:06.000+00:00",
"updateTime": "2022-06-26T08:47:18.000+00:00",
"deleted": false
},
{
"id": 4,
"stuName": "赵云",
"stuNative": "124124",
"stuSex": "男",
"stuPhone1": "12124124",
"stuPhone2": "12512512",
"classType": "1",
"stuIdentity": "124124",
"stuAddress": "123123123",
"specialType": "孤儿,烈士子女,优扶家庭子女,建档立卡贫困家庭学生",
"stuScore": "124124",
"stuMajor1": "通信工程",
"stuMajor2": "自动化",
"status": 0,
"contactContent": "",
"version": 1,
"createTime": "2022-06-23T10:29:57.000+00:00",
"updateTime": "2022-06-26T08:52:32.000+00:00",
"deleted": false
},
{
"id": 5,
"stuName": "张三",
"stuNative": "重庆",
"stuSex": "男",
"stuPhone1": "12124",
"stuPhone2": "12412",
"classType": "1",
"stuIdentity": "500224",
"stuAddress": "124125125",
"specialType": "孤儿,烈士子女,优扶家庭子女,建档立卡贫困家庭学生",
"stuScore": "654",
"stuMajor1": "市场营销",
"stuMajor2": "信息工程",
"status": 0,
"contactContent": "",
"version": 1,
"createTime": "2022-06-23T11:02:43.000+00:00",
"updateTime": "2022-06-26T09:18:58.000+00:00",
"deleted": false
}
],
"count": 21
}