当前位置:网站首页>PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
PageHelper can also be combined with lambda expressions to achieve concise paging encapsulation
2022-07-25 17:42:00 【Suddenly】
Catalog
1、 introduce POM
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
2、 encapsulation PageBean
Create query paging class BaseQueryParam, The query parameter class it needs to pass , You can inherit directly , The code is as follows :
@Data
public class BaseQueryParam {
/** * The current page number */
private Integer pageNum;
/** * Number of pages */
private Integer pageSize;
}
Encapsulate the new PageBean, Use generics to encapsulate unified request method and response paging information parameters , The code is as follows :
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 call Mapper The result of the interface * @param param Request parameters * @param tClass Responsive Class object * @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、 usage
3.1 Definition Mapper
@Mapper
public interface TaskMapper {
List<Task> list(BaseQueryParam param);
}
3.2 Definition 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 Concrete realization
Don't write here servce, layer , Convenient demonstration directly in Controller Call in Mapper
@RestController
public class TaskController {
@Resource
private TaskMapper taskMapper;
@GetMapping("test")
public Object test(BaseQueryParam param) {
/** * BaseQueryParam Is the query parameter * Task Is the entity that responded */
return NewPageBean.generatePage(() -> taskMapper.list(param), param, Task.class);
}
}
4、 test
test url:http://localhost:9090/demo/test?pageNum=2&pageSize=2
SQL preview :
Return results :
{
"total":3,
"list":[
{
"id":"1",
"name":" Timed task one ",
"createBy":"1",
"createTime":1601178428000,
"updateBy":"1",
"updateTime":1601178428000,
"remark":""
},
{
"id":"2",
"name":" Timed task two ",
"createBy":"1",
"createTime":1601178546000,
"updateBy":"1",
"updateTime":1601178546000,
"remark":""
}
],
"pageInfo":{
"pageNum":1,
"pageSize":2,
"totalPage":2,
"nextPage":2,
"previousPage":0
},
"num":3
}
边栏推荐
- 面试官:说说 log.Fatal 和 panic 的区别
- [cadence Allegro PCB design] permanently modify the shortcut key (customized) ~ it is valid for personal test~
- 哈夫曼树的构建
- 带你初步了解多方安全计算(MPC)
- Take you to a preliminary understanding of multiparty secure computing (MPC)
- stm32F407------SPI
- 对灰度图像的三维函数显示
- Interviewer: talk about log The difference between fatal and panic
- An article about ultrasonic humidifier
- 栈的顺序存储结构,链式存储结构及实现
猜你喜欢

ACL 2022 | comparative learning based on optimal transmission to achieve interpretable semantic text similarity

Take you to a preliminary understanding of multiparty secure computing (MPC)

哈夫曼树的构建

超越 ConvNeXt、RepLKNet | 看 51×51 卷积核如何破万卷!

EDI 对接CommerceHub OrderStream

四六级

食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!

Excel表格 / WPS表格中怎么在下拉滚动时让第一行标题固定住?

世界各地的标志性建筑物

十九岁的总结
随机推荐
Wu Enda logistic regression 2
走马卒
【Cadence Allegro PCB设计】error: Possible pin type conflict GND/VCC Power Connected to Output
go接口变量的类型断言
Trooper
Step by step introduction of sqlsugar based development framework (13) -- package the upload component based on elementplus, which is convenient for the project
02. Add two numbers
mysql case when
Update 3dcat real time cloud rendering V2.1.2 release
An article about ultrasonic humidifier
Redis源码与设计剖析 -- 16.AOF持久化机制
交友活动记录
RedisTemplate解决高并发下秒杀系统库存超卖方案 — Redis事务+乐观锁机制
爬虫框架-crawler
stm32F407------SPI
go语言context控制函数执行超时返回
无聊发文吐槽工作生活
WPF 实现用户头像选择器
Food safety | eight questions and eight answers take you to know crayfish again! This is the right way to eat!
8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇