• mybatis-plus 分页插件


    目录

    1 前言 

     2 配置分页插件

    2.1 selectPage()测试

    2.2 自定义分页功能


    1 前言 

            大家之前肯定都用过PageHelper来进行分页,其实mybatisplus中也提供了一个分页插件PaginationInnerInterceptor,其实分页的本质就是内部封装了一个拦截器,对于满足条件的数据进行过滤处理。

     2 配置分页插件

    相关配置:

    1. @Configuration
    2. //扫描mapper接口所在的包
    3. @MapperScan("com.atguigu.mybatisplus.mapper")
    4. public class MyBatisPlusConfig {
    5. @Bean
    6. public MybatisPlusInterceptor mybatisPlusInterceptor(){
    7. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    8. //添加分页插件
    9. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
    10. return interceptor;
    11. }
    12. }

    注意:因为PaginationInnerInterceptor支持好几种数据库类型,DbType根据类型获取应使用的分页方案。

    2.1 selectPage()测试

    在mapper提供的API中就有进行分页的方法selectPage,一个是Page对象,一个是Wrapper条件构造器对象。(就是将用wrapper对象筛选出符合条件的数据,然后根据page对象进行分页)

    <P extends IPage> P selectPage(P page, @Param("ew") Wrapper queryWrapper);

    测试语句:

    1. @Test
    2. public void testPage(){
    3. Page page = new Page<>(2, 3);
    4. userMapper.selectPage(page, null);
    5. System.out.println("当前页数据:"+page.getRecords());
    6. System.out.println("总分页数量:"+page.getPages());
    7. System.out.println("总记录数量:"+page.getTotal());
    8. System.out.println("是否有下一页:"+page.hasNext());
    9. System.out.println("是否有上一页:"+page.hasPrevious());
    10. }

    控制台打印输出:

     可以发现,我们的Page对象中的输入了两个参数,一个当前页,一个每页条数。

    2.2 自定义分页功能

    有时候可能mybatisplus中mapper提供的API不足以满足我们从查询要求,那么此时就需要我们自定义一个分页

    mapper:

    1. /**
    2. * 通过年龄查询用户信息并分页
    3. * @param page MyBatis-Plus所提供的分页对象,必须位于第一个参数的位置
    4. * @param age
    5. * @return
    6. */
    7. Page selectPageVo(@Param("page") Page page, @Param("age") Integer age);

    注意:mybatis-plus提供的分页对象,必须位于第一个参数的位置。

    mapper.xml:

    1. <select id="selectPageVo" resultType="User">
    2. select uid,user_name,age,email from t_user where age > #{age}
    3. select>

    测试类:

    1. @Test
    2. public void testPageVo(){
    3. Page page = new Page<>(1, 3);
    4. userMapper.selectPageVo(page, 20);
    5. System.out.println(page.getRecords());
    6. System.out.println(page.getPages());
    7. System.out.println(page.getTotal());
    8. System.out.println(page.hasNext());
    9. System.out.println(page.hasPrevious());
    10. }

    控制台sql:

    1. ==> Preparing: SELECT COUNT(*) AS total FROM t_user WHERE age > ?
    2. ==> Parameters: 20(Integer)
    3. <== Columns: total
    4. <== Row: 90
    5. <== Total: 1
    6. ==> Preparing: select uid,user_name,age,email from t_user where age > ? LIMIT ?
    7. ==> Parameters: 20(Integer), 3(Long)
    8. <== Columns: uid, user_name, age, email
    9. <== Row: 1, ybc1, 21, null
    10. <== Row: 2, ybc2, 22, null
    11. <== Row: 3, ybc3, 23, null
    12. <== Total: 3
    13. Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@3a08078c]
    14. [User(id=null, name=null, age=21, email=null, isDeleted=null), User(id=null, name=null, age=22, email=null, isDeleted=null), User(id=null, name=null, age=23, email=null, isDeleted=null)]
    15. 30
    16. 90
    17. true
    18. false

    可以发现,我们在xml文件中写的sql并没有实现分页功能,而是在mapper文件中传输过来时已经帮我们实现好了。

  • 相关阅读:
    2D物理引擎 Box2D for javascript Games 第七章 子弹和感应器
    【拼题A】 520 钻石争霸赛 2023 题解
    jxTMS设计思想之流程引擎与任务分发
    探索NLP中的核心架构:编码器与解码器的区别
    MQ刷盘机制
    秒杀系统面临哪些技术难题
    kube-prometheus 系列1 项目介绍
    RabbitMQ忘记guestadmin 密码
    SpringBoot的shiro实现认证
    Linux下的的GDB调试技巧二 —— 基本功能
  • 原文地址:https://blog.csdn.net/qq_50652600/article/details/126120588