1. 程式人生 > >也許是 mybaits 史上最簡單的分頁排序方式

也許是 mybaits 史上最簡單的分頁排序方式

mybaits + pageHelper + 自己封裝的分頁 實現 分頁排序

1 請求示例

{url}?pageSize=5&pageNum=1&orderBy=ljyhbj_desc

1.1 引數說明

pageSize: 分頁尺寸

pageNum: 當前頁數

orderBy: 排序條件, 多個的話 按照下劃線隔開 如: price_desc,hot_asc

1.2 Controller 層

@PostMapping("/xxx")
    public Result overdueList(MyPageInfo myPageInfo) {
        PageInfo overdueList = reportService.getOverdueList(myPageInfo);
        return
Result.success(overdueList); }

1.3 Service 層

    @Override
    public PageInfo overdueList(MyPageInfo myPageInfo) {
        PageHelperUtil.startPage(myPageInfo);

        ... 執行業務邏輯 獲取 list 

        PageInfo pagetResult = new PageInfo(list);
    return pagetResult;

1.4 結果

你沒看錯就這麼簡單就完成了分頁!!!

返回結果示例
返回結果

2 工具類

2.1 MyPageInfo

package cn.istarfinancial.common;

import java.util.ArrayList;

/**
 * @Author: huangwenjun
 * @Description:
 * @Date: Created in 10:35  2018/4/13
 **/
public class MyPageInfo {

    private static final Integer DEFALUT_PAGENUM = 1;
    private static final Integer DEFALUT_PAESIZE = 10;

    private Integer pageSize;

    private Integer pageNum;

    /**
     * 排序欄位: price_asc,...
     */
    private String orderBy;

    public Integer getPageSize() {
        if (pageSize == null) {
            return DEFALUT_PAESIZE;
        }

        return pageSize;
    }

    public Integer getPageNum() {
        if (pageNum == null) {
            return DEFALUT_PAGENUM;
        }

        return pageNum;
    }

    /**
     * input: price_asc,...
     * out: price asc,...
     * @return
     */
    public String[] getOrderBy() {
        if (orderBy != null) {
            String[] orderBys = orderBy.split(",");
            ArrayList<String> orderByArrList = new ArrayList<>();

            if (orderBys.length > 0) {
                for (int i = 0; i < orderBys.length; i++) {
                    String orderBy = orderBys[i];
                    orderBy = orderBy.replace("_asc", " asc").replace("_desc", " desc");

                    orderByArrList.add(orderBy);
                }

                if (orderByArrList.size() >= 1) {
                    String[] orderByCondition = new String[orderByArrList.size()];
                    orderByArrList.toArray(orderByCondition);
                    return orderByCondition;
                }
            }
        }

        return new String[0];
    }

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

    public void setPageNum(Integer pageNum) {
        this.pageNum = pageNum;
    }

    public void setOrderBy(String orderBy) {
        this.orderBy = orderBy;
    }
}

2.2 PageHelperUtil

package cn.istarfinancial.utils;

import cn.istarfinancial.common.MyPageInfo;
import com.github.pagehelper.PageHelper;

/**
 * @Author: huangwenjun
 * @Description:
 * @Date: Created in 12:46  2018/4/13
 **/
public class PageHelperUtil {

    public static void startPage(MyPageInfo myPageInfo) {
        PageHelper.startPage(myPageInfo.getPageNum(), myPageInfo.getPageSize());
        String[] orderBy = myPageInfo.getOrderBy();

        if (orderBy.length >= 1) {
            for (int i = 0; i < orderBy.length; i++) {
                PageHelper.orderBy(orderBy[i]);
            }
        }
    }
}

返回結果

{
    "code": "0000",
    "msg": "成功",
    "data": {
        "pageNum": 1,
        "pageSize": 2,
        "size": 2,
        "orderBy": "ljyhbj desc",
        "startRow": 1,
        "endRow": 2,
        "total": 18,
        "pages": 9,
        "list": [
            {
                "contract_no": "CON-20180329-00001",
                "ljyqznj": 1500,
                "current_duration": 3,
                "odd_corpus": 3333.34,
                "loan_complete_price": 9400,
                "interest_date": 1530178816,
                "ljyqbj": 9999.99,
                "duration_type": 2,
                "moneylender": "1",
                "ljyqglf": 550,
                "contract_price": 10000,
                "ljyqts": 6,
                "ljyhbj": 6666.66,
                "product_name": "xingxindai",
                "duration": 3,
                "card_owner_name": "測試3.20",
                "product_type": "xingxindai",
                "rest_periods": 0,
                "loan_complete_time": 1522316764,
                "id": 2,
                "ljyqlx": 200,
                "serial_no": "XJD-20180320-00001",
                "status": 3
            },
            {
                "contract_no": "CON-20180319-00003",
                "ljyqznj": 1050,
                "current_duration": 5,
                "odd_corpus": 6666.68,
                "loan_complete_price": 9400,
                "interest_date": 1534591872,
                "ljyqbj": 9999.96,
                "duration_type": 2,
                "moneylender": "1",
                "ljyqglf": 2350,
                "contract_price": 10000,
                "ljyqts": 6,
                "ljyhbj": 3333.32,
                "product_name": "xingxindai",
                "duration": 12,
                "card_owner_name": "樂塵",
                "product_type": "xingxindai",
                "rest_periods": 7,
                "loan_complete_time": 1521706232,
                "id": 1,
                "ljyqlx": 650,
                "serial_no": "XJD-20180319-00001",
                "status": 1
            }
        ],
        "firstPage": 1,
        "prePage": 0,
        "nextPage": 2,
        "lastPage": 8,
        "isFirstPage": true,
        "isLastPage": false,
        "hasPreviousPage": false,
        "hasNextPage": true,
        "navigatePages": 8,
        "navigatepageNums": [
            1,
            2,
            3,
            4,
            5,
            6,
            7,
            8
        ]
    }
}