1. 程式人生 > >mybatis邏輯分頁,含分頁導航

mybatis邏輯分頁,含分頁導航

mybatis是非常優秀的半orm框架,比hibernate更容易控制,效能也很好,但mybatis官方並沒提供分頁功能,需要自己實現,下面提供一種物理分頁的實現思路:

(基於sping、spring mvc、mybatis的整合,將PO)

1. 編寫一個Pagination.java的實體類,含有頁面顯示數、總頁數、當前頁、開始行、結束行等屬性

2. sql里加上分頁引數

3. PO物件里加上這個分頁物件

4. 在Service裡,執行查詢前,將分頁例項的總記錄數set進去,當前頁由頁面傳入,這樣就有了三個必備引數:總記錄數、每頁顯示數(可以設一個預設值)、當前頁,就可以獲取sql查詢時開始行、結束行,就實現分頁了。

優點:

不改動mybatis框架程式碼,對service的實現類改動很小,只需加入一行程式碼,對controller改個只需model裡add兩個attribute,儘量做到低侵入低耦合。分頁導航自動生成,可重新設定每頁顯示數,從不分頁改為分頁儘可能減少程式碼的增加工作。

缺點:

暫時只適用於持久化物件和表單檢視物件共用,即持久化物件既用於資料庫互動,也用於表單資料繫結。

下面為例項說明:

1. 在物件里加入一個屬性:

Java程式碼 
  1. ...  
  2. /** 用於分頁 */  
  3. private Pagination pagination = new Pagination();  
  4. public Pagination getPagination() {  
  5.     return pagination;  
  6. }  
  7. public void setPagination(Pagination pagination) {  
  8.     this.pagination = pagination;  
  9. }  

此處初始化了,是為了不用判斷是否為null,相當於給一個初始值。 

2.  在mapper.xml檔案中加入分頁語句本例以mysql(limit #{offSet}, #{pageSize})為例

Xml程式碼 
  1. <select id="getUserInfoList"
     parameterType="com.jayung.pagination.domain.UserInfo"  
  2.      resultType="com.jayung.pagination.domain.UserInfo">  
  3.     select user_id userId, user_name userName, password password,   
  4.        age age   
  5.       from user_info   
  6.      <trim prefix="where" suffixOverrides="and">  
  7.         <if test="userId!=null and userId!=''">user_id = #{userId} and </if>  
  8.         <if test="userName!=null and userName!=''">user_name = #{userName} and </if>  
  9.         <if test="password!=null and password!=''">password = #{password} and </if>  
  10.         <if test="age!=null and age!=''">age = #{age} and </if>  
  11.      </trim>  
  12.      <span style="background-color: #ffff00;">limit #{pagination.startRow}, #{pagination.pageSize}</span>  
  13. </select>  

 其中limit #{pagination.startRow}, #{pagination.pageSize}是分頁的兩個引數;

3. 定義取總記錄數的SQL

Xml程式碼 
  1. <select id="getUserInfoListCount" parameterType="com.jayung.pagination.domain.UserInfo"  
  2.      resultType="java.lang.Integer">  
  3.     select count(1)  
  4.       from user_info   
  5.      <trim prefix="where" suffixOverrides="and">  
  6.         <if test="userId!=null and userId!=''">user_id = #{userId} and </if>  
  7.         <if test="userName!=null and userName!=''">user_name = #{userName} and </if>  
  8.         <if test="password!=null and password!=''">password = #{password} and </if>  
  9.         <if test="age!=null and age!=''">age = #{age} and </if>  
  10.      </trim>  
  11. </select>  

 與上面SQL的差別是將查詢結果集換成了count記錄集的總數;

3. serviceimpl

Java程式碼 
  1. public List<UserInfo> getUserInfoList(UserInfo userInfo) {  
  2.     userInfo.getPagination().setTotalRow(userInfoMapper.getUserInfoListCount(userInfo));  
  3.     return userInfoMapper.getUserInfoList(userInfo);  
  4. }  

 4. controller

Java程式碼 
  1. @RequestMapping(value = "/demo/userInfo/search")  
  2. public String search(Model model, UserInfo userInfo, HttpServletRequest request) {  
  3.     // userInfo裡含有pagination屬性。  
  4.     model.addAttribute("userInfoList", userInfoService.getUserInfoList(userInfo));  
  5.     // pagination是分頁物件  
  6.     model.addAttribute("pagination", userInfo.getPagination());  
  7.     // paginationForm是為了不丟失從查詢頁面傳過來的查詢引數,並儲存當前頁、每頁顯示數的資訊  
  8.     model.addAttribute("paginationForm", PaginationUtil.getPaginationForm(request));  
  9.     return  "/demo/userInfo/userInfoList";  
  10. }  

5. paginationUtil用於儲存來自源頁面的引數,防止翻到第二頁時丟失。

Java程式碼 
  1. public static String getPaginationForm(HttpServletRequest request) {  
  2.     StringBuffer form = new StringBuffer();  
  3.     form.append("<form name=\"_paginationForm\" id=\"_paginationForm\" method=\"post\" aciton=\""  
  4.             + new UrlPathHelper().getOriginatingRequestUri(request) + "\">\n");  
  5.     Enumeration paramNames = request.getParameterNames();  
  6.     while (paramNames.hasMoreElements()) {  
  7.         String paramName = (String) paramNames.nextElement();  
  8.         String paramValue = request.getParameter(paramName);  
  9.         if (!"pagination.pageSize".equals(paramName) && !"pagination.currentPage".equals(paramName)) {  
  10.             form.append("   <input type=\"hidden\" name=\"" + paramName + "\" value=\"" + paramValue + "\" />\n");  
  11.         }  
  12.     }  
  13.     String pageSize = (request.getParameter("pagination.pageSize") == null) ? Constant.PAGE_SIZE_DEFAULT.toString()  
  14.             : request.getParameter("pagination.pageSize");  
  15.     String currentPage = (request.getParameter("pagination.currentPage") == null) ? "1" : request  
  16.             .getParameter("pagination.currentPage");  
  17.     form.append("   <input type=\"hidden\" id=\"_pagination.pageSize\" name=\"pagination.pageSize\" value=\"" + pageSize + "\" />\n");  
  18.     form.append("   <input type=\"hidden\" id=\"_pagination.currentPage\" name=\"pagination.currentPage\" value=\"" + currentPage + "\" />\n");  
  19.     form.append("</form>");  
  20.     return form.toString();  
  21. }  

6. 分頁頁面

Java程式碼 
  1. <table class="tableList" id="userInfoList">  
  2. <tr>  
  3.         <th>userId</th>  
  4.         <th>userName</th>  
  5.         <th>password</th>  
  6.         <th>age</th>  
  7.     <th>操作</th>  
  8. <tr>  
  9.     <c:forEach items="${userInfoList}" var="userInfo" varStatus="status">  
  10.     <tr id="${status.index}">  
  11.         <td>${userInfo.userId}</td>  
  12.         <td>${userInfo.userName}</td>  
  13.         <td>${userInfo.password}</td>  
  14.         <td>${userInfo.age}</td>  
  15.         <td><a href="${userInfo.userId}">檢視</a>   
  16.         <a href="${userInfo.userId}/edit">編輯</a>   
  17.         <a href="javascript:void();" onclick="deleteRow('${userInfo.userId}','${status.index}');">刪除</a></td>  
  18.     </tr>  
  19.     </c:forEach>  
  20. </table>  
  21. <div><span style="background-color: #ffff00;">${pagination.navigator}</span></div>  
  22. span style="background-color: #ffff00;">${paginationForm}</span>  

 ${pagination.navigator}是分頁導航

${paginationForm}是分頁表單

最後上傳完整工程附件,含初始化sql指令碼

歡迎大家討論優化。

下載連結請轉至iteye部落格地址: