分页插件源自: https://github.com/rosegun/mybatis-generator-pagination

使用方法

1. 加入maven依赖

  1. ...
  2. <dependency>
  3. <groupId>com.rosegun</groupId>
  4. <artifactId>mybatis-generator-pagination</artifactId>
  5. <version>1.0.1</version>
  6. </dependency>
  7. ...
  8. <!-- 插件依赖 -->
  9. <plugin>
  10. <groupId>org.mybatis.generator</groupId>
  11. <artifactId>mybatis-generator-maven-plugin</artifactId>
  12. <version>1.3.6</version>
  13. <configuration>
  14. <configurationFile>${basedir}/src/main/resources/generator/generatorConfig.xml</configurationFile>
  15. <overwrite>true</overwrite>
  16. <verbose>true</verbose>
  17. </configuration>
  18. <dependencies>
  19. <dependency>
  20. <groupId>mysql</groupId>
  21. <artifactId>mysql-connector-java</artifactId>
  22. <version>5.1.47</version>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.mybatis.generator</groupId>
  26. <artifactId>mybatis-generator-core</artifactId>
  27. <version>1.3.6</version>
  28. </dependency>
  29. <dependency>
  30. <groupId>com.rosegun</groupId>
  31. <artifactId>mybatis-generator-pagination</artifactId>
  32. <version>1.0.1</version>
  33. </dependency>
  34. </dependencies>
  35. </plugin>

2. generatorConfig.xml

  1. <plugin type="com.rosegun.plugin.MysqlLimitPlugin"/>

同时开启 selectByExample

3. java(Repository)

  1. public List<Contract> pageQueryContracts(int pageNum, int pageSize) {
  2. // 构建分页请求
  3. ContractDOExample example = new ContractDOExample();
  4. example.setPage(new Page(pageNum * pageSize, pageSize));
  5. List<ContractDO> contractDOS = contractDOMapper.selectByExample(example);
  6. if (CollectionUtils.isEmpty(contractDOS)) {
  7. logger.error("分页查询合约DO结果为空");
  8. return new ArrayList<>();
  9. }
  10. logger.debug("分页查询模板, contractDOS={}", contractDOS);
  11. return ModelListConverter.convertList(contractDOS, ContractConverter::convertDO2Model);
  12. }