这样做分页,你喜欢吗?

关注我,升职加薪就是你!
话不多说,直接上干货。

  1. 先创建一个简单分页对象SimplePage。
    import java.io.Serializable;
    import java.util.List;
    
    /**
     * @author: Max
     * @time: 2022/6/20
     * @description: 简单分页对象
     */
    public class SimplePage<T> implements Serializable {
    
        private static final long serialVersionUID = 1L;
    
        public static int DEFAULT_PAGESIZE = 10;
        /**
         * 当前页码
         */
        private int pageNum = 0;
        /**
         * 每页条数
         */
        private int pageSize = DEFAULT_PAGESIZE;
        /**
         * 排序方式
         */
        private String orderBy;
    
        /**
         * 总记录数
         */
        private long total;
        /**
         * 总页数
         */
        private int pages;
        /**
         * 当前页结果集
         */
        private List<T> list;
    
        public SimplePage() {
    
        }
    
        public SimplePage(int pageNum,int pageSize) {
            this.pageNum = pageNum;
            this.pageSize = pageSize;
        }
    
        public SimplePage(int pageNum,int pageSize,String orderBy) {
            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.orderBy = orderBy;
        }
    
        public SimplePage(int pageNum, int pageSize, String orderBy, long total, int pages, List<T> list) {
            super();
            this.pageNum = pageNum;
            this.pageSize = pageSize;
            this.orderBy = orderBy;
            this.total = total;
            this.pages = pages;
            this.list = list;
        }
    
        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 String getOrderBy() {
            return orderBy;
        }
    
        public void setOrderBy(String orderBy) {
            this.orderBy = orderBy;
        }
    
        public long getTotal() {
            return total;
        }
    
        public void setTotal(long total) {
            this.total = total;
        }
    
        public int getPages() {
            return pages;
        }
    
        public void setPages(int pages) {
            this.pages = pages;
        }
    
        public List<T> getList() {
            return list;
        }
    
        public void setList(List<T> list) {
            this.list = list;
        }
    }
    
  2. 再搞一个我们实际使用的分页查询对象PageQuery。
import java.util.List;

/**
 * @author: Max
 * @time: 2022/6/20
 * @description: 分页查询对象
 */
public class PageQuery<T> {

    public static int DEFAULT_PAGE_SIZE = 10;

    /**
     * 查询条件
     */
    private T parameter;

    /**
     * 分页对象
     */
    private SimplePage<T> page;


    public PageQuery() {
        super();
    }

    public PageQuery(T parameter) {
        super();
        this.parameter = parameter;
    }

    public PageQuery(T parameter, SimplePage<T> page) {
        super();
        this.parameter = parameter;
        this.page = page;
    }

    public T getParameter() {
        return parameter;
    }

    public void setParameter(T parameter) {
        this.parameter = parameter;
    }

    public SimplePage<T> getPage() {
        return page;
    }

    public void setPage(SimplePage<T> page) {
        this.page = page;
    }

    /**
     * @Author:Max
     * @Description:返回查询结果集
     * @Date:2022/6/20
     * @Param:[]
     * @Return:java.util.List<T>
     */
    public List<T> result(){
        return this.page != null ? this.page.getList() : null;
    }

    /**
     * @Author:Max
     * @Description:判断查询结果集是否为空
     * @Date:2022/6/20
     * @Param:[]
     * @Return:boolean  page或list为空返回true
     */
    public boolean isEmpty() {
        return this.page == null || this.page.getList() == null || this.page.getList().size() == 0;
    }

    /**
     * @Author:Max
     * @Description:
     * 设置排序字段。
     * <br>
     * <b>说明:如果page已设置了默认排序,会被强制覆盖。</b>
     * <br>
     * 如果不想强制覆盖,建议使用 {@link #setOrderByIfAbsent(String)}
     * @Date:2022/6/20
     * @Param:[orderBy]  如: lastUpdateTime desc 按修改日期日期倒序。
     * @Return:void
     */
    public void setOrderBy(String orderBy) {
        if(this.page == null) {
            this.page = new SimplePage<>();
        }
        page.setOrderBy(orderBy);
    }

    /**
     * @Author:Max
     * @Description:设置排序字段。
     * <br>
     * <b>说明:当page已设置排序字段,该设置将不会被执行。</b>
     * <br>
     * 如果想强制设置排序,建议使用 {@link #setOrderBy(String)}
     * @Date:2022/6/20
     * @Param:[orderBy] orderBy 如: lastUpdateTime desc 按修改日期日期倒序。
     * @Return:boolean
     */
    public boolean setOrderByIfAbsent(String orderBy) {
        if(this.page == null) {
            this.page = new SimplePage<>();
        }

        if(DataUtil.isNotEmpty(page.getOrderBy())) {
            return false;
        }

        page.setOrderBy(orderBy);

        return true;
    }
}
  1. 测试一下。
    3.1. 创建一个用户实体对象UserDto。
/**
 * @author: Max
 * @time: 2022/6/21
 * @description: 人员实体对象
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class UserDto implements Serializable {
    private static final long serialVersionUID = 1L;
    // 人员编号
    private String userCode;
    // 人员姓名
    private String userName;
}

3.2. 使用接口

import com.example.demo.testorg.dto.UserDto;
import com.example.demo.testorg.service.ITestService;
import com.example.demo.utils.PageQuery;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author: Max
 * @time: 2022/6/21
 * @description: 测试控制层
 */
@RestController
@RequestMapping("/api/test")
public class TestController {

    @Resource
    private ITestService iTestService;

    @RequestMapping(value="page",method= RequestMethod.POST)
    public PageQuery<UserDto> page(@RequestBody PageQuery<UserDto> page){
        return iTestService.page(page);
    }
}

3.3. 测试。在这里插入图片描述
3.4. 测试结果。

{
    "data": {
        "page": {
            "list": [
                {
                    "userCode": "111",
                    "userName": "老王"
                }
            ],
            "orderBy": "last_update_time desc",
            "pageNum": 1,
            "pageSize": 10,
            "pages": 10,
            "total": 1
        },
        "parameter": {
            "userCode": "111",
            "userName": "老王"
        }
    },
    "errorCode": "0",
    "errorMsg": "成功"
}

完事。
关注我,升职加薪就是你!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

徐先生Paul

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值