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());
        }
    }

    测试效果:

  • 相关阅读:
    php反射
    html video api控件总结
    linux CentOS7.2安装ffmpeg-3.0.2
    2019年7月12日星期五(C语言)
    2019年7月11日星期四(C语言)
    2019年7月10日星期三(C语言)
    2019年7月9日星期二(C语言)
    2019年7月8日星期一(C语言)
    2019年7月5日星期五(C语言)
    2019年7月4日星期四(C语言及LINUX命令)
  • 原文地址:https://www.cnblogs.com/M87-A/p/15354257.html
Copyright © 2011-2022 走看看