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
相关推荐
丶意冷11 小时前
OceanBase 双模式识别与 MyBatis-Plus 分页方言自动切换实战
mybatis·oceanbase
lv__pf18 小时前
spring之整合mybatis【TL spring 12】
mysql·spring·mybatis
ChaHae-In2 天前
MyBatis动态SQL与MyBatis-Plus高效开发指南
数据库·oracle·mybatis
csdn2015_2 天前
springboot +mybatis 查询myql开启程序级别缓存
spring boot·缓存·mybatis
C++、Java和Python的菜鸟3 天前
第1章 项目前置课-Mybatis-Plus
mybatis
jaysee-sjc3 天前
【JavaWeb】Tlias智能学习辅助系统|后端Web实战(登录认证)
java·开发语言·前端·学习·mybatis
步行cgn3 天前
MyBatis 动态 SQL 完全指南
sql·tomcat·mybatis
步行cgn4 天前
错误:Invalid bound statement (not found) 详解与解决方案
mybatis
sugar__salt4 天前
MyBatis-Plus 从入门到实战:高效简化数据库开发的完整指南
数据库·spring boot·mybatis·数据库开发
bbq粉刷匠5 天前
手写 SQL 被一次 mvn generate 抹掉之后:我用一个 extension/ 子目录躲开 MyBatis 两个坑
数据库·sql·mybatis