分頁查詢controller寫法
public PageResult findByList(@RequestBody UserDTO userDTO) {// 分頁IPage<User> page = new Page(UserDTO.getPageNumber(), UserDTO.getPageSize());// 條件構造器QueryWrapper queryWrapper = new QueryWrapper();queryWrapper.eq("user_name", userDTO.getUserName);// 查詢符合條件的內容IPage<User> pageResult = service.page(page, queryWrapper);// 獲取符合條件的List集合List<User> records = pageResult.getRecords();System.out.println("查詢結果 = " + records);// 返回結果return new PageResult(new Long(pageResult.getTotal()).intValue(), pageResult.getRecords());
}
返回結果集寫法
可根據需求自定義
/*** 分頁處理*/
@ApiModel("分頁響應類")
public class PageResult<T> {@ApiModelProperty(value ="響應代碼")private int code;@ApiModelProperty(value ="提示信息")private String msg;@ApiModelProperty(value ="總記錄數")private int count;@ApiModelProperty(value ="查詢到的結果集")private List<T> data;/*** 默認構造方法*/public PageResult() {}public PageResult(int total, List<T> rows) {this.count = total;this.data = rows;this.code = 200;this.msg = Constant.ResponseMsg.OPERATE_SUCCESS;}public int getCode() {return code;}public void setCode(int code) {this.code = code;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}public int getCount() {return count;}public void setCount(int count) {this.count = count;}public List<T> getData() {return data;}public void setData(List<T> data) {this.data = data;}
}