PageHelper介紹
- PageHelper是Github上有位開(kāi)發(fā)者寫(xiě)了一個(gè)分頁(yè)插件最域,可以很方便的添加到MyBatis的攔截器接口中。
Github項(xiàng)目地址
pom.xml添加依賴(lài)
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
配置文件編寫(xiě)
@Configuration
public class MyBatisConfiguration {
@Bean
public PageHelper pageHelper(){
PageHelper pageHelper=new PageHelper();
Properties p=new Properties();
// 當(dāng)該參數(shù)設(shè)置為 true 時(shí)锈麸,會(huì)將 RowBounds 中的 offset 參數(shù)當(dāng)成 pageNum 使用羡宙,可以用頁(yè)碼和頁(yè)面大小兩個(gè)參數(shù)進(jìn)行分頁(yè)。
p.setProperty("offsetAsPageNum","true");
// 當(dāng)該參數(shù)設(shè)置為true時(shí)掐隐,使用 RowBounds 分頁(yè)會(huì)進(jìn)行 count 查詢(xún)狗热。
p.setProperty("rowBoundsWithCount","true");
//當(dāng)該參數(shù)設(shè)置為 true 時(shí),pageNum<=0 時(shí)會(huì)查詢(xún)第一頁(yè)虑省, pageNum>pages(超過(guò)總數(shù)時(shí))匿刮,會(huì)查詢(xún)最后一頁(yè)。
p.setProperty("reasonable","true");
pageHelper.setProperties(p);
return pageHelper;
}
}
測(cè)試類(lèi)
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/queryStudentList")
public List<Student> queryStudentList(int pageNum,int pageSize){
//pageNum頁(yè)碼,pageSize條數(shù)
PageHelper.startPage(pageNum,pageSize);
return studentService.queryList();
}
}
測(cè)試
- 所有數(shù)據(jù)
所有數(shù)據(jù).png
- 分頁(yè)數(shù)據(jù)
分頁(yè)數(shù)據(jù).png
使用pagehelper-spring-boot-starter代替PageHelper
PageHelper-Spring-Boot-Starter 幫助你集成分頁(yè)插件到 Spring Boot比pagehelper更簡(jiǎn)單
替換依賴(lài)
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.1.0</version>
</dependency>
#替換為
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
yml文件配置
spring:
########################################################
###Druid -- mysql的數(shù)據(jù)庫(kù)配置.
########################################################
datasource:
druid:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
#下面為連接池補(bǔ)充設(shè)置應(yīng)用到上面所有數(shù)據(jù)源中
#初始化大小,最小,最大
initialSize: 5
minIdle: 5
maxActive: 20
#配置獲取連接等待超時(shí)的時(shí)間
maxWait: 60000
#配置間隔多久才進(jìn)行一次檢測(cè)探颈,檢測(cè)需要關(guān)閉的空閑連接熟丸,單位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一個(gè)連接在池中最小生存的時(shí)間,單位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打開(kāi)PSCache伪节,并且指定每個(gè)連接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置監(jiān)控統(tǒng)計(jì)攔截的filters光羞,去掉后監(jiān)控界面sql無(wú)法統(tǒng)計(jì),'wall'用于防火墻
filters: stat,wall,log4j
# 通過(guò)connectProperties屬性來(lái)打開(kāi)mergeSql功能怀大;慢SQL記錄
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
# 合并多個(gè)DruidDataSource的監(jiān)控?cái)?shù)據(jù)
useGlobalDataSourceStat: true
web-stat-filter:
url-pattern: /*
exclusions: /druid/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico
stat-view-servlet:
url-pattern: /druid/*
#登陸賬號(hào)密碼
login-username: guoadmin
login-password: guoadmin
#是否可以重置
reset-enable: false
#白名單
#allow: 127.0.0.1
#黑名單
#deny: 127.0.0.1
########################################################
###mybatis配置.
########################################################
mybatis:
mapperLocations: classpath:mybatis/*/*.xml
# 給實(shí)體類(lèi)起別名
type-aliases-package: com.springboot.backstage.entity
########################################################
###分頁(yè)插件
########################################################
pagehelper:
helper-dialect: mysql
#當(dāng)該參數(shù)設(shè)置為 true 時(shí)纱兑,pageNum<=0 時(shí)會(huì)查詢(xún)第一頁(yè), pageNum>pages(超過(guò)總數(shù)時(shí))化借,會(huì)查詢(xún)最后一頁(yè)潜慎。
reasonable: true
#支持通過(guò) Mapper 接口參數(shù)來(lái)傳遞分頁(yè)參數(shù),默認(rèn)值false蓖康,分頁(yè)插件會(huì)從查詢(xún)方法的參數(shù)值中铐炫,自動(dòng)根據(jù)上面 params 配置的字段中取值,查找到合適的值時(shí)就會(huì)自動(dòng)分頁(yè)
support-methods-arguments: true
#為了支持startPage(Object params)方法蒜焊,增加了該參數(shù)來(lái)配置參數(shù)映射倒信,用于從對(duì)象中根據(jù)屬性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable
params: count=countSql
測(cè)試
可以重復(fù)上面的測(cè)試方法看是否有效
PageHelper的參數(shù)詳解和PageInfo的使用
參數(shù)說(shuō)明
- helperDialect:分頁(yè)插件會(huì)自動(dòng)檢測(cè)當(dāng)前的數(shù)據(jù)庫(kù)鏈接泳梆,自動(dòng)選擇合適的分頁(yè)方式鳖悠。 你可以配置helperDialect屬性來(lái)指定分頁(yè)插件使用哪種方言唆迁。配置時(shí)拓颓,可以使用下面的縮寫(xiě)值:oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby
特別注意:使用 SqlServer2012 數(shù)據(jù)庫(kù)時(shí)卖陵,需要手動(dòng)指定為 sqlserver2012,否則會(huì)使用 SqlServer2005 的方式進(jìn)行分頁(yè)卑惜。
你也可以實(shí)現(xiàn) AbstractHelperDialect瘾带,然后配置該屬性為實(shí)現(xiàn)類(lèi)的全限定名稱(chēng)即可使用自定義的實(shí)現(xiàn)方法鼠哥。- offsetAsPageNum:默認(rèn)值為 false,該參數(shù)對(duì)使用 RowBounds 作為分頁(yè)參數(shù)時(shí)有效看政。 當(dāng)該參數(shù)設(shè)置為 true 時(shí)朴恳,會(huì)將 RowBounds 中的 offset 參數(shù)當(dāng)成 pageNum 使用,可以用頁(yè)碼和頁(yè)面大小兩個(gè)參數(shù)進(jìn)行分頁(yè)允蚣。
- rowBoundsWithCount:默認(rèn)值為false于颖,該參數(shù)對(duì)使用 RowBounds 作為分頁(yè)參數(shù)時(shí)有效。 當(dāng)該參數(shù)設(shè)置為true時(shí)嚷兔,使用 RowBounds 分頁(yè)會(huì)進(jìn)行 count 查詢(xún)森渐。
- pageSizeZero:默認(rèn)值為 false,當(dāng)該參數(shù)設(shè)置為 true 時(shí)冒晰,如果 pageSize=0 或者 RowBounds.limit = 0 就會(huì)查詢(xún)出全部的結(jié)果(相當(dāng)于沒(méi)有執(zhí)行分頁(yè)查詢(xún)同衣,但是返回結(jié)果仍然是 Page 類(lèi)型)。
- reasonable:分頁(yè)合理化參數(shù)壶运,默認(rèn)值為false耐齐。當(dāng)該參數(shù)設(shè)置為 true 時(shí),pageNum<=0 時(shí)會(huì)查詢(xún)第一頁(yè)蒋情, pageNum>pages(超過(guò)總數(shù)時(shí))埠况,會(huì)查詢(xún)最后一頁(yè)。默認(rèn)false 時(shí)棵癣,直接根據(jù)參數(shù)進(jìn)行查詢(xún)辕翰。
- params:為了支持startPage(Object params)方法,增加了該參數(shù)來(lái)配置參數(shù)映射浙巫,用于從對(duì)象中根據(jù)屬性名取值金蜀, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable刷后,不配置映射的用默認(rèn)值的畴, 默認(rèn)值為pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。
- supportMethodsArguments:支持通過(guò) Mapper 接口參數(shù)來(lái)傳遞分頁(yè)參數(shù)尝胆,默認(rèn)值false丧裁,分頁(yè)插件會(huì)從查詢(xún)方法的參數(shù)值中,自動(dòng)根據(jù)上面 params 配置的字段中取值含衔,查找到合適的值時(shí)就會(huì)自動(dòng)分頁(yè)煎娇。 使用方法可以參考測(cè)試代碼中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和 ArgumentsObjTest二庵。
- autoRuntimeDialect:默認(rèn)值為 false。設(shè)置為 true 時(shí)缓呛,允許在運(yùn)行時(shí)根據(jù)多數(shù)據(jù)源自動(dòng)識(shí)別對(duì)應(yīng)方言的分頁(yè) (不支持自動(dòng)選擇sqlserver2012催享,只能使用sqlserver),用法和注意事項(xiàng)參考下面的場(chǎng)景五哟绊。
closeConn:默認(rèn)值為 true因妙。當(dāng)使用運(yùn)行時(shí)動(dòng)態(tài)數(shù)據(jù)源或沒(méi)有設(shè)置 helperDialect 屬性自動(dòng)獲取數(shù)據(jù)庫(kù)類(lèi)型時(shí),會(huì)自動(dòng)獲取一個(gè)數(shù)據(jù)庫(kù)連接票髓, 通過(guò)該屬性來(lái)設(shè)置是否關(guān)閉獲取的這個(gè)連接攀涵,默認(rèn)true關(guān)閉,設(shè)置為 false 后洽沟,不會(huì)關(guān)閉獲取的連接以故,這個(gè)參數(shù)的設(shè)置要根據(jù)自己選擇的數(shù)據(jù)源來(lái)決定。
pageInfo的使用
在返回分頁(yè)的list后面創(chuàng)建一個(gè)pageInfo我們來(lái)看一下PageInfo返回了哪些參數(shù)
@RestController
public class StudentController {
@Autowired
private StudentService studentService;
@RequestMapping("/queryStudentList")
public PageInfo<Student> queryStudentList(int pageNum, int pageSize){
//pageNum頁(yè)碼,pageSize條數(shù)
PageHelper.startPage(pageNum,pageSize);
List<Student>list=studentService.queryList();
PageInfo<Student> pageInfo=new PageInfo<Student>(list);
return pageInfo;
}
}
查看返回參數(shù)
image.png
- endRow:當(dāng)前頁(yè)面最后一個(gè)元素在數(shù)據(jù)庫(kù)中的行號(hào)
- firstPage:第一頁(yè)
- hasNextPage:是否有下一頁(yè)
- hasPreviousPage:是否有前一頁(yè)
- isFirstPage:是否為第一頁(yè)
- isLastPage:是否為最后一頁(yè)
- lastPage:最后一頁(yè)
- list:結(jié)果集
- navigatepageNums:所有導(dǎo)航號(hào)
- nextPage:下一頁(yè)
- pageNum:當(dāng)前頁(yè)
- pageSize:每頁(yè)數(shù)量
- pages:總頁(yè)數(shù)
- prePage:前一頁(yè)
- size:當(dāng)前頁(yè)數(shù)量
- startRow:當(dāng)前頁(yè)面第一個(gè)元素在數(shù)據(jù)庫(kù)中的行號(hào)
- total:數(shù)據(jù)總數(shù)量