1. 程式人生 > >springboot+Mybatis的分頁外掛pageHelper

springboot+Mybatis的分頁外掛pageHelper

1、在pom.xml加入pageHelper依賴包

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

ps:jar包可以在maven倉庫中下載,地址如下

https://mvnrepository.com/

2、建立一個名為pageBean的實體類

package com.zt.entity;

import java.util.List;

public class PageBean<T> {
        // 當前頁
        private Integer currentPage = 1;
        // 每頁顯示的總條數
        private Integer pageSize = 10;
        // 總條數
        private Integer totalNum;
        // 是否有下一頁
        private Integer isMore;
        // 總頁數
        private Integer totalPage;
        // 開始索引
        private Integer startIndex;
        // 分頁結果
        private List<T> items;

        public PageBean() {
            super();
        }

        public PageBean(Integer currentPage, Integer pageSize, Integer totalNum) {
            super();
            this.currentPage = currentPage;
            this.pageSize = pageSize;
            this.totalNum = totalNum;
            this.totalPage = (this.totalNum+this.pageSize-1)/this.pageSize;
            this.startIndex = (this.currentPage-1)*this.pageSize;
            this.isMore = this.currentPage >= this.totalPage?0:1;
        }

        public Integer getCurrentPage() {
            return currentPage;
        }

        public void setCurrentPage(Integer currentPage) {
            this.currentPage = currentPage;
        }

        public Integer getPageSize() {
            return pageSize;
        }

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

        public Integer getTotalNum() {
            return totalNum;
        }

        public void setTotalNum(Integer totalNum) {
            this.totalNum = totalNum;
        }

        public Integer getIsMore() {
            return isMore;
        }

        public void setIsMore(Integer isMore) {
            this.isMore = isMore;
        }

        public Integer getTotalPage() {
            return totalPage;
        }

        public void setTotalPage(Integer totalPage) {
            this.totalPage = totalPage;
        }

        public Integer getStartIndex() {
            return startIndex;
        }

        public void setStartIndex(Integer startIndex) {
            this.startIndex = startIndex;
        }

        public List<T> getItems() {
            return items;
        }

        public void setItems(List<T> items) {
            this.items = items;
        }

}

3、建立一個名為AbstractPageForm的類,用來設定分頁相關的引數

package com.zt.paging;

import com.github.pagehelper.PageHelper;

import java.io.Serializable;

public class AbstractPageForm<T extends AbstractPageForm<T>> implements Serializable {

        private static final long serialVersionUID = 1L;

        /**
         * @Description 頁碼為首頁
         */
        protected int pageNum = 1;

        /**
         * @Description 每頁顯示數量,預設為10
         */
        protected int pageSize = 10;

            public int getPageNum() {
            return pageNum;
        }

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

            public int getPageSize() {
            return pageSize;
        }

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

            /**
             * @Title enablePaging
             * @Description 啟用分頁
             * @return
             */
            @SuppressWarnings("unchecked")
            public final T enablePaging() {
            PageHelper.startPage(pageNum, pageSize);
            return (T) this;
        }
}

4、構造分頁方法

 public PageBean findAllUserWithPages(int currentPage,int pageSize){
        //設定分頁資訊,分別是當前頁數和每頁顯示的總記錄數【記住:必須在mapper介面中的方法執行之前設定該分頁資訊】
        PageHelper.startPage(currentPage,pageSize);
        //全部使用者資訊
        List<User> all = iUserMapper.findAll();
        //全部條數
        int allCount = iUserMapper.findAllCount();
        PageBean<User> pageData=new PageBean<User>(currentPage,pageSize,allCount);
        pageData.setItems(all);
        return pageData;
    }

mapper.xml檔案

 <select id="findAll" resultMap="BaseResultMap">
       select <include refid="a"/> from user
  </select>
 
 <select id="findAllCount" resultType="integer">
       select count(1) from user
  </select>
  
<sql id="a">
	 id,email,email_code,email_verify,gender,google_key,google_secret,google_verify,has_stop,
	 mobile_phone,name,password,pay_password,user_name,miner_info_id,parent_id,role_id
</sql>

注意:findAllCount方法需要加上結果型別,不然會出現如下異常:
在這裡插入圖片描述

5、在controller中實現分頁功能

package com.zt.controller;

import com.zt.entity.PageBean;
import com.zt.entity.User;
import com.zt.service.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/user")
public class UserController {
   @Autowired
   private UserServiceImpl userService;

    @RequestMapping("/findAll")
     public PageBean findAll(Integer currentPage,Integer pageSize){
     
	      if(Objects.nonNull(currentPage)&Objects.nonNull(pageSize)){
	          if(currentPage>0&pageSize>0){
	              return userService.findAllUserWithPages(currentPage,pageSize);
	          }
	          return userService.findAllUserWithPages(1,324);
	      }
	      return userService.findAllUserWithPages(1,324);
	    }
 }
   
/*傳入的引數最好使用Integer包裝類,以便我們進行邏輯判斷*/

6、測試結果

在這裡插入圖片描述
這就是Mybatis分頁外掛PageHelper的簡單應用啦!