在數(shù)據(jù)庫服務(wù)器中,sql語句實現(xiàn)分頁便要每個查詢語句都要寫上limit(開始,結(jié)束),并且不能靈活的隨前端變化,為此使用攔截器的方法,過程:攔截器攔截請求的sql語句(根據(jù)需要攔截的id(正則匹配),進行攔截),并對根據(jù)前端傳過來的頁數(shù),和每頁的條數(shù),計算出limit(開始,結(jié)束),總條數(shù),然后,拼接到sql語句后邊。其中這個處理過程,已經(jīng)封裝到了,分頁插件中,可以不用理解,直接使用。
mybatis查詢分頁---使用pagehelper插件
之前在spring+springmvc由于整個大多都是xml的配置,在使用spring-boot后,需要進行分頁,
也希望能夠減少xml的配置以及新建很多分頁的相關(guān)類,找到了pagehelper這個插件,
分頁起來非常方便
page類
新建一個page< t > 用來接收分頁信息
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
/** * @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; } } |
引入依賴
1
2
3
4
5
|
<dependency> <groupid>com.github.pagehelper</groupid> <artifactid>pagehelper-spring-boot-starter</artifactid> <version> 1.2 . 3 </version> </dependency> |
springboot屬性文件增加pagehelper配置
這里使用的是yml(可讀性很強,也能少敲些鍵盤,愛護一下我們可愛的鍵盤^-^)的方式,
1
2
3
|
pagehelper: helperdialect: mysql reasonable: true |
使用pagehelper 進行分頁
public pageinfo<answerquestiondto> answerandquestiondetailbypage(page<answerquestiondto> page){//使用page保包裝我們的類
1
2
3
4
5
|
pagehelper.startpage(page.getpageno(),page.getpagesize()); //設(shè)置分頁相關(guān)值 list<answerquestiondto> answerquestiondtolist = answerandquestiondao.answerandquestiondetaillist(page.gett()); //查詢 pageinfo<answerquestiondto> pageinfo = new pageinfo<>(answerquestiondtolist); //包裝為分頁結(jié)果 return pageinfo; } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://segmentfault.com/a/1190000016201165