PageHelper結合Spring boot快速實現(xiàn)分頁查詢
MyBatis 分頁插件 PageHelper
如果你也在用 MyBatis买乃,建議嘗試該分頁插件坑赡,這一定是最方便使用的分頁插件笨蚁。分頁插件支持任何復雜的單表铡溪、多表分頁铝噩。
但在其官方文檔中衡蚂,現(xiàn)階段并沒有看到此插件與spring boot的結合使用說明。如何使用分頁插件 (pagehelper.github.io)
本篇博客實現(xiàn)Spring boot結合MyBatis 分頁插件 PageHelper實現(xiàn)分頁效果:
后端代碼:
創(chuàng)建實體類:(HistoryLog,不多贅述)
service層:
import java.util.List;
public interface HistoryLogService {
/**
* 分頁查詢接口
* 這里統(tǒng)一封裝了分頁請求和結果毛甲,避免直接引入具體框架的分頁對象, 如MyBatis或JPA的分頁對象
* 從而避免因為替換ORM框架而導致服務層年叮、控制層的分頁接口也需要變動的情況,替換ORM框架也不會
* 影響服務層以上的分頁接口玻募,起到了解耦的作用
*/
Page<HistoryLog> QueryHistory2();
}
mapper持久層:
import com.github.pagehelper.Page;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;
import java.util.List;
@Mapper
@Repository
public interface HistoryLogMapper {
// 分頁查詢用戶瀏覽日志
@Select("select content_id,people_id,hl_ip,date_format(create_date, '%Y-%c-%d %h:%i:%s' ) create_date from cms_history_log where people_id is not null ORDER BY create_date desc ")
@Results({
@Result(property = "content_id", column = "content_id"),
@Result(property = "people_id", column = "people_id"),
@Result(property = "hl_ip", column = "hl_ip"),
@Result(property = "create_date", column = "create_date")
})
Page<HistoryLog> queryHistoryLog2();
}
實現(xiàn)類:
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;
@Service
public class HistoryLogServiceImpl implements HistoryLogService {
@Autowired
HistoryLogMapper historyLogMapper;
@Override
public Page<HistoryLog> QueryHistory2() {
return historyLogMapper.queryHistoryLog2();
}
}
控制層:
package net.mingsoft.front.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import java.util.List;
@RestController
public class HistoryLogController {
@Autowired
HistoryLogServiceImpl historyLogServiceimpl;
// 分頁查詢
@RequestMapping("ms/userlog/pagehelper")
public Object getUserList2(int pagenum) {
PageHelper.startPage(pagenum, 15);//設置跳轉(zhuǎn)到第幾頁以及每頁顯示15條數(shù)據(jù)
List<HistoryLog> list = historyLogServiceimpl.QueryHistory2();
PageInfo<HistoryLog> pageInfo = new PageInfo<HistoryLog>(list);//將查詢數(shù)據(jù)存到PageInfo中
ModelAndView mv = new ModelAndView();
mv.addObject("historyLog",pageInfo);
mv.setViewName("../WEB-INF/manager/cms/userLog/index1.html");
return mv;
}
}
前端代碼:結合themeleaf模板只损,采用bootstrap組件
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>用戶瀏覽日志</title>
<link rel="stylesheet" >
<script src="https://cdn.staticfile.org/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.staticfile.org/twitter-bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="table-responsive">
<table class="table">
<!-- <caption>用戶瀏覽日志</caption>-->
<thead>
<tr>
<th>用戶ID</th>
<th>IP訪問地址</th>
<th>訪問的文章ID</th>
<th>創(chuàng)建時間</th>
</tr>
</thead>
<tbody>
<tr th:each="history,historyinfo:${historyLog.list}">
<td th:text="${history.people_id}">people_id</td>
<td th:text="${history.hl_ip}">hl_ip</td>
<td th:text="${history.content_id}">content_id</td>
<td th:text="${history.create_date}">create_date</td>
</tr>
</tbody>
</table>
</div>
<nav aria-label="Page navigation" style="text-align: center">
<ul class="pagination">
<li>
<a th:href="@{'http://localhost:8080/ms/userlog/pagehelper?pagenum=1'}" aria-label="Previous">
<span aria-hidden="true">«</span>
</a>
</li>
<li>
<a th:href="@{'http://localhost:8080/ms/userlog/pagehelper?pagenum='+${historyLog.prePage}}" aria-label="Previous">
<span aria-hidden="true"><</span>
</a>
</li>
</ul>
<ul class="pagination" th:each="page,pages:${historyLog.navigatepageNums}">
<li style="list-style: none;width: 34px;border: #337AB7"><a th:href="@{'http://localhost:8080/ms/userlog/pagehelper?pagenum='+${page}}" th:text="${page}"></a></li>
</ul>
<ul class="pagination">
<li>
<a th:href="@{'http://localhost:8080/ms/userlog/pagehelper?pagenum='+${historyLog.nextPage}}" aria-label="Previous">
<span aria-hidden="true">></span>
</a>
</li>
<li>
<a th:href="@{'http://localhost:8080/ms/userlog/pagehelper?pagenum='+${historyLog.pages}}" aria-label="Previous">
<span aria-hidden="true">»</span>
</a>
</li>
</ul>
</nav>
</body>
</html>
效果圖:
分析:
通過返回pageinfo的數(shù)據(jù)進行分析:
{
"total": 15, //總共顯示數(shù)據(jù)條數(shù)
"list": [ //顯示的數(shù)據(jù)
{
"id": 0,
"content_id": 1329315928002863000,
"people_id": 123123,
"hl_ip": "127.0.0.1",
"hl_is_mobile": null,
"update_date": null,
"update_by": null,
"create_date": "2022-3-29 08:30:30",
"create_by": null,
"del": 0
},
{
"id": 0,
"content_id": 1329315860382294000,
"people_id": 123123,
"hl_ip": "127.0.0.1",
"hl_is_mobile": null,
"update_date": null,
"update_by": null,
"create_date": "2022-3-29 08:30:28",
"create_by": null,
"del": 0
},
{
"id": 0,
"content_id": 1345972716284457000,
"people_id": 123123,
"hl_ip": "127.0.0.1",
"hl_is_mobile": null,
"update_date": null,
"update_by": null,
"create_date": "2022-3-29 08:30:26",
"create_by": null,
"del": 0
}
],
"pageNum": 1, //當前頁碼
"pageSize": 3, //每頁的數(shù)量
"size": 3, //當前頁的數(shù)量
"startRow": 1, //當前頁面第一個元素在數(shù)據(jù)庫中的行號
"endRow": 3, //當前頁面最后一個元素在數(shù)據(jù)庫中的行號
"pages": 5, //總頁數(shù)
"prePage": 0, //前一頁頁碼
"nextPage": 2, //下一頁頁碼
"isFirstPage": true, //是否為第一頁
"isLastPage": false, //是否為最后一頁
"hasPreviousPage": false, //是否有前一頁
"hasNextPage": true, //是否有下一頁
"navigatePages": 8, //導航頁碼數(shù)
"navigatepageNums": [ //所有導航頁號
1,
2,
3,
4,
5
],
"navigateFirstPage": 1, //導航欄第一頁 (導航欄最多8頁,可根據(jù)當前頁碼動態(tài)顯示)
"navigateLastPage": 5, //導航欄最后一頁
"lastPage": 5, //最后一頁
"firstPage": 1 //第一頁
}
注:此處pageinfo改為從第一條開始七咧,每頁顯示三條跃惫。
在分頁時,此插件會在持久層的sql 后面+limit 坑雅,通過拼接sql的方式使得在每次翻頁的時候會進行查詢把數(shù)據(jù)存放到pageinfo中辈挂。
總結:此插件中pageinfo的多種參數(shù)滿足了絕大多數(shù)的開發(fā)場景,使用spring boot結合此插件實現(xiàn)快速開發(fā)裹粤。