目录
1.导入依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
</dependency>
2.响应包装类
定义包装类,响应数据给前端
@ApiModel("分页数据模型")
@Data
public class PageResult<T> implements Serializable {
/**
* 总记录数
*/
@ApiModelProperty(value = "总记录数")
private Long totalRows;
/**
* 总页数
*/
@ApiModelProperty(value = "总页数")
private Integer totalPages;
/**
* 当前第几页
*/
@ApiModelProperty(value = "当前第几页")
private Integer pageNum;
/**
* 每页记录数
*/
@ApiModelProperty(value = "每页记录数")
private Integer pageSize;
/**
* 当前页记录数
*/
@ApiModelProperty(value = "当前页记录数")
private Integer size;
/**
* 结果集
*/
@ApiModelProperty(value = "结果集")
private List<T> rows;
/**
* 分页数据组装
* @param pageInfo
*/
public PageResult(PageInfo<T> pageInfo) {
totalRows = pageInfo.getTotal();
totalPages = pageInfo.getPages();
pageNum = pageInfo.getPageNum();
pageSize = pageInfo.getPageSize();
size = pageInfo.getSize();
rows = pageInfo.getList();
}
}
3.业务逻辑实现
1.controller层
@GetMapping("/page")
public R<PageResult<StockUpDownDo>> getPageInfo( @RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
@RequestParam(value = "pageSize", required = false, defaultValue = "20") Integer pageSize) {
return stockService.getPageInfo(page,pageSize);
}
2.service层
@Override
public R<PageResult<StockUpDownDo>> getPageInfo(Integer page, Integer pageSize) {
//1.设置分页参数,自动sql语句添加limit
PageHelper.startPage(page, pageSize);
//2.调用mapper查询
List<StockUpDownDo> pageData=stockRtInfoMapper.getPageInfo();
//3.组装PageResult对象
PageInfo<StockUpDownDo> pageInfo = new PageInfo<>(pageData);
PageResult<StockUpDownDo> pageResult = new PageResult<>(pageInfo);
//4.响应数据
return R.ok(pageResult);
}
3.mapper层
<select id="getPageInfo" resultMap="BaseResultMap">
select
*
from use_info
</select>