Mybatis分页插件pagehelper

pageheler官方介绍

1. 项目pom依赖配置

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

2. mybatis-config.xml配置

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>    
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="helperDialect" value="db2"/>
            <!--page < 1时,返回第一页,pageNum > 总页时,返回最后一页-->
            <property name="reasonable" value="true"/>
        </plugin>
    </plugins>
</configuration>

3. 分页

//调用查询之前,先设置分页
PageHelper.startPage(1,10); //调用查询,获得第一页,10条记录 List<Map<String,Object>> originResults = query(cussAutoValidationQueryUtil); //为了获取查询的总条数 Page<Map<String,Object>> p = (Page<Map<String,Object>>) originResults; long globalTotalCompareNums = p.getTotal();

4. 注意

pagehelper在查询数据库为db2时,会在查询结果里增加一个ROW_ID字段

原文地址:https://www.cnblogs.com/darange/p/9669630.html