• MyBatis-Plus分页插件和使用Mapper文件


    一、分页插件

    官方文档-分页插件:https://baomidou.com/pages/97710a/#paginationinnerinterceptor

    1、注入分页插件

    我们也编写一个配置类,注入分页插件。

    @Configuration
    public class MyBatisPlusConfig {
    
        /**
         * 注册插件
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
    
            // 1.添加分页插件
            PaginationInnerInterceptor pageInterceptor = new PaginationInnerInterceptor();
            // 设置数据库方言类型
            pageInterceptor.setDbType(DbType.MYSQL);
            // 下面配置根据需求自行设置
            // 设置请求的页面大于最大页后操作,true调回到首页,false继续请求。默认false
            pageInterceptor.setOverflow(false);
            // 单页分页条数限制,默认无限制
            pageInterceptor.setMaxLimit(500L);
    
            interceptor.addInnerInterceptor(pageInterceptor);
            return interceptor;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    然后就可以使用了。

    二、分页查询

    官方文档-分页查询:https://baomidou.com/pages/49cc81/#page

    // 无条件分页查询
    IPage<T> page(IPage<T> page);
    
    // 条件分页查询
    IPage<T> page(IPage<T> page, Wrapper<T> queryWrapper);
    
    // 无条件分页查询
    IPage<Map<String, Object>> pageMaps(IPage<T> page);
    
    // 条件分页查询
    IPage<Map<String, Object>> pageMaps(IPage<T> page, Wrapper<T> queryWrapper);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    注意:IPage接口只用一个实现类Page。

    1、分页查询-返回DO

    	@Test
    	public void testPage() {
    		System.out.println(("----- 分页 method test ------"));
    		/**
    		 * 两个参数:
    * current的值默认是1,从1开始,不是0。
    * size是每一页的条数。
    */
    Page<UserDO> page = new Page<>(2, 4); Page<UserDO> userPage = userService.page(page, null); System.out.println("当前页:" + userPage.getCurrent()); System.out.println("总页数:" + userPage.getPages()); System.out.println("记录数:" + userPage.getTotal()); System.out.println("是否有上一页:" + userPage.hasPrevious()); System.out.println("是否有下一页:" + userPage.hasNext()); System.out.println("所有记录数据如下:"); userPage.getRecords().forEach(System.out::println); }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    在这里插入图片描述

    三、使用Mapper文件

    1、指定Mapper文件目录

    在yml配置文件中指定 mapper文件位置。然后创建mapper文件,和之前使用 Mybatis一样。

    mybatis-plus:
      ## 默认mapper文件在 classpath*:/mapper/**/*.xml。可自定义
      mapper-locations: classpath:/mybatis/mapper/**/*.xml
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    2、分页查询-返回VO

    1) 在 Mapper接口上定义方法

    Page<UserVO>  pageSearch(@Param("page") IPage page, @Param("userName") String userName);
    
    • 1

    2)在 Mapper文件上定义sql

      <select id="pageSearch" resultType="com.charge.learn.mybatis.plus.vo.UserVO">
        select id, user_name userName, age, height, email, create_time createTime, update_time updateTime
        from t_user
        where user_name like concat('%',concat(#{userName},'%'))
      select>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3)测试

        /**
         * 使用xml 分页查询
         */
        @Test
        public void testPageSearch() {
            System.out.println(("----- 使用xml 分页查询 pageSearch method test ------"));
            Page<UserVO> page = new Page<>(2, 4);
            Page<UserVO> pageVOList = userMapper.pageSearch(page, "赵云");
    
            System.out.println("当前页:" + pageVOList.getCurrent());
            System.out.println("总页数:" + pageVOList.getPages());
            System.out.println("记录数:" + pageVOList.getTotal());
            System.out.println("是否有上一页:" + pageVOList.hasPrevious());
            System.out.println("是否有下一页:" + pageVOList.hasNext());
            System.out.println("所有VO记录数据如下:");
            pageVOList.getRecords().forEach(System.out::println);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    在这里插入图片描述

    四、使用注解

    直接在 Mapper接口上使用对应的注解即可。

    1、查询

    1) 在 Mapper接口上使用 @Select注解。

        @Select("select id, user_name userName, age, height, email, create_time\n" +
                "    from t_user\n" +
                "    where id = #{id}")
        UserVO selectVOByPrimaryKey(Long id);
    
    • 1
    • 2
    • 3
    • 4

    2)测试:

        /**
         * 使用注解 查询
         */
        @Test
        public void testSelectVOByPrimaryKey2() {
            System.out.println(("----- 使用注解 查询 selectVOByPrimaryKey method test ------"));
            UserVO userVO = userMapper.selectVOByPrimaryKey(6L);
    
            System.out.println("userVO = " + userVO);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    在这里插入图片描述

    2、分页查询-返回VO

    1) 在 Mapper接口上使用 @Select注解

        @Select("select id, user_name userName, age, height, email, create_time createTime, update_time updateTime\n" +
                "    from t_user\n" +
                "    where user_name like concat('%',concat(#{userName},'%'))")
        Page<UserVO>  pageVOSearch(@Param("page") IPage page, @Param("userName") String userName);
    
    • 1
    • 2
    • 3
    • 4

    测试和上面类似。

    这里推荐两个插件。上面的附结果图主要是更直观的了解SQL的执行情况。

    在这里插入图片描述

    – 求知若饥,虚心若愚。

  • 相关阅读:
    用同一棵红黑树实现map和set【STL】
    Guava中常用Object方法-equals与null比较、hashCode、自定义toString、自定义compareTo排序
    一文熟悉 Go 的基础语法和基本数据类型
    centerOS下docker 搭建IotDB集群
    vue组件中的data为什么必须是一个函数?
    ChatGPT 串接到 Discord - 团队协作好助理
    游戏资讯查询易语言代码
    确保企业物联网部署安全的5个基本步骤
    bulkTransfer发送数据丢包
    关于proTable的一些配置
  • 原文地址:https://blog.csdn.net/qq_42402854/article/details/126203461