导入分页依赖

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

配置分页插件

<?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>

    <!-- 引入properties文件 -->
    <properties resource="jdbc.properties"/>

    <settings>
        <!-- 设置驼峰 -->
        <setting name="mapUnderscoreToCamelCase" value="true"/>
    </settings>

    <typeAliases>
        <!-- 全部有默认类名 -->
        <package name="com.xy.mybatis.com.xy.mybatis.cache.pojo"/>
    </typeAliases>

    <plugins>
        <!-- 配置分页插件 -->
        <plugin interceptor="com.github.pagehelper.PageInterceptor"></plugin>
    </plugins>

    <!-- 配置环境 default 默认环境  -->
    <environments default="development">
        <!-- 开发环境 -->
        <environment id="development">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>

        <!-- 测试环境 -->
        <environment id="test">
            <transactionManager type="JDBC"/>
            <dataSource type="POOLED">
                <property name="driver" value="${jdbc.driver}"/>
                <property name="url" value="${jdbc.url}"/>
                <property name="username" value="${jdbc.username}"/>
                <property name="password" value="${jdbc.password}"/>
            </dataSource>
        </environment>
    </environments>

    <mappers>
        <package name="com.xy.mybatis.cache.mapper" />
    </mappers>
</configuration>

测试使用

@Test
public void test01() {
   EmpMapper mapper = SqlSessionUtil.getSqlSession().getMapper(EmpMapper.class);
   // 开启分页,第2页,每页显示2条数据
   Page<Emp> page = PageHelper.startPage(2, 2);
   List<Emp> emps = mapper.getAllEmp();
   // 封装分页详细信息,显示分页栏(eg. 4 5 6 7 8)即其他信息
   PageInfo<Emp> empPageInfo = new PageInfo<>(page, 5);
   emps.forEach(System.out::println);
   System.out.println();
   System.out.println("page = " + page + "\n");
   System.out.println("empPageInfo = " + empPageInfo);
}

测试结果 image.png