通用分页查询

Java分页查询设计

要实现通用的分页查询要实现两个JavaBean

第一个是封装客户机带过来的分页请求信息

第二个是封装客户机要显示的分页信息

JavaBean的代码如下:

package cn.test.domain; //封装客户机带过来的分页请求信息 public class PageQuery { private int currentpage = 1; private int pagesize = 9; private String condition; //记住客户机带过来的查询条件 category_id private String value; //记住客户机带过来的查询条件的值 3 private int startindex; private String where; //where category_id=? public int getStartindex() { this.startindex = (this.currentpage-1)*this.pagesize; return startindex; } public String getWhere() { if(this.condition==null || this.condition.trim().equals("")){ this.where = null; }else{ this.where = "where " + condition + "=?"; } return where; } public int getCurrentpage() { return currentpage; } public void setCurrentpage(int currentpage) { this.currentpage = currentpage; } public int getPagesize() { return pagesize; } public void setPagesize(int pagesize) { this.pagesize = pagesize; } public String getCondition() { return condition; } public void setCondition(String condition) { this.condition = condition; } public String getValue() { return value; } public void setValue(String value) { this.value = value; } }

package cn.test.domain; import java.util.List; //封装客户机要显示的分页信息 public class PageBean { private List list; private int totalrecord; private int pagesize; private int totalpage; private int currentpage; private int previouspage; private int nextpage; private int[] pagebar; public List getList() { return list; } public void setList(List list) { this.list = list; } public int getTotalrecord() { return totalrecord; } public void setTotalrecord(int totalrecord) { this.totalrecord = totalrecord; } public int getPagesize() { return pagesize; } public void setPagesize(int pagesize) { this.pagesize = pagesize; } public int getTotalpage() { if(this.totalrecord%this.pagesize==0){ this.totalpage = this.totalrecord/this.pagesize; }else{ this.totalpage = this.totalrecord/this.pagesize + 1; } return totalpage; } public int getCurrentpage() { return currentpage; } public void setCurrentpage(int currentpage) { this.currentpage = currentpage; } public int getPreviouspage() { if(this.currentpage-1>0){ this.previouspage = this.currentpage-1; }else{ this.previouspage = 1; } return previouspage; } public int getNextpage() { if(this.currentpage+1>this.totalpage){ this.nextpage = this.totalpage; }else{ this.nextpage = this.currentpage + 1; } return nextpage; } public int[] getPagebar() { this.pagebar = new int[getTotalpage()]; for(int i=1;i<=this.totalpage;i++){ this.pagebar[i-1] = i; } return pagebar; } }



在MySql中可以很好的支持分页查询比如:

select * from book limit 0,9

这条sql语句代表的意思是从book表中第一行数据开始查,往下数九行,也就是每页显示九条数据,如果是第二页的话只改变第一个数值就可以了也就是“select * from book limit 9,9”

### 使用 MyBatis-Plus 实现通用分页查询实体 为了实现通用分页查询功能,MyBatis-Plus 提供了内置的支持工具类 `Page` 和接口方法。下面是一个完整的示例说明如何创建一个可以用于多种场景下的分页查询服务。 #### 创建分页查询的服务层逻辑 定义一个泛型化的分页查询方法,在此方法内部完成对不同业务对象的分页操作: ```java import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; public interface BaseService<T> { /** * 查询列表并返回分页结果 * * @param page 当前页码 * @param pageSize 页面大小 * @param wrapper 条件构造器 */ default Page<T> selectPageList(Integer page, Integer pageSize, QueryWrapper<T> wrapper) { // 构建分页参数实例 Page<T> pager = new Page<>(page, pageSize); // 执行数据库查询并将结果集封装到pager中 return this.baseMapper.selectPage(pager, wrapper); } } ``` 上述代码展示了怎样构建一个基础的服务接口,它接受三个主要输入参数:当前页数、每页显示记录数量以及查询条件[^1]。 #### 封装请求参数 对于前端传来的分页和筛选条件,通常会将其打包成一个专门的对象以便于传递给后台API端点。这里给出一个简单的例子展示如何设计这样的DTO (Data Transfer Object): ```java @Data @NoArgsConstructor @AllArgsConstructor public class PageQueryVO { private int pageNum; // 当前页号,默认第一页 private int pageSize; // 每页条目数,默认十条 // 可选字段,具体取决于实际需求 private String searchKey; // 关键字搜索项 } ``` 当接收到客户端发送过来的数据包后,就可以很容易地解析出所需的分页配置和其他过滤选项[^2]。 #### 编写 Mapper 层 SQL 映射文件中的动态SQL语句 如果涉及到复杂的多表关联或者其他高级特性,则可以在对应的XML映射文档里编写相应的<select>标签,并利用<if test="...">等结构化标签来控制最终执行的具体SQL命令形式。不过对于基本CRUD来说,大多数情况下不需要额外定制SQL脚本,因为框架已经提供了足够的默认行为支持。 综上所述,通过合理运用MyBatis-Plus所提供的组件和服务抽象层次,能够极大地简化开发过程中有关分页检索方面的编码工作量,同时也提高了系统的灵活性与扩展能力。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值