欢迎光临
我们一直在努力

MyBatis-Plus分页查询的方法有哪些

本文小编为大家详细介绍“MyBatis-Plus分页查询的方法有哪些”,内容详细,步骤清晰,细节处理妥当,希望这篇“MyBatis-Plus分页查询的方法有哪些”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

方法:

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

前提:

表中的数据为:

第一种方式:

//分页查询
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());

结果为:

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

第二种方式:

//分页查询
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());

结果为:

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

第三种方式:

//分页查询
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分页查询的方法有哪些”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注云搜网行业资讯频道。

赞(0)
【声明】:本博客不参与任何交易,也非中介,仅记录个人感兴趣的主机测评结果和优惠活动,内容均不作直接、间接、法定、约定的保证。访问本博客请务必遵守有关互联网的相关法律、规定与规则。一旦您访问本博客,即表示您已经知晓并接受了此声明通告。