一、基本信息

1、pom.xml

  1. <dependency>
  2. <groupId>com.baomidou</groupId>
  3. <artifactId>mybatis-plus-boot-starter</artifactId>
  4. <version>3.4.0</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>com.baomidou</groupId>
  8. <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
  9. <version>3.4.0</version>
  10. </dependency>

2、MybatisPlusConfig

  1. @Configuration
  2. public class MybatisPlusConfig {
  3. @Bean
  4. public ConfigurationCustomizer mybatisConfigurationCustomizer(){
  5. return configuration -> configuration.setObjectWrapperFactory(new MybatisMapWrapperFactory());
  6. }
  7. /**
  8. * 分页插件
  9. */
  10. @Bean
  11. public MybatisPlusInterceptor mybatisPlusInterceptor() {
  12. MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
  13. interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
  14. return interceptor;
  15. }
  16. }

二、Mybatis分页

1、实现

  1. IPage<?> page = new Page<>(currentPage,pageSize);
  2. IPage<?> iPage = mapper.getList(query, page);
  3. long total = iPage.getTotal();
  4. List<?> list = iPage.getRecords();

2、防坑

前端传过来第一页currentPage = 1,不需要做currentPage-1处理。

如果将currentPage-1处理后:
currentPage=1时,currentPage-1 = 0,但是iPage也会自动帮忙处理让page属性第一页等于1;
currentPage=2时,currentPage-1 = 1iPagepage属性第二页等于1;
所以这时候会遇到一个问题,第一页和第二页数据是相同的。