1. 程式人生 > >mybatis常用經典分頁方法

mybatis常用經典分頁方法

  1. import java.io.BufferedReader;  
  2. import java.io.File;  
  3. import java.io.FileInputStream;  
  4. import java.io.InputStreamReader;  
  5. import com.ssm.util.PageData;  
  6. public class Page {  
  7.     private int isM1 = 0; // 每頁顯示記錄數  
  8.     private int isM2; // 每頁顯示記錄數  
  9.     private int showCount = 10; // 每頁顯示記錄數  
  10.     private int totalPage; // 總頁數  
  11.     private int totalResult; // 總記錄數  
  12.     private int currentPage = 1; // 當前頁  
  13.     private int currentResult; // 當前記錄起始索引  
  14.     private boolean entityOrField; // true:需要分頁的地方,傳入的引數就是Page實體;false:需要分頁的地方,傳入的引數所代表的實體擁有Page屬性  
  15.     private String pageStr; // 最終頁面顯示的底部翻頁導航,詳細見:getPageStr();  
  16.     private PageData pd = new PageData();  
  17.     private int begin;  
  18.     private int end;  
  19.     // 總行數,需要外接傳入  
  20.     private int rows;  
  21.     public int getIsM1() {  
  22.         return isM1;  
  23.     }  
  24.     public void setIsM1(int isM1) {  
  25.         this.isM1 = isM1;  
  26.     }  
  27.     public int getIsM2() {  
  28.         return isM2;  
  29.     }  
  30.     public void setIsM2(int isM2) {  
  31.         this.isM2 = isM2;  
  32.     }  
  33.     public int getRows() {  
  34.         return rows;  
  35.     }  
  36.     public void setRows(int rows) {  
  37.         this.rows = rows;  
  38.     }  
  39.     public int getBegin() {  
  40.         begin = (currentPage - 1) * showCount;  
  41.         return begin;  
  42.     }  
  43.     public void setBegin(int begin) {  
  44.         this.begin = begin;  
  45.     }  
  46.     public int getEnd() {  
  47.         end = currentPage * showCount;  
  48.         return end;  
  49.     }  
  50.     public void setEnd(int end) {  
  51.         this.end = end;  
  52.     }  
  53.     public Page() {  
  54.         // 通過page。txt設定每頁顯示的條數  
  55.         // String xmpath =  
  56.         // String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));  
  57.         // // System.out.println(xmpath);  
  58.         // xmpath = xmpath.substring(6)+"page.txt";  
  59.         // // System.out.println(xmpath);  
  60.         // this.showCount = Integer.parseInt(readTxtFile(xmpath));  
  61.     }  
  62.     public int getTotalPage() {  
  63.         if (rows % showCount == 0)  
  64.             totalPage = rows / showCount;  
  65.         else  
  66.             totalPage = rows / showCount + 1;  
  67.         return totalPage;  
  68.     }  
  69.     public void setTotalPage(int totalPage) {  
  70.         this.totalPage = totalPage;  
  71.     }  
  72.     public int getTotalResult() {  
  73.         return totalResult;  
  74.     }  
  75.     public void setTotalResult(int totalResult) {  
  76.         this.totalResult = totalResult;  
  77.     }  
  78.     public int getCurrentPage() {  
  79.         if (currentPage <= 0)  
  80.             currentPage = 1;  
  81.         if (currentPage > getTotalPage())  
  82.             currentPage = getTotalPage();  
  83.         return currentPage;  
  84.     }  
  85.     public void setCurrentPage(int currentPage) {  
  86.         this.currentPage = currentPage;  
  87.     }  
  88.     // 用於顯示頁碼  
  89.     public String getPageStr() {  
  90.         StringBuffer sb = new StringBuffer();  
  91.         if (totalResult > 0) {  
  92.             sb.append(" <ul>\n");  
  93.             if (currentPage == 1) {  
  94.                 sb.append(" <li><a>共<font color=red>" + totalResult  
  95.                         + "</font>條</a></li>\n");  
  96.                 sb.append(" <li><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width:50px;text-align:center;float:left\" placeholder=\"頁碼\"/></li>\n");  
  97.                 sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"toTZ();\"  class=\"btn btn-mini btn-success\">跳轉</a></li>\n");  
  98.                 sb.append(" <li><a>首頁</a></li>\n");  
  99.                 sb.append(" <li><a>上頁</a></li>\n");  
  100.             } else {  
  101.                 sb.append(" <li><a>共<font color=red>" + totalResult  
  102.                         + "</font>條</a></li>\n");  
  103.                 sb.append(" <li><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width:35px;text-align:center;\" placeholder=\"頁碼\"/></li>\n");  
  104.                 sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"toTZ();\"  class=\"btn btn-mini btn-success\">跳轉</a></li>\n");  
  105.                 sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage(1)\">首頁</a></li>\n");  
  106.                 sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("  
  107.                         + (currentPage - 1) + ")\">上頁</a></li>\n");  
  108.             }  
  109.             int showTag = 3; // 分頁標籤顯示數量  
  110.             int startTag = 1;  
  111.             if (currentPage > showTag) {  
  112.                 startTag = currentPage - 1;  
  113.             }  
  114.             int endTag = startTag + showTag - 1;  
  115.             for (int i = startTag; i <= totalPage && i <= endTag; i++) {  
  116.                 if (currentPage == i)  
  117.                     sb.append("<li class=\"current\"><a>" + i + "</a></li>\n");  
  118.                 else  
  119.                     sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("  
  120.                             + i + ")\">" + i + "</a></li>\n");  
  121.             }  
  122.             if (currentPage == totalPage) {  
  123.                 sb.append(" <li><a>下頁</a></li>\n");  
  124.                 sb.append(" <li><a>尾頁</a></li>\n");  
  125.             } else {  
  126.                 sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("  
  127.                         + (currentPage + 1) + ")\">下頁</a></li>\n");  
  128.                 sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("  
  129.                         + totalPage + ")\">尾頁</a></li>\n");  
  130.             }  
  131.             sb.append(" <li><a>第" + currentPage + "頁</a></li>\n");  
  132.             sb.append(" <li><a>共" + totalPage + "頁</a></li>\n");  
  133.             // sb.append("  <li style=\"cursor:pointer;\"><a onclick=\"toTZ();\"  class=\"btn btn-mini btn-success\">跳轉</a></li>\n");  
  134.             sb.append("</ul>\n");  
  135.             sb.append("<script type=\"text/javascript\">\n");  
  136.             sb.append("function nextPage(page){");  
  137.             sb.append(" if(true && document.forms[0]){\n");  
  138.             sb.append("     var url = document.forms[0].getAttribute(\"action\");\n");  
  139.             sb.append("     if(url.indexOf('?')>-1){url += \"&"  
  140.                     + (entityOrField ? "currentPage" : "page.currentPage")  
  141.                     + "=\";}\n");  
  142.             sb.append("     else{url += \"?"  
  143.                     + (entityOrField ? "currentPage" : "page.currentPage")  
  144.                     + "=\";}\n");  
  145.             sb.append("     document.forms[0].action = url+page;\n");  
  146.             sb.append("     document.forms[0].submit();\n");  
  147.             sb.append(" }else{\n");  
  148.             sb.append("     var url = document.location+'';\n");  
  149.             sb.append("     if(url.indexOf('?')>-1){\n");  
  150.             sb.append("         if(url.indexOf('currentPage')>-1){\n");  
  151.             sb.append("             var reg = /currentPage=\\d*/g;\n");  
  152.             sb.append("             url = url.replace(reg,'currentPage=');\n");  
  153.             sb.append("         }else{\n");  
  154.             sb.append("             url += \"&"  
  155.                     + (entityOrField ? "currentPage" : "page.currentPage")  
  156.                     + "=\";\n");  
  157.             sb.append("         }\n");  
  158.             sb.append("     }else{url += \"?"  
  159.                     + (entityOrField ? "currentPage" : "page.currentPage")  
  160.                     + "=\";}\n");  
  161.             sb.append("     document.location = url + page;\n");  
  162.             sb.append(" }\n");  
  163.             sb.append("}\n");  
  164.             sb.append("function toTZ(){");  
  165.             sb.append("var toPaggeVlue = document.getElementById(\"toGoPage\").value;");  
  166.             sb.append("if(toPaggeVlue == ''){document.getElementById(\"toGoPage\").value=1;return;}");  
  167.             sb.append("if(isNaN(Number(toPaggeVlue))){document.getElementById(\"toGoPage\").value=1;return;}");  
  168.             sb.append("nextPage(toPaggeVlue);");  
  169.             sb.append("}\n");  
  170.             sb.append("</script>\n");  
  171.         }  
  172.         pageStr = sb.toString();  
  173.         return pageStr;  
  174.     }  
  175.     public void setPageStr(String pageStr) {  
  176.         this.pageStr = pageStr;  
  177.     }  
  178.     public int getShowCount() {  
  179.         return showCount;  
  180.     }  
  181.     public void setShowCount(int showCount) {  
  182.         this.showCount = showCount;  
  183.     }  
  184.     public int getCurrentResult() {  
  185.         currentResult = (getCurrentPage() - 1) * getShowCount();  
  186.         if (currentResult < 0)  
  187.             currentResult = 0;  
  188.         return currentResult;  
  189.     }  
  190.     public void setCurrentResult(int currentResult) {  
  191.         this.currentResult = currentResult;  
  192.     }  
  193.     public boolean isEntityOrField() {  
  194.         return entityOrField;  
  195.     }  
  196.     public void setEntityOrField(boolean entityOrField) {  
  197.         this.entityOrField = entityOrField;  
  198.     }  
  199.     public PageData getPd() {  
  200.         return pd;  
  201.     }  
  202.     public void setPd(PageData pd) {  
  203.         this.pd = pd;  
  204.     }  
  205.     // 讀取檔案  
  206.     public String readTxtFile(String filePath) {  
  207.         try {  
  208.             String encoding = "utf-8";  
  209.             File file = new File(filePath);  
  210.             if (file.isFile() && file.exists()) { // 判斷檔案是否存在  
  211.                 InputStreamReader read = new InputStreamReader(  
  212.                         new FileInputStream(file), encoding);// 考慮到編碼格式  
  213.                 BufferedReader bufferedReader = new BufferedReader(read);  
  214.                 String lineTxt = null;  
  215.                 while ((lineTxt = bufferedReader.readLine()) != null) {  
  216.                     return lineTxt;  
  217.                 }  
  218.                 read.close();  
  219.             } else {  
  220.                 System.out.println("找不到指定的檔案");  
  221.             }  
  222.         } catch (Exception e) {  
  223.             System.out.println("讀取檔案內容出錯");  
  224.             e.printStackTrace();  
  225.         }  
  226.         return "";  
  227.     }  
  228. }