zoukankan      html  css  js  c++  java
  • Mybatis plus 分页插件使用

    1、导入依赖,注意版本问题

        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.4.11</version>
            <relativePath/>
        </parent>
    
        <modelVersion>4.0.0</modelVersion>
        <groupId>com.example</groupId>
        <artifactId>MybatisplusDemo</artifactId>
        <version>1.0-RELEASE</version>
    
        <properties>
            <java.version>11</java.version>
        </properties>
    
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.projectlombok</groupId>
                <artifactId>lombok</artifactId>
                <version>1.18.16</version>
                <scope>provided</scope>
            </dependency>
    
            <dependency>
                <groupId>mysql</groupId>
                <artifactId>mysql-connector-java</artifactId>
            </dependency>
    
            <!--mybatis plus和springboot整合-->
            <dependency>
                <groupId>com.baomidou</groupId>
                <artifactId>mybatis-plus-boot-starter</artifactId>
                <version>3.4.1</version>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

    2、配置分页插件

    @Configuration
    public class MybatisPlusPageConfig {
    
        /**
         * 旧版本配置(已过时)
         */
        /*@Bean
        public PaginationInterceptor paginationInterceptor(){
            return new PaginationInterceptor();
        }*/
    
        /**
         * 新的分页插件(推荐)
         */
        @Bean
        public MybatisPlusInterceptor mybatisPlusInterceptor() {
            MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
            interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
            return interceptor;
        }
    
    }

    3、编写测试案列

    需要自行搭建好工程,不会整合mybatis plus的同志,可以去学习一下!

    这里跳过整合过程,直接编写案列测试:

    @SpringBootTest(classes = MybatisplusDemoApp.class)
    @Log4j2
    public class TestQuery {
    
        @Autowired
        private Test3Dao test3Dao;
    
        @Test
        public void showTest3(){
            // current 1 表示当前页
            // size 2 表示分页大小
            Page<Test3> page = new Page<>(1,3);
            Page<Test3> test3Page = test3Dao.selectPage(page, new QueryWrapper<Test3>().orderByDesc("birthday"));
            log.info("总条数:{}",test3Page.getTotal());
            log.info("总页数:{}",test3Page.getPages());
            log.info("当前页:{}",test3Page.getCurrent());
            log.info("查询数据:{}",test3Page.getRecords());
        }
    }

    测试效果:

  • 相关阅读:
    docker
    iOS开发之抓取花瓣网json数据
    iOS与Android工程项目的简单对比
    Android与iOS的比较
    Jenkins时间和centos时间相差八小时解决方法
    MYSQL中索引里面的基数是什么意思
    使用Java写一个minio的客户端上传下载文件
    Java 使用AOP实现打印日志
    macOS使用brew安装mysql8.x
    MacOS安装homebrew报错:curl: (7) Failed to connect to raw.githubusercontent.com port 443: Connection refused
  • 原文地址:https://www.cnblogs.com/M87-A/p/15354257.html
Copyright © 2011-2022 走看看