使用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);
    }
}


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM