分页插件使用
- 引入jar包坐标, 这里进行了自动配置, 因此不需要重新创建bean或者配置类
<!--分页插件-->
<!-- pagehelper分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
<version>1.3.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
- service层 (不是在controller层哦)
@Override
public PageInfo<VersionRecord> selectList(Integer pageNum, Integer pageSize, Integer updateType) {
//1. 开启分页
PageHelper.startPage(pageNum,pageSize);
//2. 接收list<object>集合
List<VersionRecord> versionRecordList = versionRecordMapper.selectPageByupdateType(updateType);
//3.
PageInfo<VersionRecord> pageInfo = new PageInfo<>(versionRecordList);
return pageInfo;
}
- controller层
@GetMapping("selectList")
public ResponseEntity selectList(@RequestParam(value = "pageNum",defaultValue = "1")Integer pageNum,
@RequestParam(value = "pageSize",defaultValue = "10")Integer pageSize,
@RequestParam(value = "updateType") Integer updateType) {
//直接返回这个分页对象即可
PageInfo<VersionRecord> versionRecordPageInfo = versionRecordService.selectList(pageNum, pageSize,updateType);
- 返回JSON文本格式
{
"content": {
"total": 3,
"list": [
{
"versionId": 8,
"updateType": 1,
"versionNumber": "2.2.7",
"versionSize": "193MB",
"versionContent": "1.测试版本发布",
"publishTime": "2021.11.04 15:48:49",
"publishAccount": "admin",
"updateState": 3,
"fileUrl": "/upload/images/2.2.7.apk"
},
{
"versionId": 6,
"updateType": 1,
"versionNumber": "2.2.6",
"versionSize": "193MB",
"versionContent": null,
"publishTime": "2021.11.04 14:41:09",
"publishAccount": "admin",
"updateState": 3,
"fileUrl": "/upload/images\\2.2.6.apk"
},
{
"versionId": 5,
"updateType": 1,
"versionNumber": "2.2.6",
"versionSize": "193MB",
"versionContent": null,
"publishTime": "2021.11.04 14:40:35",
"publishAccount": "admin",
"updateState": 3,
"fileUrl": "/upload/images\\2.2.6.apk"
}
],
"pageNum": 1,
"pageSize": 10,
"size": 3,
"startRow": 1,
"endRow": 3,
"pages": 1,
"prePage": 0,
"nextPage": 0,
"isFirstPage": true,
"isLastPage": true,
"hasPreviousPage": false,
"hasNextPage": false,
"navigatePages": 8,
"navigatepageNums": [
1
],
"navigateFirstPage": 1,
"navigateLastPage": 1
}
}
- 实际相当于执行了这样一条sql语句
select 查询列表
from 表
limit(page-1)*size ,size;
从n-1行后开始, 每行显示size个数据
分页查关键字询
特点:
①limit语句放在查询语句的,执行上语法上都在最后
②公式, 要显示的页数page ,每页的条目数 size
参数校验以及全局异常处理
- jar坐标
<!--参数校验-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
- 使用注解即可
public class Item {
@NotNull(message = "id不能为空")
@Min(value = 1, message = "id必须为正整数")
private Long id;
@Valid // 嵌套验证必须用@Valid
@NotNull(message = "props不能为空")
@Size(min = 1, message = "props至少要有一个自定义属性")
private List<Prop> props;
}
- 全局异常处理
/**
* 全局异常处理
*/
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* 当系统出现 MethodArgumentNotValidException 这个异常时,会调用下面的方法
* @param e
* @return
*/
@ExceptionHandler(value = MethodArgumentNotValidException.class)
public ResponseEntity jsonErrorHandler(MethodArgumentNotValidException e){
ReturnCode returnCode = ReturnCode.OK;
ResponseEntity responseEntity = null;
List<Map<String,Object>> list=new ArrayList<>();
List<ObjectError> allErrors = e.getBindingResult().getAllErrors();
for (ObjectError allError : allErrors) {
Map<String,Object> map=new HashMap<>();
map.put("defaultMessage",allError.getDefaultMessage());
map.put("objectName",allError.getObjectName());
// 注意,这里面拿到具体的某一个属性
FieldError fieldError= (FieldError) allError;
map.put("field",fieldError.getField());
list.add(map);
}
returnCode = ReturnCode.INFO_PARTID_FAIL;
responseEntity = new ResponseEntity(returnCode);
responseEntity.setContent(list);
return responseEntity;
}
}
==这个是返回实体
import com.example.callmachine.utils.ReturnCode;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ResponseEntity {
private ResponseHeader header = new ResponseHeader();
private Object content = new Object();
public ResponseHeader getHeader() {
return header;
}
public void setHeader(ResponseHeader header) {
this.header = header;
}
public Object getContent() {
return content;
}
public void setContent(Object content) {
this.content = content;
}
public ResponseEntity() {
}
public ResponseEntity(ReturnCode code) {
this.header.setRtnCode(code.getCode());
this.header.setReturnMsg(code.getMsg());
}
public static String toJson(String returnCode, Object result) {
ResponseHeader header = new ResponseHeader();
header.setRtnCode(returnCode);
header.setReturnMsg(ReturnCode.getReturnCodeByCode(returnCode).getMsg());
ResponseEntity resEntity = new ResponseEntity();
resEntity.setHeader(header);
if (null != result) {
resEntity.setContent(result);
}
return GsonUtils.toJson(resEntity);
}
public static void setResponse(HttpServletResponse response, String rspBody) throws IOException {
response.setContentType("text/json;charset=UTF-8");// application/json
response.getWriter().write(rspBody);
}
}
==返回码
public enum ReturnCode {
//(00000 - 00999) System or common code
ERROR ("0", "失败"),
OK ("1", "成功"),
SERVICE_NOT_FOUND ("2", "找不到系统服务"),
NO_DATA ("3", "暂无数据"),
ILLEGAL_ACCESS ("4", "非法访问"),
SYSTEM_ERROR ("999", "系统错误"),
ReturnCode(String code, String msg) {
this.code = code;
this.msg = msg;
}
private String code;
private String msg;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
//根据返回码获取返回对象
public static ReturnCode getReturnCodeByCode(String code){
for(ReturnCode returnCode : ReturnCode.values())
{
if(returnCode.getCode().equalsIgnoreCase(code))
return returnCode;
}
return null;
}