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
相关推荐
lclcooky2 小时前
Spring 中使用Mybatis,超详细
spring·tomcat·mybatis
椰汁菠萝2 小时前
Mybatis-plus + PostgreSQL json格式类型转换异常
postgresql·json·mybatis
TlYf NTLE6 小时前
Spring Boot3.3.X整合Mybatis-Plus
spring boot·后端·mybatis
qqacj8 小时前
SpringBoot【十一】mybatis-plus实现多数据源配置,开箱即用!
spring boot·后端·mybatis
希望永不加班8 小时前
SpringBoot 整合 MyBatis 完整实战
java·spring boot·后端·spring·mybatis
小红的布丁9 小时前
Redis 内存淘汰与过期策略
java·spring·mybatis
zuowei288910 小时前
SpringBootInvalid bound statement (not found)的原因和解决方案
mybatis
2601_9498144910 小时前
Spring-boot3.4最新版整合swagger和Mybatis-plus
mybatis
geBR OTTE11 小时前
Spring Boot中集成MyBatis操作数据库详细教程
数据库·spring boot·mybatis
计算机学姐13 小时前
基于SpringBoot的特色美食分享系统
java·vue.js·spring boot·后端·spring·tomcat·mybatis