使用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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
帶你了解Java數(shù)據(jù)結(jié)構(gòu)和算法之?dāng)?shù)組
這篇文章主要為大家介紹了Java數(shù)據(jù)結(jié)構(gòu)和算法之?dāng)?shù)組,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助2022-01-01關(guān)于@ResponseBody 默認(rèn)輸出的誤區(qū)的解答
這篇文章主要介紹了關(guān)于@ResponseBody 默認(rèn)輸出的誤區(qū)的解答,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-04-04深入淺出理解Java Lambda表達(dá)式之四大核心函數(shù)式的用法與范例
Lambda 表達(dá)式,也可稱為閉包,它是推動(dòng) Java 8 發(fā)布的最重要新特性。Lambda 允許把函數(shù)作為一個(gè)方法的參數(shù)(函數(shù)作為參數(shù)傳遞進(jìn)方法中)。使用 Lambda 表達(dá)式可以使代碼變的更加簡潔緊湊,今天小編帶你理解Lambda表達(dá)式之四大核心函數(shù)式的用法,感興趣的朋友快來看看吧2021-11-11Mybatis之通用Mapper動(dòng)態(tài)表名及其原理分析
這篇文章主要介紹了Mybatis之通用Mapper動(dòng)態(tài)表名及其原理分析,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08java實(shí)現(xiàn)微信企業(yè)付款到個(gè)人
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)微信企業(yè)付款到個(gè)人功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10springcloud干貨之服務(wù)注冊與發(fā)現(xiàn)(Eureka)
這篇文章主要介紹了springcloud干貨之服務(wù)注冊與發(fā)現(xiàn)(Eureka) ,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01Java數(shù)據(jù)脫敏實(shí)現(xiàn)的方法總結(jié)
數(shù)據(jù)脫敏,指的是對某些敏感信息通過脫敏規(guī)則進(jìn)行數(shù)據(jù)的變形,實(shí)現(xiàn)敏感隱私數(shù)據(jù)的可靠保護(hù),本文主要是對后端數(shù)據(jù)脫敏實(shí)現(xiàn)的簡單總結(jié),希望對大家有所幫助2023-07-07Java實(shí)現(xiàn)求數(shù)組最長子序列算法示例
這篇文章主要介紹了Java實(shí)現(xiàn)求數(shù)組最長子序列算法,涉及java針對數(shù)組的遞歸遍歷、判斷相關(guān)操作技巧,需要的朋友可以參考下2018-07-07