使用Mybatis的PageHelper分頁工具的教程詳解
1、導(dǎo)入相關(guān)的jar包
在pom.xm中加入
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --> <dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.10</version> </dependency>
2、在Mybatis的配置文件mybatis-config.xml中加入以下代碼
<plugins>
<!-- com.github.pagehelper為PageHelper類所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置參數(shù),后面會(huì)有所有的參數(shù)介紹 -->
<property name="reasonable" value="true"/>
</plugin>
</plugins>
在controller中編寫代碼引用
@RequestMapping(value = "/emps")
public String GetEmployees(@RequestParam(value = "pn",
defaultValue ="1")Integer pn , Model model){
PageHelper.startPage(pn,8);
List<Employee> employeeslist = employeeService.GetEmployees();
PageInfo page = new PageInfo(employeeslist,7);
model.addAttribute("pageinfo",page);
return "list";
}

PS:下面看下PageHelper的簡單使用(強(qiáng)大的分頁工具)
1.使用maven解決依賴
<dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>3.4.2</version> </dependency>
2.在Controller調(diào)用Service的時(shí)候,調(diào)用PageHelper
@RequestMapping("/sysadmin/dept/list")
public String toDeptList(Model model,@RequestParam(required = false,defaultValue = "1",value = "pn")Integer pn ) {
PageHelper.startPage(pn, 8);
List<Dept> deptList = deptService.findAll();
PageInfo<Dept> p = new PageInfo<>(deptList);
model.addAttribute("deptList", deptList);
model.addAttribute("page", p);
return "sysadmin/dept/jDeptList";
}
PageHelper.startPage(pn, 8); //參數(shù)分別是設(shè)置當(dāng)前的頁數(shù)和每頁的數(shù)量
PageInfo<Dept> p = new PageInfo<>(deptList); //將得到查詢結(jié)果集進(jìn)行封裝
3.在jsp頁面進(jìn)行簡單的分頁
<a href="/sysadmin/dept/list?pn=${page.firstPage}" rel="external nofollow" >首頁</a>
<c:if test="${page.hasPreviousPage}"><a href="/sysadmin/dept/list?pn=${page.prePage}" rel="external nofollow" >上一頁</a></c:if>
<c:if test="${!page.hasPreviousPage}">上一頁</c:if>
<c:if test="${page.hasNextPage}"><a href="/sysadmin/dept/list?pn=${page.nextPage}" rel="external nofollow" >下一頁</a></c:if>
<c:if test="${! page.hasNextPage}">下一頁</c:if>
<a href="/sysadmin/dept/list?pn=${page.lastPage}" rel="external nofollow" >最后頁</a>
<p>一共${page.pages}頁 --當(dāng)前頁是${page.pageNum } -- 共有${page.total }條數(shù)據(jù)</p>
簡單的進(jìn)行了調(diào)用,實(shí)現(xiàn)了基本的功能(使用pageInfo的相關(guān)屬性)
總結(jié)
到此這篇關(guān)于使用Mybatis的PageHelper分頁工具的教程詳解的文章就介紹到這了,更多相關(guān)Mybatis的PageHelper分頁工具內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot接收數(shù)組參數(shù)和集合參數(shù)方式
這篇文章主要介紹了SpringBoot接收數(shù)組參數(shù)和集合參數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2025-03-03
Mybatis-plus如何查詢返回對(duì)象內(nèi)有List<String>屬性
在使用Mybatis-Plus進(jìn)行開發(fā)時(shí),我們經(jīng)常會(huì)遇到需要處理一對(duì)多關(guān)系映射的情況,例如,查詢用戶數(shù)據(jù)時(shí),可能需要同時(shí)獲取該用戶管理的所有小區(qū)名稱列表,這要求我們?cè)诜祷氐膶?shí)體類中包含一個(gè)List<String>屬性,用于存放小區(qū)名稱,實(shí)現(xiàn)這一功能2024-10-10
spring boot自定義log4j2日志文件的實(shí)例講解
下面小編就為大家分享一篇spring boot自定義log4j2日志文件的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11
Spring Boot集成MinIO對(duì)象存儲(chǔ)服務(wù)器操作步驟
通過Spring Boot集成MinIO,你可以在應(yīng)用中方便地進(jìn)行文件的存儲(chǔ)和管理,本文給大家分享Spring Boot集成MinIO對(duì)象存儲(chǔ)服務(wù)器詳細(xì)操作步驟,感興趣的朋友一起看看吧2024-01-01
SpringMVC前后端傳值的幾種實(shí)現(xiàn)方式
本文主要介紹了SpringMVC前后端傳值的方式實(shí)現(xiàn),包括使用HttpServletRequest、HttpSession、Model和ModelAndView等方法,具有一定的參考價(jià)值,感興趣的可以了解一下2025-02-02

