# MyBatis-分页查询

在pom文件中引入依赖

         <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>6.1.1</version>
        </dependency>
1
2
3
4
5

在mybatis配置文件配置插件

    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor"/>
    </plugins>
1
2
3

1、需要在查询功能之前开启分页 PageHelper.startPage(int pageNum, int pageSize);

2、在查询功能之后获取分页相关信息

PageInfo<Emp> page = new PageInfo<>(list, 5); List表示分页数据 *5表示当前导航分页的数量

    @Test
    public void test() {
        try {
            InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml");
            EmpMapper mapper = new SqlSessionFactoryBuilder().build(inputStream).openSession().getMapper(EmpMapper.class);
            PageHelper.startPage(1, 2);
            List<Emp> emps = mapper.selectByExample(null);
            PageInfo<Emp> page=new PageInfo<>(emps,5);
            System.out.println(page);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
1
2
3
4
5
6
7
8
9
10
11
12
13
14

最近更新: 9/19/2026, 1:27:08 PM
MyBatis-分页查询

编程NOTE   |