程序员Java

mybatis-plus分页

2020-09-21  本文已影响0人  GG_lyf

前言

  之前在学mybatis-plus的时候没用过分页,今天补上来。


开搞

  1.先导包(两个)

    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>mybatis-plus-boot-starter</artifactId>
      <version>3.3.2</version>
    </dependency>
    <dependency>
      <groupId>com.alibaba</groupId>
      <artifactId>druid</artifactId>
      <version>1.1.22</version>
    </dependency>

  2.yml配置

mybatis-plus:
  #type-aliases-package: org.vector.domain
  configuration:
    map-underscore-to-camel-case: false # 关闭驼峰命名
    #log-impl: org.apache.ibatis.logging.stdout.StdOutImpl #mybatis-plus配置控制台打印完整带参数SQL语句

  3.写个config用来解决分页插件

@Configuration
@ConditionalOnClass(value = {PaginationInterceptor.class})
public class PageConfig {
  @Bean
  public PaginationInterceptor paginationInterceptor() {
    return new PaginationInterceptor();
  }
}

  4.Mapper层什么都不用写,直接在Service层搞

  public List<Warlock> queryWarlockForPage(int page, int size) {
    IPage<Warlock> warlockPage = new Page<>(page, size);//page当前页,size每页条数
    warlockPage = warlockDao.selectPage(warlockPage, null);
    return warlockPage.getRecords();
  }

  5.Controller层

   @GetMapping("/pages/{page}/{size}")
  public List<Warlock> queryWarlockForPage(@PathVariable String page, @PathVariable String size) {
    return warlockService.queryWarlockForPage(Integer.parseInt(page), Integer.parseInt(size));
  }

  6.测试


success
上一篇下一篇

猜你喜欢

热点阅读