MyBatis分页插件PageHelper的使用

1.在pom.xml中添加依赖

xml 复制代码
<dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.1.10</version>
</dependency>
<dependency>
            <groupId>com.github.jsqlparser</groupId>
            <artifactId>jsqlparser</artifactId>
            <version>2.0</version>
</dependency>

2.在mybatis-config.xml中添加配置

xml 复制代码
<!--    启用Pagehelper分页插件-->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
<!--            设置数据库类型-->
            <property name="helperDialect" value="mysql"/>
<!--            分页合理化-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>

3.调用PageHelper.startPage

java 复制代码
 session=MyBatisUtils.openSession();
//            startPage方法会自动将下一次查询进行分页
            PageHelper.startPage(2,10);
            Page<Goods> page=(Page)session.selectList("goods.selectPage");
            System.out.println("总页数"+page.getPages());
            System.out.println("总记录数"+page.getTotal());
            System.out.println("开始行号"+page.getStartRow());
            System.out.println("结束行号"+page.getEndRow());
            System.out.println("当前页码"+page.getPageNum());
            List<Goods> data=page.getResult();//当前页数据
            for(Goods g:data){
                System.out.println(g.getTitle());
            }

4.MySQL分页

sql 复制代码
select * from table limit 10,20;

5.Oracle分页

sql 复制代码
select t3.* from(
	select t2.*,rownum as row_num from(
		select * from table order by id asc
	)t2 where rownum<=20
)t3
where t2.row_num>11

6.SQL Server 2000分页

sql 复制代码
select top 3 * from table
where 
	id not in
	(select top 15 id from table)

7.SQL Server 2012+

sql 复制代码
select * from table order by id
	offset 4 rows fetch next 5 rows only
相关推荐
我登哥MVP17 小时前
Spring Boot 从“会用”到“精通”:Model-Map原理
java·spring boot·后端·spring·servlet·maven·mybatis
Full Stack Developme18 小时前
MyBatis-Plus 分页使用详解
mybatis
落木萧萧82519 小时前
MyBatisGX 批量操作:比 MyBatis-Plus 和 MyBatis-Flex 更好用、更快
mybatis·orm
Jabes.yang19 小时前
Java电商订单系统面试全流程解析:接口设计、数据库、微服务与分布式事务实战
java·微服务·mybatis·分布式事务·电商·订单系统·接口设计
cheems952720 小时前
[开发日记]Spring Boot + MyBatis-Plus 抽奖系统开发复盘:从奖品创建、活动校验到前端圈选人员失效的一次完整排障
前端·spring boot·mybatis
就叫_这个吧2 天前
Java+MySQL+Mybatis+Junit4实现学生信息管理系统
java·mysql·mybatis
噢,我明白了2 天前
MyBatis-Plus 中IPage的分页查询
java·mybatis
basketball6162 天前
Redis基础:3. Redis 持久化(重要)
redis·bootstrap·mybatis
Knight_AL2 天前
MyBatis 报错:Parameter ‘xxx‘ not found 的原因与解决方案
java·tomcat·mybatis
我登哥MVP2 天前
Spring Boot 从“会用”到“精通”:Converter 原理
java·spring boot·servlet·maven·mybatis·converter