使用PageHelper插件分页时,如何对对象进行转换以及添加属性

一、插件介绍

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

二、基本用法

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

第一种

1.引入依赖

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>4.1.6</version>
</dependency>

2.配置插件

/**
 * @author Hanstrovsky
 */
@Configuration
public class MybatisConfig {
    @Bean
    public PageHelper pageHelper() {
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("dialect", "mysql");
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}

第二种

1.引入依赖

<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-starter</artifactId>
    <version>1.2.5</version>
</dependency>

2.配置插件

pagehelper:
  offset-as-page-num: true
  row-bounds-with-count: true
  page-size-zero: true
  reasonable: true
  auto-dialect: mysql

分页示范

public PageInfo<Student> findAll(Integer pageNum, Integer pageSize) {
        //设置分页信息
        PageHelper.startPage(pageNum, pageSize);
        List<Student> students = studentDao.findAll();
        PageInfo<Student> pageInfo = new PageInfo<>(students);
    	//返回分页对象
        return pageInfo;
    }

三、对象转换

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

可以这样做:

/**
 * @author Hanstrovsky
 */
@Service
public class TestPage {
    @Autowired
    private StudentDao studentDao;

    public PageInfo<StudentVO> getAllStudent(Integer pageNum, Integer pageSize) {
        // 1. 开启分页
        PageHelper.startPage(pageNum, pageSize);
        // 2. 从数据库中查询出
        List<StudentDO> studentDos = studentDao.findAll();
        // 3. 这一步的作用主要是为了获取分页信息
        PageInfo studentDoPageInfo = new PageInfo<>(studentDos);
        // 4. 创建需要分页的VoList
        List<StudentVO> studentVos = new ArrayList<>();
        // 5. 对象转换
        for (StudentDO studentDO : studentDos) {
            StudentVO studentVO = new StudentVO();
            BeanUtils.copyProperties(studentDO, studentVO);
            studentVO.setClassName("六年级二班");
            studentVos.add(studentVO);
        }
        // 6.这一步的作用是将封装后的列表放到分页对象中
        studentDoPageInfo.setList(studentVos);
        return studentDoPageInfo;
    }
}

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

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

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

public class PageUtils {
    public static <Do, Vo> PageInfo<Vo> convertPageInfo(PageInfo<Do> pageInfoDo) {
        // 创建Page对象,Page对象继承了ArrayList
        Page<Vo> page = new Page<>(pageInfoDo.getPageNum(), pageInfoDo.getPageSize());
        page.setTotal(pageInfoDo.getTotal());
        page.setStartRow(pageInfoDo.getStartRow());
        //... 等等信息,可以按需要挨个设置
        return new PageInfo<>(page);
    }
}
原文地址:https://www.cnblogs.com/hanstrovsky/p/12527022.html