首页

源码搜藏网

首页 > 开发教程 > java教程 >

MyBatis-Plus分页查询的实现示例

创建时间:2022-03-06 13:07  

方法:

使用selectPage()方法,
第一个参数是传入分页方法(传入当前页和当前显示多少条数据),
第二个参数是传入查询条件(如果查询全部的话,可以传null)。

MyBatis-Plus分页查询的实现示例

前提:

表中的数据为:

MyBatis-Plus分页查询的实现示例

第一种方式:

//分页查询
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println("数据为:"+employees.getRecords());
System.out.println("总数为:"+employees.getTotal()+",总页数为:"+employees.getPages());
System.out.println("当前页为:"+employees.getCurrent()+",每页限制:"+employees.getSize());

结果为:

MyBatis-Plus分页查询的实现示例

展示了所有的数据,也没有总数,并没有分页的效果。

第二种方式:

//分页查询
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
Integer count = employeeMapper.selectCount(null);
employees.setTotal(count);
System.out.println("数据为:"+employees.getRecords());
System.out.println("总数为:"+employees.getTotal()+",总页数为:"+employees.getPages());
System.out.println("当前页为:"+employees.getCurrent()+",每页限制:"+employees.getSize());

结果为:

MyBatis-Plus分页查询的实现示例

虽然有了总数和总页数,但依然没有分页的效果。

第三种方式:

//分页查询
Page<Employee> employees = employeeMapper.selectPage(new Page<>(3, 2), null);
System.out.println("数据为:"+employees.getRecords());
System.out.println("总数为:"+employees.getTotal()+",总页数为:"+employees.getPages());
System.out.println("当前页为:"+employees.getCurrent()+",每页限制:"+employees.getSize());

增加Mybatis-Plus插件,

@Configuration
public class MyBatisPlusConfig {
    @Bean
    public PaginationInterceptor paginationInterceptor(){
        PaginationInterceptor page = new PaginationInterceptor();
        return page;
    }
}

结果:

MyBatis-Plus分页查询的实现示例

到此这篇关于MyBatis-Plus 分页查询的实现示例的文章就介绍到这了,更多相关MyBatis-Plus 分页查询内容请搜索源码搜藏网以前的文章或继续浏览下面的相关文章希望大家以后多多支持源码搜藏网!

上一篇:MyBatis-PlusQueryWrapper及LambdaQueryWrapper的使用详解
下一篇:SpringBoot中Dozer的使用小结

相关内容

热门推荐