PageHelper是针对Mybaits的分页插件,支持任何复杂的单表、多表分页。

以springboot为例,有两种方式配置,一种是传统的,引入依赖,编写配置类;一种是使用application.yml进行配置。

1.引入依赖

  1. <dependency>
  2.   <groupId>com.github.pagehelper</groupId>
  3.   <artifactId>pagehelper</artifactId>
  4.   <version>4.1.6</version>
  5. </dependency>

2.配置插件

  1. /**
  2. * @author Hanstrovsky
  3. */
  4. @Configuration
  5. public class MybatisConfig {
  6. @Bean
  7. public PageHelper pageHelper() {
  8. PageHelper pageHelper = new PageHelper();
  9. Properties properties = new Properties();
  10. properties.setProperty("offsetAsPageNum", "true");
  11. properties.setProperty("rowBoundsWithCount", "true");
  12. properties.setProperty("reasonable", "true");
  13. properties.setProperty("dialect", "mysql");
  14. pageHelper.setProperties(properties);
  15. return pageHelper;
  16. }
  17. }

1.引入依赖

  1. <!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
  2. <dependency>
  3. <groupId>com.github.pagehelper</groupId>
  4. <artifactId>pagehelper-spring-boot-starter</artifactId>
  5. <version>1.2.5</version>
  6. </dependency>

2.配置插件

  1. pagehelper:
  2. offset-as-page-num: true
  3. row-bounds-with-count: true
  4. page-size-zero: true
  5. reasonable: true
  6. auto-dialect: mysql
  1. public PageInfo<Student> findAll(Integer pageNum, Integer pageSize) {
  2. //设置分页信息
  3. PageHelper.startPage(pageNum, pageSize);
  4. List<Student> students = studentDao.findAll();
  5. PageInfo<Student> pageInfo = new PageInfo<>(students);
  6. //返回分页对象
  7. return pageInfo;
  8. }

如以上代码示范,分页对象中直接分装了与数据库映射的实体列表。但是在开发过程中很多时候都要进行对象的转换,将DO对象转换为DTO或者VO,加上或去掉一些属性。

可以这样做:

  1. /**
  2. * @author Hanstrovsky
  3. */
  4. @Service
  5. public class TestPage {
  6. @Autowired
  7. private StudentDao studentDao;
  8. public PageInfo<StudentVO> getAllStudent(Integer pageNum, Integer pageSize) {
  9. // 1. 开启分页
  10. PageHelper.startPage(pageNum, pageSize);
  11. // 2. 从数据库中查询出
  12. List<StudentDO> studentDos = studentDao.findAll();
  13. // 3. 这一步的作用主要是为了获取分页信息
  14. PageInfo studentDoPageInfo = new PageInfo<>(studentDos);
  15. // 4. 创建需要分页的VoList
  16. List<StudentVO> studentVos = new ArrayList<>();
  17. // 5. 对象转换
  18. for (StudentDO studentDO : studentDos) {
  19. StudentVO studentVO = new StudentVO();
  20. BeanUtils.copyProperties(studentDO, studentVO);
  21. studentVO.setClassName("六年级二班");
  22. studentVos.add(studentVO);
  23. }
  24. // 6.这一步的作用是将封装后的列表放到分页对象中
  25. studentDoPageInfo.setList(studentVos);
  26. return studentDoPageInfo;
  27. }
  28. }

这里有个细节,第3步,要先通过原list创建PageInfo对象,这样才能获取到分页的那些参数。之前想当然的试过先把Dolist转换为Volist,再创建pageInfo对象,那样做的话,并不能获取到分页信息。

其实之所以可以这样做,就巧在原对象的分页参数,和转换后的对象的分页参数是一致的,于是我们才可以狸猫换太子。

还有一种方式,也可以再创建一个pageInfo对象,将原pageInfo的参数拷贝过去,同样能达到目的,贴出来,供参考。

  1. public class PageUtils {
  2. public static <Do, Vo> PageInfo<Vo> convertPageInfo(PageInfo<Do> pageInfoDo) {
  3. // 创建Page对象,Page对象继承了ArrayList
  4. Page<Vo> page = new Page<>(pageInfoDo.getPageNum(), pageInfoDo.getPageSize());
  5. page.setTotal(pageInfoDo.getTotal());
  6. page.setStartRow(pageInfoDo.getStartRow());
  7. //... 等等信息,可以按需要挨个设置
  8. return new PageInfo<>(page);
  9. }
  10. }

版权声明:本文为hanstrovsky原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/hanstrovsky/p/12527022.html