当前位置:网站首页>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
}
边栏推荐
- 食品安全 | 八问八答带你重新认识小龙虾!这样吃才对!
- I2C通信——时序图
- [untitled]
- Beyond convnext, replknet | look 51 × 51 convolution kernel how to break ten thousand volumes!
- OSPF综合实验
- go channel简单笔记
- [solution] the Microsoft edge browser has the problem of "unable to access this page"
- With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
- Take you to a preliminary understanding of multiparty secure computing (MPC)
- How to fix the first row title when scrolling down in Excel table / WPS table?
猜你喜欢

With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development

如何看一本书
Principle and implementation of UDP penetration NAT in P2P

Installation steps and usage of NVM under windows10 system

Three dimensional function display of gray image

8 年产品经验,我总结了这些持续高效研发实践经验 · 研发篇

Postdoctoral recruitment | West Lake University Machine Intelligence Laboratory recruitment postdoctoral / Assistant Researcher / scientific research assistant

Cet

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

吴恩达机器学习编程作业无法暂停pause问题解决
随机推荐
RestTemplate通过泛型实现POST、PUT、DELETE、GET、集合请求以及文件上传(可批量文件、可带参数)的统一封装(可打印日志)
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
Stm32 paj7620u2 gesture recognition module (IIC communication) program source code explanation
POWERBOARD coco! Dino: let target detection embrace transformer
Notes on Flickr's dataset
约瑟夫环问题
11、照相机与透镜
实时黄金交易平台哪个可靠安全?
We were tossed all night by a Kong performance bug
OSPF---开放式最短优先路径协议
Update 3dcat real time cloud rendering V2.1.2 release
Dating activity records
走马卒
Lvgl 7.11 tileview interface cycle switching
With 8 years of product experience, I have summarized these practical experience of continuous and efficient research and development
什么是 IP SSL 证书,如何申请?
无聊发文吐槽工作生活
Ultimate doll 2.0 | cloud native delivery package
Methods of de duplication and connection query in MySQL database
04.寻找两个正序数组的中位数