SpringBoot集成Mybatis分页插件

代码如下:

  1. pom.xml导入依赖
<!--                mysql驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>2.1.4</version>
        </dependency>
        <!--导入pagehelper相关依赖-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
  1. applicaiton.yml配置
# 配置pagehelper参数
pagehelper:
#  指定方言(数据库类型)
  helperDialect: mysql
  reasonable: true
#  方法参数
  supportMethodsArguments: true
  params: count=countSql
  1. 简单的测试
@RequestMapping("/pageTest")
    public String pageTest(Model model){
          
   
//        当前页,每页条数
        PageHelper.startPage(2,1);
        List<Account> accountList = accountMapper.findAll();
//      页面信息
        PageInfo<Account> pageInfo = new PageInfo<>(accountList);
        pageInfo.getPrePage();//上一页
        model.addAttribute("list",pageInfo.getList());
        return "pageTest";
    }
}
  1. 查看pageInfo源码可获得跟分页相关的参数如:
private int pageNum;
    private int pageSize;
    private int size;
    private long startRow;
    private long endRow;
    private int pages;
    private int prePage;
    private int nextPage;
    private boolean isFirstPage;
    private boolean isLastPage;
    private boolean hasPreviousPage;
    private boolean hasNextPage;
    private int navigatePages;
    private int[] navigatepageNums;
    private int navigateFirstPage;
    private int navigateLastPage;
经验分享 程序员 微信小程序 职场和发展