pagehelper踩坑記之分頁亂套問題解決
正文
我們在使用數(shù)據(jù)庫進(jìn)行查詢時(shí),很多時(shí)候會用到分頁展示功能,因此除了像mybatis這樣的完善的orm框架之外,還有pagehelper這樣的插件幫助減輕我們的工作。
pagehelper的實(shí)現(xiàn)方式是,不需要我們?nèi)ゾ帉懛猪摯a,只需要調(diào)用一個(gè)分頁方法,出來的結(jié)果就是經(jīng)過分頁處理的。一來,我們的xml中的sql編寫就會靈活很多,二來,它可以幫我們規(guī)避各種不同類型的數(shù)據(jù)庫的分頁描述方式。所以,總體來說是個(gè)好事。
使用pagehelper遇到的坑說明
現(xiàn)象是這樣的:我們有一個(gè)場景是查詢數(shù)據(jù)庫表中的全量記錄返回給第三方,但是突然某一天發(fā)現(xiàn)第三方告警說我們給的數(shù)據(jù)不對了,比如之前會給到200條記錄的,某次只給到了10條記錄。
隨后我們推出了幾個(gè)猜想:
1. 第三方系統(tǒng)處理數(shù)據(jù)有bug,漏掉了一些數(shù)據(jù);
2. 數(shù)據(jù)庫被人臨時(shí)改掉過,然后又被復(fù)原了;
3. 數(shù)據(jù)庫bug,在全量select時(shí)可能不返回全部記錄;
其實(shí)以上猜想都顯得有點(diǎn)無厘頭,比如數(shù)據(jù)庫怎么可能有這種低級bug?但是人在沒有辦法的情況下只能胡猜一通了。最后終于發(fā)現(xiàn)是pagehelper的原因,因?yàn)榉猪搧y套了,復(fù)用了其他場景下的分頁設(shè)置,丟到數(shù)據(jù)庫查詢后返回了10條記錄;
pagehelper的至簡使用方式
本身pagehelper就是一個(gè)輔助工具類,所以使用起來一般很簡單。尤其在springboot中,只要引用starter類,依賴就可以滿足了。(如果是其他版本,則可能需要配置下mybatis的intercepter)
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-starter -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>${pagehelper.version}</version>
</dependency>在使用時(shí)只需要加上 Page.startPage(pageNum, pageSize) 即可。
public Object getUsers(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
List<UserEntity> list = userMapper.selectAllWithPage(null);
com.github.pagehelper.Page listWithPage = (com.github.pagehelper.Page) list;
System.out.println("listCnt:" + listWithPage.getTotal());
return list;
}而真正的sql里只需按沒有分頁的樣式寫一下就可以了。
<select id="selectAllWithPage" parameterType="java.util.Map"
resultType="com.my.mvc.app.dao.entity.UserEntity">
select * from t_users
</select>還是很易用的。少去了一些寫死的sql樣例。
pagehelper實(shí)現(xiàn)原理簡說
pagehelper不是什么高深的組件,實(shí)際上它就是一個(gè)mybatis的一個(gè)插件或者攔截器。是mybatis在執(zhí)行調(diào)用時(shí),將請求轉(zhuǎn)發(fā)給pagehelper處理,然后由pagehelper包裝分頁邏輯。
// com.github.pagehelper.PageInterceptor#intercept
@Override
public Object intercept(Invocation invocation) throws Throwable {
try {
Object[] args = invocation.getArgs();
MappedStatement ms = (MappedStatement) args[0];
Object parameter = args[1];
RowBounds rowBounds = (RowBounds) args[2];
ResultHandler resultHandler = (ResultHandler) args[3];
Executor executor = (Executor) invocation.getTarget();
CacheKey cacheKey;
BoundSql boundSql;
//由于邏輯關(guān)系,只會進(jìn)入一次
if (args.length == 4) {
//4 個(gè)參數(shù)時(shí)
boundSql = ms.getBoundSql(parameter);
cacheKey = executor.createCacheKey(ms, parameter, rowBounds, boundSql);
} else {
//6 個(gè)參數(shù)時(shí)
cacheKey = (CacheKey) args[4];
boundSql = (BoundSql) args[5];
}
checkDialectExists();
List resultList;
//調(diào)用方法判斷是否需要進(jìn)行分頁,如果不需要,直接返回結(jié)果
if (!dialect.skip(ms, parameter, rowBounds)) {
//判斷是否需要進(jìn)行 count 查詢
if (dialect.beforeCount(ms, parameter, rowBounds)) {
//查詢總數(shù)
Long count = count(executor, ms, parameter, rowBounds, resultHandler, boundSql);
//處理查詢總數(shù),返回 true 時(shí)繼續(xù)分頁查詢,false 時(shí)直接返回
if (!dialect.afterCount(count, parameter, rowBounds)) {
//當(dāng)查詢總數(shù)為 0 時(shí),直接返回空的結(jié)果
return dialect.afterPage(new ArrayList(), parameter, rowBounds);
}
}
resultList = ExecutorUtil.pageQuery(dialect, executor,
ms, parameter, rowBounds, resultHandler, boundSql, cacheKey);
} else {
//rowBounds用參數(shù)值,不使用分頁插件處理時(shí),仍然支持默認(rèn)的內(nèi)存分頁
resultList = executor.query(ms, parameter, rowBounds, resultHandler, cacheKey, boundSql);
}
return dialect.afterPage(resultList, parameter, rowBounds);
} finally {
if(dialect != null){
dialect.afterAll();
}
}
}如果沒有分頁邏輯需要處理,和普通的沒什么差別,如果有分頁請求,則會在原來的sql之上套上limit.. offset.. 之類的關(guān)鍵詞。從而完成分頁效果。
為什么pagehelper的分頁會亂套?
現(xiàn)在我們來說說為什么分頁會亂套?原因是 PageHelper.startPage(xx) 的原理是將分頁信息設(shè)置到線程上下文中,然后在隨后的查詢中使用該值,使用完成后就將該信息清除。
/**
* 開始分頁
*
* @param pageNum 頁碼
* @param pageSize 每頁顯示數(shù)量
* @param count 是否進(jìn)行count查詢
*/
public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count) {
return startPage(pageNum, pageSize, count, null, null);
}
/**
* 開始分頁
*
* @param pageNum 頁碼
* @param pageSize 每頁顯示數(shù)量
* @param count 是否進(jìn)行count查詢
* @param reasonable 分頁合理化,null時(shí)用默認(rèn)配置
* @param pageSizeZero true且pageSize=0時(shí)返回全部結(jié)果,false時(shí)分頁,null時(shí)用默認(rèn)配置
*/
public static <E> Page<E> startPage(int pageNum, int pageSize, boolean count, Boolean reasonable, Boolean pageSizeZero) {
Page<E> page = new Page<E>(pageNum, pageSize, count);
page.setReasonable(reasonable);
page.setPageSizeZero(pageSizeZero);
//當(dāng)已經(jīng)執(zhí)行過orderBy的時(shí)候
Page<E> oldPage = getLocalPage();
if (oldPage != null && oldPage.isOrderByOnly()) {
page.setOrderBy(oldPage.getOrderBy());
}
setLocalPage(page);
return page;
}
protected static final ThreadLocal<Page> LOCAL_PAGE = new ThreadLocal<Page>();
/**
* 設(shè)置 Page 參數(shù)
*
* @param page
*/
protected static void setLocalPage(Page page) {
LOCAL_PAGE.set(page);
}
// com.github.pagehelper.PageHelper#afterAll
@Override
public void afterAll() {
//這個(gè)方法即使不分頁也會被執(zhí)行,所以要判斷 null
AbstractHelperDialect delegate = autoDialect.getDelegate();
if (delegate != null) {
delegate.afterAll();
autoDialect.clearDelegate();
}
clearPage();
}
/**
* 移除本地變量
*/
public static void clearPage() {
LOCAL_PAGE.remove();
}那么什么情況下會導(dǎo)致分頁信息亂套呢?實(shí)際上就是線程變量什么情況會被亂用呢?
線程被復(fù)用的時(shí)候,將可能導(dǎo)致該問題。比如某個(gè)請求將某個(gè)線程設(shè)置了一個(gè)線程變量,然后隨后另一個(gè)請求復(fù)用了該線程,那么這個(gè)變量就被復(fù)用過去了。那么什么情況下線程會被復(fù)用呢?
一般是線程池、連接池等等。是的,大概就是這么原理了。
分頁問題復(fù)現(xiàn)
既然從理論上說明了這個(gè)問題,能否穩(wěn)定復(fù)現(xiàn)呢?咱們編寫下面的,很快就復(fù)現(xiàn)了。
@RestController
@RequestMapping("/hello")
@Slf4j
public class HelloController {
@Resource
private UserService userService;
// 1. 先請求該getUsers接口,將得到異常,pageNum=1, pageSize=1
@GetMapping("getUsers")
@ResponseBody
public Object getUsers(int pageNum, int pageSize) {
return userService.getUsers(pageNum, pageSize);
}
// 2. 多次請求該 getAllActors接口,正常情況下會得到N條全表記錄,但將會偶發(fā)地得到只有一條記錄,現(xiàn)象復(fù)現(xiàn)
@GetMapping("getAllActors")
@ResponseBody
public Object getAllActors() {
return userService.getAllActors();
}
}
@Service
@Slf4j
public class UserService {
@Resource
private UserMapper userMapper;
public Object getUsers(int pageNum, int pageSize) {
PageHelper.startPage(pageNum, pageSize);
// 此處強(qiáng)行拋出異常, 使以上 pagehelper 信息得以保存
throw new RuntimeException("exception ran");
}
public Object getAllActors() {
// 正常的全表查詢
List<ActorEntity> list = userMapper.selectAllActors();
return list;
}
}驗(yàn)證步驟及結(jié)果如下:(數(shù)據(jù)方面,自己隨便找一些表就好了)
// 步驟1: 發(fā)送請求: http://localhost:8081/hello/getUsers?pageNum=1&pageSize=1
// 步驟2: 發(fā)送請求: http://localhost:8081/hello/getAllActors
// 正常時(shí)返回
[{"actorId":1,"firstName":"PENELOPE","lastName":null,"lastUpdate":null},{"actorId":2,"firstName":"NICK","lastName":null,"lastUpdate":null},{"actorId":3,"firstName":"ED","lastName":null,"lastUpdate":null},{"actorId":4,"firstName":"JENNIFER","lastName":null,"lastUpdate":null},{"actorId":5,"firstName":"JOHNNY","lastName":null,"lastUpdate":null},{"actorId":6,"firstName":"BETTE","lastName":null,"lastUpdate":null},{"actorId":7,"firstName":"GRACE","lastName":null,"lastUpdate":null},{"actorId":8,"firstName":"MATTHEW","lastName":null,"lastUpdate":null},{"actorId":9,"firstName":"JOE","lastName":null,"lastUpdate":null},{"actorId":10,"firstName":"CHRISTIAN","lastName":null,"lastUpdate":null},{"actorId":11,"firstName":"ZERO","lastName":null,"lastUpdate":null},{"actorId":12,"firstName":"KARL","lastName":null,"lastUpdate":null},{"actorId":13,"firstName":"UMA","lastName":null,"lastUpdate":null},{"actorId":14,"firstName":"VIVIEN","lastName":null,"lastUpdate":null},{"actorId":15,"firstName":"CUBA","lastName":null,"lastUpdate":null},{"actorId":16,"firstName":"FRED","lastName":null,"lastUpdate":null},...
// 出異常時(shí)返回
[{"actorId":1,"firstName":"PENELOPE","lastName":null,"lastUpdate":null}]
以上,幾乎都可以復(fù)現(xiàn)該現(xiàn)象。實(shí)際上該問題由于tomcat的連接池復(fù)用導(dǎo)致的,本身和pagehelper關(guān)聯(lián)不是很大,但是在此處卻可能帶來比較大的影響。這也警示我們使用ThreadLocal 時(shí),一定要小心清理,否則將產(chǎn)生難以預(yù)料的結(jié)果。而且將很難排查。供諸君參考,更多關(guān)于pagehelper分頁亂套解決的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決
這篇文章主要介紹了SpringCloud 服務(wù)網(wǎng)關(guān)路由規(guī)則的坑及解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07
簡單談?wù)刯ava中final,finally,finalize的區(qū)別
Java中final、finally、finalize的區(qū)別與用法,困擾了不少學(xué)習(xí)者,下面我們就這個(gè)問題進(jìn)行一些探討,希望對大家的學(xué)習(xí)有所幫助。2016-05-05
Spring Security保護(hù)用戶密碼常用方法詳解
這篇文章主要介紹了Spring Security保護(hù)用戶密碼常用方法詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09
SpringBoot實(shí)現(xiàn)接口文檔自動生成的方法示例
在開發(fā)Web應(yīng)用程序時(shí),接口文檔是非常重要的一環(huán),本文主要介紹了SpringBoot實(shí)現(xiàn)接口文檔自動生成的方法示例,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10
詳解SpringBoot下文件上傳與下載的實(shí)現(xiàn)
這篇文章主要介紹了SpringBoot下文件上傳與下載的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
springboot注解Aspect實(shí)現(xiàn)方案
本文提供一種自定義注解,來實(shí)現(xiàn)業(yè)務(wù)審批操作的DEMO,不包含審批流程的配置功能。對springboot注解Aspect實(shí)現(xiàn)方案感興趣的朋友一起看看吧2022-01-01
java+mysql實(shí)現(xiàn)登錄和注冊功能
這篇文章主要為大家詳細(xì)介紹了java+mysql實(shí)現(xiàn)登錄和注冊功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-04-04
使用SpringSecurity+defaultSuccessUrl不跳轉(zhuǎn)指定頁面的問題解決方法
本人是用springsecurity的新手,今天遇到defaultSuccessUrl不跳轉(zhuǎn)指定頁面的問題,真是頭疼死了,網(wǎng)上找遍了解決方法都解決不了,今天給大家分享使用SpringSecurity+defaultSuccessUrl不跳轉(zhuǎn)指定頁面的問題解決方法,感興趣的朋友一起看看吧2023-12-12
Java?Lambda表達(dá)式常用的函數(shù)式接口
這篇文章主要介紹了Java?Lambda表達(dá)式常用的函數(shù)式接口,文章基于Java?Lambda表達(dá)式展開對常用的函數(shù)式接口的介紹,具有一的的參考價(jià)值需要的小伙伴可以參考一下2022-04-04

