1.1 pagehelper介紹和使用
PageHelper是一款好用的開源免費的Mybatis第三方物理分頁插件。
原本以為分頁插件,應該是很簡單的,然而PageHelper比我想象的要復雜許多,它做的很強大,也很徹底,強大到使用者可能並不需要這么多功能,徹底到一參可以兩用。
1.1.1 springboot下使用
- 依賴包導入(這里划重點!!!有坑!!)
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>最新版本</version>
</dependency>
這是從官網上的標准形式,但是注意了。pagehelper與springboot集成的話,不能用這個包,這樣會導致分頁失效!
正確的是下面這種導入方式:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.3</version>
</dependency>
可以看到跟springboot集成的包大多數后面都是以spring-boot-starter結尾。。。。
剛開始我用的第一種,代碼也全都正確,就是沒有分頁效果。。。這坑!!
- User
public class User {
private Integer id;
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", password='" + password + '\'' +
", uid=" + uid +
", admin=" + admin +
", email='" + email + '\'' +
", phonenumber=" + phonenumber +
", classid=" + classid +
'}';
}
private String name;
private String password;
private Integer uid;
private Integer admin;
private String email;
private Integer phonenumber;
private Integer classid;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name == null ? null : name.trim();
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password == null ? null : password.trim();
}
public Integer getUid() {
return uid;
}
public void setUid(Integer uid) {
this.uid = uid;
}
public Integer getAdmin() {
return admin;
}
public void setAdmin(Integer admin) {
this.admin = admin;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email == null ? null : email.trim();
}
public Integer getPhonenumber() {
return phonenumber;
}
public void setPhonenumber(Integer phonenumber) {
this.phonenumber = phonenumber;
}
public Integer getClassid() {
return classid;
}
public void setClassid(Integer classid) {
this.classid = classid;
}
}
- UserMapper
@Repository
public interface UserMapper {
long countByExample(UserExample example);
int deleteByExample(UserExample example);
int insert(User record);
int insertSelective(User record);
List<User> selectAllUser();
User selectByExample(UserExample example);
User selectBynameANDpassword(String name,String password);
int updateByExampleSelective(@Param("record") User record, @Param("example") UserExample example);
int updateByExample(@Param("record") User record, @Param("example") UserExample example);
}
這里只用selectAllUser()這個方法就行了!
- UserMapper.xml
<select id="selectAllUser" resultType="com.hifix.springbootmyfirstproject.pojo.User">
select *from user
</select>
鄙人還是比較喜歡這種xml的方式聲明sql,並不是覺得注解的不好,只是覺得這種方式使得代碼的整體可讀性更好!
- controller
//測試分頁信息
@RequestMapping("/UserInfo")
public String presentUser(Model model,
@RequestParam(required = false,defaultValue="1",value="pageNum")Integer pageNum,
@RequestParam(defaultValue="2",value="pageSize")Integer pageSize)throws Exception {
if(pageNum == null){
pageNum = 1; //設置默認當前頁
}
if(pageNum <= 0){
pageNum = 1;
}
if(pageSize == null){
pageSize = 2; //設置默認每頁顯示的數據數
}
System.out.println("當前頁是:"+pageNum+"顯示條數是:"+pageSize);
//1.引入分頁插件,pageNum是第幾頁,pageSize是每頁顯示多少條,默認查詢總數count
//2.緊跟的查詢就是一個分頁查詢-必須緊跟.后面的其他查詢不會被分頁,除非再次調用PageHelper.startPage
PageHelper.startPage(pageNum,pageSize);
List<User> userList =pageimpl.getAllUser();
System.out.println("分頁數據:"+userList);
//3.使用PageInfo包裝查詢后的結果,5是連續 顯示的條數,結果list類型是Page<E>
PageInfo<User> pageInfo = new PageInfo<User>(userList,pageSize);
//4.使用model/map/modelandview等帶回前端
model.addAttribute("pageInfo",pageInfo);
return "bootstraptable";
}
- 前端
<!doctype html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:c="http://www.w3.org/1999/xhtml">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title>Hello, Bootstrap Table!</title>
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
<link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.17.1/dist/bootstrap-table.min.css">
</head>
<body>
<table data-toggle="table">
<thead>
<tr>
<th>name</th>
<th>password</th>
<th>uid</th>
</tr>
</thead>
<tbody >
<tr th:each="c:${pageInfo.list}">
<td>[[${c.name}]]</td>
<td>[[${c.password}]]</td>
<td>[[${c.uid}]]</td>
</tr>
</tbody>
</table>
<br/>
<div class="modal-footer no-margin-top">
<div class="col-md-6">
當前第 [[${pageInfo.pageNum}]]頁,共 [[${pageInfo.pages}]] 頁.一共 [[${pageInfo.total}]] 條記錄
</div>
<ul class="pagination pull-right no-margin">
<li th:if="${pageInfo.hasPreviousPage}">
<a th:href="'/UserInfo?pageNum=1'">首頁</a>
</li>
<li class="prev" th:if="${pageInfo.hasPreviousPage}">
<a th:href="'/UserInfo?pageNum='+${pageInfo.prePage}">
<i class="ace-icon fa fa-angle-double-left"></i>
</a>
</li>
<!--遍歷條數-->
<li th:each="nav:${pageInfo.navigatepageNums}">
<a th:href="'/UserInfo?pageNum='+${nav}" th:text="${nav}" th:if="${nav != pageInfo.pageNum}"></a>
<span style="font-weight: bold;background: #6faed9;" th:if="${nav == pageInfo.pageNum}" th:text="${nav}" ></span>
</li>
<li class="next" th:if="${pageInfo.hasNextPage}">
<a th:href="'/UserInfo?pageNum='+${pageInfo.nextPage}">
<i class="ace-icon fa fa-angle-double-right"></i>
</a>
</li>
<li>
<a th:href="'/UserInfo?pageNum='+${pageInfo.pages}">尾頁</a>
</li>
</ul>
</div>
<div>當前頁號:<span th:text="${pageInfo.pageNum}"></span></div>
<div>每頁條數:<span th:text="${pageInfo.pageSize}"></span></div>
<div>起始行號:<span th:text="${pageInfo.startRow}"></span></div>
<div>終止行號:<span th:text="${pageInfo.endRow}"></span></div>
<div>總結果數:<span th:text="${pageInfo.total}"></span></div>
<div>總頁數:<span th:text="${pageInfo.pages}"></span></div>
<hr />
<div>是否為第一頁:<span th:text="${pageInfo.isFirstPage}"></span></div>
<div>是否為最后一頁:<span th:text="${pageInfo.isLastPage}"></span></div>
<div>是否有前一頁:<span th:text="${pageInfo.hasPreviousPage}"></span></div>
<div>是否有下一頁:<span th:text="${pageInfo.hasNextPage}"></span></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js" integrity="sha512-bLT0Qm9VnAYZDflyKcBaQ2gg0hSYNQrJ8RilYldYQ1FxQYoCLtUjuuRuZo+fjqhx/qtq/1itJ0C2ejDxltZVFg==" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
<script src="https://unpkg.com/bootstrap-table@1.17.1/dist/bootstrap-table.min.js"></script>
</body>
</html>
- 效果
2.1 總結
相比其他分頁插件而言,pagehelper強大的很多。個人覺得用起來還是挺香的。