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
相关推荐
Dontla3 小时前
Mybatis Introduction (Java ORM Framework)
java·开发语言·mybatis
Knight_AL6 小时前
Spring Boot 多模块项目中优雅实现自动配置(基于 AutoConfiguration.imports)
java·spring boot·mybatis
indexsunny6 小时前
互联网大厂Java面试实战:从Spring Boot到微服务架构的三轮提问
java·spring boot·微服务·eureka·kafka·mybatis·spring security
Dontla7 小时前
boilerplate Introduction样板代码介绍(raw JDBC,Mybatis,productivity killer)
mybatis
没有bug.的程序员8 小时前
Spring Boot 数据访问:JPA 与 MyBatis 集成对比与性能优化深度解密
java·spring boot·性能优化·mybatis·jpa·集成对比
派大鑫wink8 小时前
【Day47】MyBatis 进阶:动态 SQL、关联查询(一对一 / 一对多)
数据库·sql·mybatis
派大鑫wink8 小时前
【Day48】MyBatis 注解开发:替代 XML 映射文件
xml·java·mybatis
小北方城市网10 小时前
JVM 调优实战指南:从 GC 频繁到性能稳定
jvm·数据库·spring boot·后端·mysql·mybatis
sin220119 小时前
MyBatis的执行流程
java·开发语言·mybatis
高山上有一只小老虎1 天前
mybatisplus实现分页查询
java·spring boot·mybatis