springboot中使用mybatisplus自带插件实现分页

springboot中使用mybatisplus自带插件实现分页

1.导入mybatisplus分页依赖

<dependency>
            <groupid>com.baomidou</groupid>
            <artifactid>mybatis-plus-extension</artifactid>
            <version>3.4.3.1</version>
        </dependency>

2.添加MybatisPlusConfig配置类

@Configuration
@MapperScan("scan.your.mapper.package")
public class MybatisPlusConfig {

    /**
     * &#x65B0;&#x7684;&#x5206;&#x9875;&#x63D2;&#x4EF6;,&#x4E00;&#x7F13;&#x548C;&#x4E8C;&#x7F13;&#x9075;&#x5FAA;mybatis&#x7684;&#x89C4;&#x5219;,&#x9700;&#x8981;&#x8BBE;&#x7F6E; MybatisConfiguration#useDeprecatedExecutor = false &#x907F;&#x514D;&#x7F13;&#x5B58;&#x51FA;&#x73B0;&#x95EE;&#x9898;(&#x8BE5;&#x5C5E;&#x6027;&#x4F1A;&#x5728;&#x65E7;&#x63D2;&#x4EF6;&#x79FB;&#x9664;&#x540E;&#x4E00;&#x540C;&#x79FB;&#x9664;)
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.H2));
        return interceptor;
    }

    @Bean
    public ConfigurationCustomizer configurationCustomizer() {
        return configuration -> configuration.setUseDeprecatedExecutor(false);
    }
}

3.测试分页

@Test
void testPage(){
    //1&#x3001;&#x521B;&#x5EFA;page&#x5BF9;&#x8C61;
    //&#x4F20;&#x5165;&#x4E24;&#x4E2A;&#x53C2;&#x6570;&#xFF1A;&#x5F53;&#x524D;&#x9875; &#x548C; &#x6BCF;&#x9875;&#x663E;&#x793A;&#x7684;&#x8BB0;&#x5F55;&#x6570;
    Page<user> page = new Page<>(1,3);
    //&#x8C03;&#x7528;mp&#x5206;&#x9875;&#x67E5;&#x8BE2;&#x7684;&#x65B9;&#x6CD5;,null&#x4E3A;&#x6CA1;&#x6709;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;
    userMapper.selectPage(page,null);

     Page pageinfo = new Page(page,size);
        //&#x5E26;&#x6761;&#x4EF6;&#x67E5;&#x8BE2;
     QueryWrapper<实体类> wrapper = new QueryWrapper<>();
     Map<string, object> pageMap = new HashMap<>();
     pageMap.put("key", value);
     wrapper.eq("name", value);
     wrapper.allEq(pageMap);
     bbooksMapper.selectPage(pageinfo, wrapper);

    //&#x901A;&#x8FC7;page&#x5BF9;&#x8C61;&#x83B7;&#x53D6;&#x5206;&#x9875;&#x6570;&#x636E;
    System.out.println("&#x5F53;&#x524D;&#x9875;&#x7801;&#xFF1A;" + page.getCurrent());//&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x9875;
    System.out.println("&#x6570;&#x636E;&#x7684;list&#x96C6;&#x5408;&#xFF1A;" + page.getRecords());//&#x6BCF;&#x9875;&#x6570;&#x636E;&#x7684;list&#x96C6;&#x5408;
    System.out.println("&#x6BCF;&#x9875;&#x663E;&#x793A;&#x7684;&#x8BB0;&#x5F55;&#x6570;&#xFF1A;" + page.getSize());//&#x6BCF;&#x9875;&#x663E;&#x793A;&#x7684;&#x8BB0;&#x5F55;&#x6570;
    System.out.println("&#x603B;&#x8BB0;&#x5F55;&#x6570;&#xFF1A;" + page.getTotal());//&#x603B;&#x8BB0;&#x5F55;&#x6570;
    System.out.println("&#x603B;&#x9875;&#x6570;&#xFF1A;" + page.getPages());//&#x603B;&#x9875;&#x6570;

    System.out.println("&#x662F;&#x5426;&#x6709;&#x4E0B;&#x4E00;&#x9875;&#xFF1A;" + page.hasNext());//&#x662F;&#x5426;&#x6709;&#x4E0B;&#x4E00;&#x9875;
    System.out.println("&#x662F;&#x5426;&#x6709;&#x4E0A;&#x4E00;&#x9875;&#xFF1A;" + page.hasPrevious());//&#x662F;&#x5426;&#x6709;&#x4E0A;&#x4E00;&#x9875;
}
</string,></实体类></user>

图片:

springboot中使用mybatisplus自带插件实现分页

Original: https://www.cnblogs.com/b10100912/p/16726984.html
Author: 面向CV工程师
Title: springboot中使用mybatisplus自带插件实现分页

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/684923/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球