Mybatis query paging – using the PageHelper plug-in

Time:2021-10-13

Mybatis query paging – using the PageHelper plug-in

Previously, in Spring + spring MVC, since the whole configuration is mostly XML, paging is required after using spring boot,
I also hope to reduce the configuration of XML and create many paging related classes. I found itpageHelperThis plug-in,
Paging is very convenient

Page class

Create a new page < T > to receive paging information

/**
 * @Author xuelongjiang
 */
public class Page<T> {

    private Integer pageNo = 0;

    private Integer pageSize = 10;

    private T t;

    public Integer getPageNo() {
        return pageNo;
    }

    public void setPageNo(Integer pageNo) {
        this.pageNo = pageNo;
    }

    public Integer getPageSize() {
        return pageSize;
    }

    public void setPageSize(Integer pageSize) {
        this.pageSize = pageSize;
    }

    public T getT() {
        return t;
    }

    public void setT(T t) {
        this.t = t;
    }
}

Introduce dependency


    <dependency>
        <groupId>com.github.pagehelper</groupId>
        <artifactId>pagehelper-spring-boot-starter</artifactId>
        <version>1.2.3</version>
    </dependency>
        

Add PageHelper configuration to springboot properties file

The YML (highly readable, less keyboard typing, and take care of our lovely keyboard ^ – ^) is used here,

    pagehelper:
        helperDialect: mysql
         reasonable: true

Paging with PageHelper

Public pageinfo < answerquestiondto > answerandquestiondetailbypage (page < answerquestiondto > page) {// wrap our class with page protection

        PageHelper.startPage(page.getPageNo(),page.getPageSize());// Set paging related values
        List<AnswerQuestionDTO> answerQuestionDTOList = answerAndQuestionDao.answerAndQuestionDetailList(page.getT());// query
        PageInfo<AnswerQuestionDTO> pageInfo = new PageInfo<>(answerQuestionDTOList);// Wrap as paging results
        return pageInfo;
}

Pay attention to my official account, read the interesting technical stories for the first time.
Code scanning attention:
Mybatis query paging - using the PageHelper plug-in

You can also search for the official account in WeChat, you can focus on me: codexiulian
Eager to grow and progress with you!

Recommended Today

The selector returned by ngrx store createselector performs one-step debugging of fetching logic

Test source code: import { Component } from ‘@angular/core’; import { createSelector } from ‘@ngrx/store’; export interface State { counter1: number; counter2: number; } export const selectCounter1 = (state: State) => state.counter1; export const selectCounter2 = (state: State) => state.counter2; export const selectTotal = createSelector( selectCounter1, selectCounter2, (counter1, counter2) => counter1 + counter2 ); // […]