MyBatis分页插件PageHelper的使用

1.在pom.xml中添加依赖

<dependency>
            <groupId>com.github.pagehelpergroupId>
            <artifactId>pagehelperartifactId>
            <version>5.1.10version>
dependency>
<dependency>
            <groupId>com.github.jsqlparsergroupId>
            <artifactId>jsqlparserartifactId>
            <version>2.0version>
dependency>

2.在mybatis-config.xml中添加配置


    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">

            <property name="helperDialect" value="mysql"/>

            <property name="reasonable" value="true"/>
        plugin>
    plugins>

3.调用PageHelper.startPage

 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分页

select * from table limit 10,20;

5.Oracle分页

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分页

select top 3 * from table
where 
	id not in
	(select top 15 id from table)

7.SQL Server 2012+

select * from table order by id
	offset 4 rows fetch next 5 rows only

你可能感兴趣的:(mybatis)