1. 程式人生 > >Mybatis分頁查詢之pagehelper

Mybatis分頁查詢之pagehelper

引入pom依賴

<!--mybatis分頁查詢外掛-->
        <dependency>
            <groupId>com.github.pagehelper</groupId>
            <artifactId>pagehelper</artifactId>
            <version>5.0.0</version>
        </dependency>

配置mybatis-config.xml

<!-- mybatis分頁查詢 -->
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageInterceptor">
            <property name="helperDialect" value="Oracle"/>
        </plugin>
    </plugins>

編寫工具類TurnPage

package com.my.ssmmaven.util;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @author zhoujq
 * @version 1.0
 * @description
 * @date 2018/7/6 15:10
 * @modified
 */
public class TurnPage {

    /**
     * 分頁查詢
     * @param pageNum 當前頁
     * @param pageSize 每頁數量
     * @param list 被分頁的集合
     * @return
     */
    public static PageInfo page(int pageNum, int pageSize, List<JSONObject> list){
        PageHelper.startPage(pageNum,pageSize);
        PageInfo pageInfo = new PageInfo<>(list);
//        //總條數
//        pageInfo.getTotal();
//        //總頁數
//        pageInfo.getPages();
//        //資料詳情
//        pageInfo.getList();
//        //當前頁
//        pageInfo.getPageNum();
//        //當前頁資料量
//        pageInfo.getPageSize();
        return pageInfo;
    }
}

service層呼叫

/**
     * @param params
     * @return
     */
    @Override
    public JSONObject getTestList(JSONObject params) {
        JSONObject result = new JSONObject();
        List<JSONObject> list= getTestListMapper.getTestList(params);
        int pageNum=params.getInteger("pageNum");
        int pageSize=params.getInteger("pageSize");
        PageInfo pageInfo=TurnPage.page(pageNum,pageSize,list);
        //總頁數
        result.put("total",pageInfo.getTotal());
        //每頁資訊
        result.put("list",pageInfo);
        return result;
    }