MyBatis基于pagehelper實現(xiàn)分頁原理及代碼實例
使用pagehelper分頁的原理是:
通過MyBatis的插件原理(類似web里的filter攔截器),在mapper配置文件將pagehelper注冊為MyBatis的插件,從而進行分頁
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); //設置第幾條記錄開始,多少條記錄為一頁 //通過userService獲取user的信息,其sql語句為'select * from user' 但因pagehelp已經(jīng)注冊為插件,所以pagehelp會在原sql語句上增加limit,從而實現(xiàn)分頁 List<Person> persons=userService.getAllUsersBypageHelper(); //因而獲得的是分好頁的結(jié)果集 PageInfo<?> pageHelper=page.toPageInfo(); //獲取頁面信息的對象,里面封裝了許多頁面的信息 如:總條數(shù),當前頁碼,需顯示的導航頁等等 request.setAttribute('persons',persons); request.setAttribute('pagehelper',pageHelper); request.getRequestDispatcher('/persons.jsp').forward(request,response); }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關文章:
1. MySQL decimal unsigned更新負數(shù)轉(zhuǎn)化為02. Oracle的PDB數(shù)據(jù)庫創(chuàng)建DIRECTORY時遇到ORA-65254問題及解決方法3. MYSQL(電話號碼,身份證)數(shù)據(jù)脫敏的實現(xiàn)4. MySql導出后再導入數(shù)據(jù)時出錯問題5. 如何:創(chuàng)建和運行 CLR SQL Server 存儲過程6. 用一個實例講解Oracle的自定義聚集函數(shù)7. Sql Server 2000數(shù)據(jù)庫日志日益龐大的解決方法8. 快速刪除ORACLE重復記錄9. 教你在AIX上安裝IBM DB2 9版本的分區(qū)環(huán)境10. mysql-bin.000001文件的來源及處理方法
