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

    测试效果:

  • 相关阅读:
    公号文章模板
    css 网格线
    刷题笔记-图-图的存储
    PAT Advanced A1021 Deepest Root (25) [图的遍历,DFS,计算连通分量的个数,BFS,并查集]
    PAT Advanced 1013 Battle Over Cities (25) [图的遍历,统计连通分量的个数,DFS,BFS,并查集]
    PAT Advanced 1076 Forwards on Weibo (30) [图的遍历,BFS,DFS]
    PAT Advanced 1034 Head of a Gang (30) [图的遍历,BFS,DFS,并查集]
    堆排序
    PAT Advanced 1155 Heap Paths (30) [DFS, 深搜回溯,堆]
    PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]
  • 原文地址:https://www.cnblogs.com/M87-A/p/15354257.html
Copyright © 2011-2022 走看看