我是靠谱客的博主 欣慰冬天,最近开发中收集的这篇文章主要介绍PageHelper还能结合Lambda表达式实现简洁的分页封装1、引入POM2、封装PageBean3、用法3.1 定义Mapper3.2 定义Mapping3.3 具体实现4、测试,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
目录
- 1、引入POM
- 2、封装PageBean
- 3、用法
- 3.1 定义Mapper
- 3.2 定义Mapping
- 3.3 具体实现
- 4、测试
1、引入POM
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
2、封装PageBean
创建查询分页类BaseQueryParam
,其需要传递的查询参数类,可以直接进行继承,代码如下:
@Data
public class BaseQueryParam {
/**
* 当前页码
*/
private Integer pageNum;
/**
* 每页数量
*/
private Integer pageSize;
}
封装新的PageBean
,利用泛型封装统一的请求方法与响应分页信息参数,代码如下:
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import java.util.List;
public class NewPageBean<T> {
private Long total;
private List<T> list;
private MyPage pageInfo;
/**
* @param select 调用Mapper接口的结果
* @param param 请求参数
* @param tClass 响应的Class对象
* @param <T>
* @return
*/
public static <T> NewPageBean<T> generatePage(ISelect select, BaseQueryParam param, Class<T> tClass) {
Integer current = param.getPageNum();
Integer pageSize = param.getPageSize();
List<T> list = PageHelper
.startPage(current == null ? 1 : current, pageSize == null ? 10 : pageSize)
.doSelectPage(select);
return new NewPageBean<>(list);
}
public NewPageBean(List<T> list) {
PageInfo<T> pageInfo = new PageInfo<>(list);
this.total = pageInfo.getTotal();
this.list = list;
MyPage page = new MyPage();
page.setTotalPage(pageInfo.getPages());
page.setPageNum(pageInfo.getPageNum());
page.setPageSize(pageInfo.getPageSize());
page.setPreviousPage(pageInfo.getPrePage());
page.setNextPage(pageInfo.getNextPage());
this.pageInfo = page;
}
public long getNum() {
return this.total;
}
public void setNum(Long total) {
this.total = total;
}
public Long getTotal() {
return total;
}
public void setTotal(Long total) {
this.total = total;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
public MyPage getPageInfo() {
return pageInfo;
}
public void setPageInfo(MyPage pageInfo) {
this.pageInfo = pageInfo;
}
static class MyPage {
private int pageNum;
private int pageSize;
private int totalPage;
private int nextPage;
private int previousPage;
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getNextPage() {
return nextPage;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
public int getPreviousPage() {
return previousPage;
}
public void setPreviousPage(int previousPage) {
this.previousPage = previousPage;
}
}
}
3、用法
3.1 定义Mapper
@Mapper
public interface TaskMapper {
List<Task> list(BaseQueryParam param);
}
3.2 定义Mapping
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.mapper.TaskMapper">
<select id="list" parameterType="com.lhz.controller.page.BaseQueryParam" resultMap="BaseResultMap">
select
id,name,create_by,create_time,update_by,update_time,remark
from sys_task
</select>
</mapper>
3.3 具体实现
此处不写servce
,层,方便演示直接在Controller
中调用Mapper
@RestController
public class TaskController {
@Resource
private TaskMapper taskMapper;
@GetMapping("test")
public Object test(BaseQueryParam param) {
/**
* BaseQueryParam 就是查询的参数
* Task 为响应的实体
*/
return NewPageBean.generatePage(() -> taskMapper.list(param), param, Task.class);
}
}
4、测试
测试url:http://localhost:9090/demo/test?pageNum=2&pageSize=2
SQL预览:
返回结果:
{
"total":3,
"list":[
{
"id":"1",
"name":"定时任务一",
"createBy":"1",
"createTime":1601178428000,
"updateBy":"1",
"updateTime":1601178428000,
"remark":""
},
{
"id":"2",
"name":"定时任务二",
"createBy":"1",
"createTime":1601178546000,
"updateBy":"1",
"updateTime":1601178546000,
"remark":""
}
],
"pageInfo":{
"pageNum":1,
"pageSize":2,
"totalPage":2,
"nextPage":2,
"previousPage":0
},
"num":3
}
最后
以上就是欣慰冬天为你收集整理的PageHelper还能结合Lambda表达式实现简洁的分页封装1、引入POM2、封装PageBean3、用法3.1 定义Mapper3.2 定义Mapping3.3 具体实现4、测试的全部内容,希望文章能够帮你解决PageHelper还能结合Lambda表达式实现简洁的分页封装1、引入POM2、封装PageBean3、用法3.1 定义Mapper3.2 定义Mapping3.3 具体实现4、测试所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复