mybatis分頁外掛---PageHelper外掛
阿新 • • 發佈:2020-12-22
mybatis分頁外掛---PageHelper外掛
文件地址:https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md
資料庫分頁:物理分頁和記憶體分頁
1)記憶體分頁:先將所有資料載入記憶體中,然後從記憶體中查詢分頁的資料
2)物理分頁:在資料檢索資料的時候,只檢索分頁資料,將資料返回給客戶端。
MyBatis提供記憶體分頁 RowBounds引數
Mybatis物理分頁
1、自己寫sql語句 sql limit ?,?
2、MyBatis外掛 MyBatis PageHelper
PageHelper的使用步驟
導人jar
jsqlparser-2.1.jar
pagehelper-5.1.9.jar
maven工程
<!--分頁外掛-->
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.2.0</version>
</dependency>
spring整合
spring-datasource.xml
<!--分頁外掛 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<!-- 這裡的幾個配置主要演示如何使用,如果不理解,一定要去掉下面的配置 -->
<property name="properties">
<value>
helperDialect=mysql
reasonable=true
supportMethodsArguments=true
params=count=countSql
autoRuntimeDialect=true
</value>
</property>
</bean>
</array>
</property>
在service層進行呼叫分頁的方法
PageHelper.startPage(2, 3); 查詢第2頁 每頁顯示3條
只有緊跟在PageHelper.startPage方法後的第一個Mybatis的查詢(Select)方法會被分頁。
自定義一個MyPage實體類:
import com.github.pagehelper.Page;
import java.util.List;
public class MyPage<T> {
private int curryPage;//當前頁
private int totalPage;//總頁面數量
private List<T> dateList;//每頁的資料集合
public MyPage(){
}
public MyPage(Page page){
curryPage=page.getPageNum();
totalPage=page.getPages();
dateList=page.getResult();
}
public int getCurryPage() {
if (curryPage<1)curryPage=1;
if (curryPage>totalPage)curryPage=totalPage;
return curryPage;
}
public void setCurryPage(int curryPage) {
this.curryPage = curryPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public List<T> getDateList() {
return dateList;
}
public void setDateList(List<T> dateList) {
this.dateList = dateList;
}
}
在service層中查詢出我們需要的資料:
@Autowired
UserinfoMapper mapper;
@Override
public MyPage<Userinfo> selectUserByCurryPageAndPageSize(int curry, int pageSize) {
PageHelper.startPage(curry, pageSize);
List<Userinfo> allUser = mapper.selectAllUser();
Page page=(Page)allUser;
MyPage myPage=new MyPage(page);
return myPage;
}
在controller中傳遞MyPage到jsp頁面:
@Autowired
UserService userService;
@RequestMapping("/info")
public ModelAndView showUser(@RequestParam(value="pageNum",defaultValue="1") int pageNum){
MyPage<Userinfo> myPage = userService.selectUserByCurryPageAndPageSize(pageNum, 10);
ModelAndView mv=new ModelAndView();
mv.addObject("myPage",myPage);
mv.setViewName("info");
return mv;
}
jsp頁面的編寫:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<table width="80%" border="1">
<tr>
<th>id</th>
<th>username</th>
<th>birthday</th>
<th>sex</th>
<th>address</th>
<th>money</th>
<th>操作</th>
</tr>
<c:forEach items="${myPage.dateList}" var="item">
<tr>
<td>${item.id}</td>
<td>${item.username}</td>
<td>${item.birthday}</td>
<td>${item.sex}</td>
<td>${item.address}</td>
<td>${item.money}</td>
<td><a href="/delete?id=${item.id}">刪除</a>
<a href="/toUpdate?id=${item.id}">修改</a>
</td>
</tr>
</c:forEach>
</table>
<a href="/info?pageNum=1">首頁</a>
<a href="/info?pageNum=${myPage.curryPage-1}">上一頁</a>
<a href="/info?pageNum=${myPage.curryPage}">當前頁${myPage.curryPage}</a>
<a href="/info?pageNum=${myPage.curryPage+1}">下一頁</a>
<a href="/info?pageNum=${myPage.totalPage}">尾頁</a>
</body>
</html>