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
相关推荐
柒.梧.31 分钟前
SSM常见核心面试问题深度解析
java·spring·面试·职场和发展·mybatis
rabbit_pro4 小时前
Java使用Mybatis-Plus封装动态数据源工具类
java·python·mybatis
IT_Octopus8 小时前
java 实体属性 Map 解决 mybatis-plus wrapper selectone 查mysql json类型为null 问题
java·mysql·mybatis
Dolphin_Home9 小时前
MyBatis 核心属性详解笔记(由浅入深)
笔记·mybatis
一直都在5729 小时前
MyBatis入门:CRUD、参数处理与防 SQL 注入
java·sql·mybatis
while(1){yan}18 小时前
MyBatis Generator
数据库·spring boot·java-ee·mybatis
memgLIFE1 天前
mybatis数据库查询
数据库·oracle·mybatis
drebander1 天前
MyBatis-Plus saveBatch 在异步线程中事务未提交问题排查与修复
数据库·mybatis
super_lzb1 天前
mybatis拦截器ResultSetHandler详解
java·spring·mybatis·springboot
七夜zippoe1 天前
ORM框架下的SQL优化 N+1问题识别与解决方案
自动化·mybatis·jpa·n+1·batch fetching