• pagehelper 使用


    • MySQL对分页的支持

    简单来说MySQL对分页的支持是通过limit子句。请看下面的例子。

    limit关键字的用法是
    LIMIT [offset,] rows
    offset是相对于首行的偏移量(首行是0),rows是返回条数。
    
    # 每页10条记录,取第一页,返回的是前10条记录
    select * from tableA limit 0,10;
    # 每页10条记录,取第二页,返回的是第11条记录,到第20条记录,
    select * from tableA limit 10,10;
    • Mybatis分页插件PageHelper

    1、POM依赖

    这里用5.1.4

            <dependency>
                <groupId>com.github.pagehelper</groupId>
                <artifactId>pagehelper</artifactId>
                <version>5.1.4</version>
            </dependency>

    2、Mybatis对PageHelper的配置

    打开Mybatis配置文件,一般在Resource路径下。

        <plugins>
            <!-- 5.0.0之前使用com.github.pagehelper.PageHelper,以后使用com.github.pagehelper.PageInterceptor作为拦截器; -->
            <plugin interceptor="com.github.pagehelper.PageInterceptor">
    
                <!-- 设置数据库类型Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库,5.0.0之后不需配置有冲突 -->
                <!-- <property name="dialect" value="mysql"/> -->
    
            </plugin>
        </plugins>

    3、使用

    调用对应的mapper方法之前

    写上PageHelper.startPage([page],[size])

    3、常用文档

    startPage参数:

    pageNum页码

    pageSize每页显示条数

    pageSizeZero默认false,当设置为true的时候,如果pagesize设置为0,就不执行分页,返回全部结果

    orderBy排序,列"create_time asc"

    reasonable 分页合理化, 3.3.0版本可用- 默认false禁用 , 启用合理化时,如果pageNum<1会查询第一页,如果pageNum>pages会查询最后一页 禁用合理化时,如果pageNum<1或pageNum>pages会返回空数据

    写法:

    统计总数和分页 Page page = PageHelper.startPage(pageNum, pageSize, true);  true表示需要统计总数,这样会多进行一次请求select count(0); 省略掉true参数只返回分页数据。 

    只统计总数 PageHelper.startPage(1,-1);

    只分页不统计(每次只执行分页语句) PageHelper.startPage([pageNum],[pageSize]);

    使用PageHelper查全部(不分页)PageHelper.startPage(1,0);

    官方文档:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

    其他文章:https://blog.csdn.net/Soinice/article/details/80592675

  • 相关阅读:
    AVR开发 Arduino方法(六) 内存子系统
    AVR开发 Arduino方法(五) 模数转换子系统
    AVR开发 Arduino方法(四) 串行通信子系统
    AVR开发 Arduino方法(三) 定时/计数器子系统
    AVR开发 Arduino方法(二) 中断子系统
    2014.5.17—所谓生活,就是让自己变得更好
    2014.5.10—做事分清时间地点
    2014.5.7—社交网络用户心理分析
    2014.5.7—20岁这几年
    2014.5.5—反向绑定域名,无需工信部备案
  • 原文地址:https://www.cnblogs.com/ooo0/p/9092142.html
Copyright © 2020-2023  润新知