PageHelper paging plugin Mybatis paging MySQL paging

PageHelper

PageHelper is a paging plug-in for Mybatis, which supports multiple databases and multiple data sources. The paging query operation of the database can be simplified, and the integration process is also extremely simple, just introduce dependencies.

Code cloud address

The plugin currently supports physical paging of the following databases :

  1. Oracle
  2. Mysql
  3. MariaDB
  4. SQLite
  5. Hsqldb
  6. PostgreSQL
  7. DB2
  8. SqlServer(2005,2008)
  9. Informix
  10. H2
  11. SqlServer2012
  12. Derby
  13. Phoenix
  14. Dameng Database (dm)
  15. Alibaba Cloud PPAS database
  16. Supernatural power database
  17. HerdDB

Steps for usage

  1. First add dependencies in pom.xml
<!--分页插件 pageHelper-->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.12</version>
</dependency>
  1. If it is a MySQL database, it can be used without any configuration
  2. Test use
@Test
public void selectAllByLimit(){
    
    
    //初始化 currNo起始页码 和 PageSize页面容量
    PageHelper.startPage(1,5);
    //需要传入分页返回的对象 和 查询数据的接口  
    //这里使用的是MyBatis-Plus查询全部的接口
    PageInfo<User> userList = new PageInfo<>(userMapper.selectList(null));
    //返回的userList里边有很多你的需要的参数
    //getTotal总共几条数据
    System.out.println(userList.getTotal());
    //getPages一共有几页
    System.out.println(userList.getPages());
    //getList返回的数据集合
    userList.getList().forEach(System.out::println );
    //还有很多参数 不在一一举例
}

PageHelper common configuration

  • Configure in application.yml
pagehelper:
  # dialect: ①
  # 分页插件会自动检测当前的数据库链接,自动选择合适的分页方式(可以不设置)
  helper-dialect: mysql 
  # 上面数据库设置后,下面的设置为true不会改变上面的结果(默认为true)
  auto-dialect: true 
  page-size-zero: false # ②
  reasonable: true # ③
  # 默认值为 false,该参数对使用 RowBounds 作为分页参数时有效。(一般用不着)
  offset-as-page-num: false 
  # 默认值为 false,RowBounds是否进行count查询(一般用不着)
  row-bounds-with-count: false 
  #params: ④
  #support-methods-arguments: 和params配合使用,具体可以看下面的讲解
  # 默认值为 false。设置为 true 时,允许在运行时根据多数据源自动识别对应方言的分页
  auto-runtime-dialect: false # ⑤
  # 与auto-runtime-dialect配合使用
  close-conn: true 
  # 用于控制默认不带 count 查询的方法中,是否执行 count 查询,这里设置为true后,total会为-1
  default-count: false 
  #dialect-alias: ⑥

Guess you like

Origin blog.csdn.net/weixin_43420255/article/details/106102791