Mybatis uses paging plug-in for paging query

The plug-in is a third-party plug-in independent of the Mybatis framework

Add dependencies for the paging plugin

pom.xmladd in file

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

Configure plugin

Configure through the tag in the mybatismain configuration file ofmybatis-config.xmlplugins

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

Pagination example

Test class

@Test
public void queryStudentListByPluginPage() {
    
    
    StudentDao studentDao = MyBatisUtil.getMapper(StudentDao.class);    //sqlSession
    // 在sql查询之前先设置分页信息,分页信息设置给sqlSession
    // 设置之后,也就创建好了一个pageHelper的拦截器
    PageHelper.startPage(2,4);
    // mybatis在操作的时候就会检查有没有拦截器,
    // 若有,在查询的时候就会把对应的分页信息设置到List<Student>中
    List<Student> students = studentDao.queryStudentList();
    // 通过new PageInfo 方法,获取和分页有关的信息值
    PageInfo<Student> pageInfo = new PageInfo(students);
    List<Student> list = pageInfo.getList();
    for (Student student : list) {
    
    
        System.out.println(student);
    }
}

Guess you like

Origin blog.csdn.net/weixin_55556204/article/details/125314809