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

发布时间:2022-03-07 09:58:57 作者:iii
来源:亿速云 阅读:417

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

方法:

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

推荐阅读:
  1. mysql分页查询有什么作用
  2. MySQL实现分页查询的方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

mybatis plus

上一篇:SpringCloud服务注册中的nacos怎么实现

下一篇:如何解决Bioconductor安装软件比较慢的问题

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》