MyBatis基于pagehelper實(shí)現(xiàn)分頁原理及代碼實(shí)例
使用pagehelper分頁的原理是:
通過MyBatis的插件原理(類似web里的filter攔截器),在mapper配置文件將pagehelper注冊為MyBatis的插件,從而進(jìn)行分頁
1.通過maven引入pagehelper依賴:
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper --><dependency> <groupId>com.github.pagehelper</groupId> <artifactId>pagehelper</artifactId> <version>5.1.11</version></dependency>
2.在MyBatis的mapper配置文件將pagehelper注冊為MyBatis的插件
<plugins> <plugin interceptor='com.github.pagehelper.PageInterceptor'></plugin> </plugins>
3.pagehelper的用法:
private void selectAllUsers(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String num=request.getParameter('num'); if(null==num) { num='1'; } // Page PageInfo Page<?> page=PageHelper.startPage(Integer.parseInt(num),5); //設(shè)置第幾條記錄開始,多少條記錄為一頁 //通過userService獲取user的信息,其sql語句為'select * from user' 但因pagehelp已經(jīng)注冊為插件,所以pagehelp會(huì)在原sql語句上增加limit,從而實(shí)現(xiàn)分頁 List<Person> persons=userService.getAllUsersBypageHelper(); //因而獲得的是分好頁的結(jié)果集 PageInfo<?> pageHelper=page.toPageInfo(); //獲取頁面信息的對象,里面封裝了許多頁面的信息 如:總條數(shù),當(dāng)前頁碼,需顯示的導(dǎo)航頁等等 request.setAttribute('persons',persons); request.setAttribute('pagehelper',pageHelper); request.getRequestDispatcher('/persons.jsp').forward(request,response); }
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. DB2的高可用性和災(zāi)難恢復(fù)概述2. Microsoft Office Access添加外鍵的方法3. 記一次mariadb數(shù)據(jù)庫無法連接4. sqlserver給表添加新字段、給表和字段添加備注、更新備注及查詢備注(sql語句)5. SQL優(yōu)化過程中常見Oracle HINT的用法6. MySQL安裝提示配置信息已損壞請聯(lián)系技術(shù)人員7. 三種級別的DB2數(shù)據(jù)庫字符集的設(shè)置與修改8. MySQL之高可用集群部署及故障切換實(shí)現(xiàn)9. 如何修改Linux服務(wù)器中的MySQL數(shù)據(jù)庫密碼10. DB2 實(shí)用程序介紹之?dāng)?shù)據(jù)移動(dòng)實(shí)用程序
