- 問題發(fā)現(xiàn)
PageHelper有時(shí)候有效果,有時(shí)候沒有效果
我在項(xiàng)目中查了好久,后來在網(wǎng)上也進(jìn)行了搜索,并發(fā)現(xiàn)了其中的原因,故記錄。 - 解決的辦法
1. PageHelper.startPage(1,10);只對該語句以后的第一個(gè)查詢語句得到的數(shù)據(jù)進(jìn)行分頁
2. 就算你在PageInfo pa = new PageInfo("",對象);語句里面的對象是寫的最終得到的數(shù)據(jù),該插件還是只會對第一個(gè)查詢所查詢出來的數(shù)據(jù)進(jìn)行分頁
第一個(gè)查詢語句是指什么呢?舉個(gè)例子吧,比如你有一個(gè)查詢數(shù)據(jù)的方法,寫在了PageHelper.startPage(1, 10);下面.但是這個(gè)查詢方法里面包含兩個(gè)查詢語句的話,該插件就只會對第一查詢語句查詢的數(shù)據(jù)進(jìn)行分頁,而不是對返回最終數(shù)據(jù)的查詢與基礎(chǔ)查詢出來的數(shù)據(jù)進(jìn)行分頁。
改變一下自己的代碼結(jié)構(gòu),讓最終需要的數(shù)據(jù)所需要的查詢語句放在PageHelper.startPage(1, 10)下面就行
但是
問題來了,有些數(shù)據(jù)不僅僅是一條查詢語句就能得到的,而是需要進(jìn)行數(shù)據(jù)組裝來獲得最終你想要的樣子,比如說,我自己代碼里遇到的問題
JSONArray shifts = tZyMiddleControlTruckMapper.queryShiftsByWharfAndTime(tmpMap);
for (int i = 0;i<shifts.size();i++) {
Map tmpMap2 = new HashMap();
JSONObject item = shifts.getJSONObject(i);
String shiftsGuid = item.getString("guid");
String shiftsName = item.getString("shiftsName");
String timeBegin = item.getString("timeBegin");
String timeEnd = item.getString("timeEnd");
tmpMap2.put("timeBegin",timeBegin);
tmpMap2.put("timeEnd",timeEnd);
JSONArray queryTruckSummaryNewOne = tZyMiddleControlTruckMapper.queryTruckSummaryNewOne(tmpMap2);
for (int j=0;j<queryTruckSummaryNewOne.size();j++) {
Map tmpMap3 = new HashMap();
JSONObject item1 = queryTruckSummaryNewOne.getJSONObject(j);
String washAmount = item1.getString("washAmount");
String loadingAndUnloadingAmount = item1.getString("loadingAndUnloadingAmount");
String truckGuid = item1.getString("truckGuid");
String licensePlateNo = item1.getString("licensePlateNo");
tmpMap3.put("washAmount",washAmount);
tmpMap3.put("loadingAndUnloadingAmount",loadingAndUnloadingAmount);
tmpMap3.put("truckGuid",truckGuid);
tmpMap2.put("truckGuid",truckGuid);
Map queryTruckSummaryNewTwo = tZyMiddleControlTruckMapper.queryTruckSummaryNewTwo(tmpMap2);
String facilStatus = (queryTruckSummaryNewTwo == null)?" ":String.valueOf(queryTruckSummaryNewTwo.get("facilStatus"));
String exitModel = (queryTruckSummaryNewTwo == null)?" ":String.valueOf(queryTruckSummaryNewTwo.get("exitModel"));
tmpMap3.put("licensePlateNo",licensePlateNo);
tmpMap3.put("facilStatus",facilStatus);
tmpMap3.put("exitModel",exitModel);
tmpMap3.put("shiftsGuid",shiftsGuid);
tmpMap3.put("shiftsName",shiftsName);
tmpList.add(tmpMap3);
}
}
PageInfo tmpListPageInfo = new PageInfo<>(tmpList);
List pageList = tmpListPageInfo.getList();
Long total = tmpListPageInfo.getTotal();
這里,tmpList是最終我的數(shù)據(jù),它是由三條查詢語句經(jīng)過for循環(huán)組裝而成的,這個(gè)地方我試了PageHelper,結(jié)果就是分頁不準(zhǔn)確
所以我用了別的辦法進(jìn)行分頁
int currentPage; //當(dāng)前第幾頁數(shù)據(jù)
int totalRecord = tmpList.size(); //一共多少條記錄
int totalPage = totalRecord % pageSize; //一共多少頁
if (totalPage > 0) {
totalPage = totalRecord / pageSize + 1;
} else {
totalPage = totalRecord / pageSize;
}
// 當(dāng)前第幾頁數(shù)據(jù)
currentPage = totalPage < pageNum ? totalPage : pageNum;
//起始索引
int fromIndex = pageSize * (currentPage - 1);
//結(jié)束索引
int toIndex = pageSize * currentPage > totalRecord ? totalRecord : pageSize * currentPage;
list = tmpList.subList(fromIndex,toIndex);
list就是最終你返回給前臺的數(shù)據(jù)
經(jīng)過測試,在幾萬條數(shù)據(jù)的情況下,這種分頁效果還是不錯(cuò)的。