Mybatis分页插件PageHelper的使用详解
作者:ganchuanpu 时间:2022-02-26 16:43:05
1.说明
如果你也在用Mybatis,建议尝试该分页插件,这个一定是最方便使用的分页插件。
该插件目前支持Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库分页。
2.使用方法
第一步:在Mybatis配置xml中配置 * 插件:
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
第二步:在代码中使用
1、设置分页信息:
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);
2、取分页信息
//分页后,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>,
Page<Country> listCountry = (Page<Country>)list;
listCountry.getTotal();
3、取分页信息的第二种方法
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectAll();
//用PageInfo对结果进行包装
PageInfo page = new PageInfo(list);
//测试PageInfo全部属性
//PageInfo包含了非常全面的分页属性
assertEquals(1, page.getPageNum());
assertEquals(10, page.getPageSize());
assertEquals(1, page.getStartRow());
assertEquals(10, page.getEndRow());
assertEquals(183, page.getTotal());
assertEquals(19, page.getPages());
assertEquals(1, page.getFirstPage());
assertEquals(8, page.getLastPage());
assertEquals(true, page.isFirstPage());
assertEquals(false, page.isLastPage());
assertEquals(false, page.isHasPreviousPage());
assertEquals(true, page.isHasNextPage());
来源:http://www.cnblogs.com/ganchuanpu/archive/2016/12/09/6147911.html
标签:mybatis,pagehelper
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java 请求跨域问题解决方法实例详解
2023-08-24 02:55:11
SpringBoot整合dataworks的实现过程
2023-11-29 12:13:09
spring webflux自定义netty 参数解析
2023-07-26 18:38:25
![](https://img.aspxhome.com/file/2023/1/57811_0s.jpg)
spring MVC中接口参数解析的过程详解
2023-11-28 09:17:50
![](https://img.aspxhome.com/file/2023/5/60525_0s.png)
深入学习Java编程中的字符串的进阶使用
2023-11-11 12:54:20
java.net.SocketException: Connection reset 解决方法
2023-06-17 01:28:43
![](https://img.aspxhome.com/file/2023/6/57416_0s.png)
springboot+mybatis-plus 两种方式打印sql语句的方法
2022-12-29 13:41:11
![](https://img.aspxhome.com/file/2023/9/64379_0s.jpg)
java生成验证码图片的方法
2023-12-09 08:57:15
Java设计模式之命令模式
2022-06-17 22:49:07
利用Spring Boot操作MongoDB的方法教程
2023-11-29 11:14:27
![](https://img.aspxhome.com/file/2023/5/60595_0s.jpg)
Springboot初始化项目并完成登入注册的全过程
2023-07-31 15:09:09
![](https://img.aspxhome.com/file/2023/8/57948_0s.png)
FeignClient如何脱离eureka自定义URL
2022-08-27 01:37:25
Java数据结构之数组(动力节点之Java学院整理)
2023-09-23 21:20:12
springmvc如何使用map接收参数
2023-11-28 04:49:17
![](https://img.aspxhome.com/file/2023/9/60289_0s.png)
Java二维数组与稀疏数组相互转换实现详解
2022-10-28 07:27:33
![](https://img.aspxhome.com/file/2023/0/63990_0s.png)
java中Statement 与 PreparedStatement接口之间的关系和区别
2023-11-25 03:11:11
![](https://img.aspxhome.com/file/2023/9/60029_0s.png)
SpringBoot应用启动流程源码解析
2023-11-25 00:03:24
Java泛型<T> T与T的使用方法详解
2021-07-12 10:58:42
关于Java8 parallelStream并发安全的深入讲解
2023-11-15 06:37:35
![](https://img.aspxhome.com/file/2023/5/59545_0s.png)
Java超详细讲解设计模式中的命令模式
2023-07-26 15:23:11