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
相关推荐
不恋水的雨12 小时前
mybatis-plus保存数据实现公共字段自动填充
mybatis
MegaDataFlowers12 小时前
基于EasyCode插件的SpringBoot和Mybatis框架快速整合以及PostMan的使用
spring boot·mybatis·postman
qingwufeiyang_53015 小时前
Mybatis-plus学习笔记1
笔记·学习·mybatis
毅炼19 小时前
MyBatis 常见问题总结
java·数据库·sql·mybatis
消失的旧时光-194319 小时前
Spring Boot 实战(四):MySQL + MyBatis 接入,打通用户注册最小闭环
spring boot·mysql·mybatis
行走的搬运工2 天前
Spring Security_05
java·spring·mybatis
无级程序员2 天前
Mybatis中保证时间戳的一致性
mybatis
希望永不加班2 天前
SpringBoot 自定义 Starter:从零开发一个私有 Starter
java·spring boot·后端·spring·mybatis
未秃头的程序猿3 天前
💥 MyBatis 面试连环炮:从源码原理到实战避坑,彻底拿下 Offer 通关秘籍
后端·面试·mybatis
A_QXBlms3 天前
企微群发消息技术实现:定时任务+模板消息
java·mybatis·企业微信