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
相关推荐
提笔了无痕4 小时前
什么是Redis的缓存问题,以及如何解决
数据库·redis·后端·缓存·mybatis
vx Biye_Design15 小时前
servlet宠物医院管理系统-计算机毕业设计源码77418
java·vue.js·spring·servlet·eclipse·mybatis
梁辰兴16 小时前
企业培训笔记:外卖平台后端--套餐管理模块--回显套餐信息
笔记·vue·mybatis·springboot·外卖管理系统
kkkkk0211061 天前
微服务学习笔记(黑马商城)
java·spring boot·spring·spring cloud·sentinel·mybatis·java-rabbitmq
夜幽青玄1 天前
mybatis-plus调用报 org.springframework.dao.DataIntegrityViolationException 错误处理
开发语言·python·mybatis
毕业设计制作和分享2 天前
springboot150基于springboot的贸易行业crm系统
java·vue.js·spring boot·后端·毕业设计·mybatis
SpiderPex2 天前
论MyBatis和JPA权威性
java·mybatis
瑞士卷@3 天前
MyBatis入门到精通(Mybatis学习笔记)
java·数据库·后端·mybatis
optimistic_chen3 天前
【Java EE进阶 --- SpringBoot】Mybatis - plus 操作数据库
数据库·spring boot·笔记·java-ee·mybatis·mybatis-plus
詩句☾⋆᭄南笙3 天前
Mybatis一对一、一对多
java·mybatis·resulttype·resultmap·一对多·一对一