SpringBoot整合Pagehelper分页插件

在web开发中,数据的分页是必不可少的。Pagehelper分页插件很强大,虽说平时我们不需要用到它的很多功能,但是了解下还是有必要的。

官网:https://pagehelper.github.io/

注:在 MyBatis下使用。本节是在SpringBoot整合mybatis基础上进行演示。

一、引入依赖

<!-- 核心启动器, 包括auto-configuration、logging and YAML -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<!-- 数据库操作需要的mysql 驱动包 -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.48</version>
</dependency>
<!-- mybatis -->
<dependency>
    <groupId>org.mybatis.spring.boot</groupId>
    <artifactId>mybatis-spring-boot-starter</artifactId>
    <version>2.1.1</version>
</dependency>
<!-- pagehelper -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.13</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
</dependency>

二、application.properties

#这里要注意&,可能在spring的xml中我们用的是转义符号(&amp;),但是在这里不用
spring.datasource.url=jdbc:mysql://192.168.178.5:12345/mydb?characterEncoding=utf8&useUnicode=true&useSSL=false&serverTimezone=UTC
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource

####### mybatis #######
# 指定映射文件的具体位置
mybatis.mapper-locations=classpath:mapper/*.xml

####### pagehelper #######
# 默认情况下会使用 PageHelper 方式进行分页,如果想要实现自己的分页逻辑,
# 可以实现 Dialect(com.github.pagehelper.Dialect) 接口,然后配置该属性为实现类的全限定名称。
# 下面几个参数都是针对默认 dialect 情况下的参数。使用自定义 dialect 实现时(不推荐),下面的参数没有任何作用# 分页的数据库语言, oracle/mysql
pagehelper.helper-dialect=mysql
# 该参数对使用 RowBounds 作为分页参数时有效。 当该参数设置为 true 时,
# 会将 RowBounds 中的 offset 参数当成 pageNum 使用,可以用页码和页面大小两个参数进行分页。
pagehelper.offset-as-page-num= false
# 该参数对使用 RowBounds 作为分页参数时有效。
# 当该参数设置为true时,使用 RowBounds 分页会进行 count 查询。
pagehelper.row-bounds-with-count=false
# 当该参数设置为 true 时,如果 pageSize=0 或者 RowBounds.limit = 0
# 就会查询出全部的结果(相当于没有执行分页查询,但是返回结果仍然是 Page 类型)。
pagehelper.page-size-zero=false
# 分页合理化参数,默认值为false。当该参数设置为 true 时,pageNum<=0 时会查询第一页,
# pageNum>pages(超过总数时),会查询最后一页。默认false 时,直接根据参数进行查询。
pagehelper.reasonable=true
# 为了支持startPage(Object params)方法,增加了该参数来配置参数映射,
# 用于从对象中根据属性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,
# 不配置映射的用默认值,
# 默认值为pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。
pagehelper.params=pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
# 支持通过 Mapper 接口参数来传递分页参数,默认值false,分页插件会从查询方法的参数值中,
# 自动根据上面 params 配置的字段中取值,查找到合适的值时就会自动分页。
pagehelper.support-methods-arguments=
# 当使用运行时动态数据源或没有设置 helperDialect 属性自动获取数据库类型时,
# 会自动获取一个数据库连接, 通过该属性来设置是否关闭获取的这个连接,默认true关闭,
# 设置为 false 后,不会关闭获取的连接,这个参数的设置要根据自己选择的数据源来决定。
pagehelper.close-conn=true
# 默认值为 false。设置为 true 时,允许在运行时根据多数据源自动识别对应方言的分页
# (不支持自动选择sqlserver2012,只能使用sqlserver)
pagehelper.auto-runtime-dialect=true

三、PageHelper.startPage 静态方法调用

除了 PageHelper.startPage 方法外,还提供了类似用法的 PageHelper.offsetPage 方法。

在你需要进行分页的 MyBatis 查询方法前调用 PageHelper.startPage 静态方法即可,紧跟在这个方法后的第一个MyBatis 查询方法会被进行分页。

猜你喜欢

转载自www.cnblogs.com/myitnews/p/12349655.html