mybatis常用經典分頁方法
阿新 • • 發佈:2018-12-15
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileInputStream;
- import java.io.InputStreamReader;
- import com.ssm.util.PageData;
- public class Page {
- private int isM1 = 0; // 每頁顯示記錄數
- private int isM2; // 每頁顯示記錄數
- private int showCount = 10; // 每頁顯示記錄數
- private int totalPage; // 總頁數
- private int totalResult; // 總記錄數
- private int currentPage = 1; // 當前頁
- private int currentResult; // 當前記錄起始索引
- private boolean entityOrField; // true:需要分頁的地方,傳入的引數就是Page實體;false:需要分頁的地方,傳入的引數所代表的實體擁有Page屬性
- private String pageStr; // 最終頁面顯示的底部翻頁導航,詳細見:getPageStr();
- private PageData pd = new PageData();
- private int begin;
- private int end;
- // 總行數,需要外接傳入
- private int rows;
- public int getIsM1() {
- return isM1;
- }
- public void setIsM1(int isM1) {
- this.isM1 = isM1;
- }
- public int getIsM2() {
- return isM2;
- }
- public void setIsM2(int isM2) {
- this.isM2 = isM2;
- }
- public int getRows() {
- return rows;
- }
- public void setRows(int rows) {
- this.rows = rows;
- }
- public int getBegin() {
- begin = (currentPage - 1) * showCount;
- return begin;
- }
- public void setBegin(int begin) {
- this.begin = begin;
- }
- public int getEnd() {
- end = currentPage * showCount;
- return end;
- }
- public void setEnd(int end) {
- this.end = end;
- }
- public Page() {
- // 通過page。txt設定每頁顯示的條數
- // String xmpath =
- // String.valueOf(Thread.currentThread().getContextClassLoader().getResource(""));
- // // System.out.println(xmpath);
- // xmpath = xmpath.substring(6)+"page.txt";
- // // System.out.println(xmpath);
- // this.showCount = Integer.parseInt(readTxtFile(xmpath));
- }
- public int getTotalPage() {
- if (rows % showCount == 0)
- totalPage = rows / showCount;
- else
- totalPage = rows / showCount + 1;
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- public int getTotalResult() {
- return totalResult;
- }
- public void setTotalResult(int totalResult) {
- this.totalResult = totalResult;
- }
- public int getCurrentPage() {
- if (currentPage <= 0)
- currentPage = 1;
- if (currentPage > getTotalPage())
- currentPage = getTotalPage();
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- // 用於顯示頁碼
- public String getPageStr() {
- StringBuffer sb = new StringBuffer();
- if (totalResult > 0) {
- sb.append(" <ul>\n");
- if (currentPage == 1) {
- sb.append(" <li><a>共<font color=red>" + totalResult
- + "</font>條</a></li>\n");
- sb.append(" <li><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width:50px;text-align:center;float:left\" placeholder=\"頁碼\"/></li>\n");
- sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"toTZ();\" class=\"btn btn-mini btn-success\">跳轉</a></li>\n");
- sb.append(" <li><a>首頁</a></li>\n");
- sb.append(" <li><a>上頁</a></li>\n");
- } else {
- sb.append(" <li><a>共<font color=red>" + totalResult
- + "</font>條</a></li>\n");
- sb.append(" <li><input type=\"number\" value=\"\" id=\"toGoPage\" style=\"width:35px;text-align:center;\" placeholder=\"頁碼\"/></li>\n");
- sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"toTZ();\" class=\"btn btn-mini btn-success\">跳轉</a></li>\n");
- sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage(1)\">首頁</a></li>\n");
- sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("
- + (currentPage - 1) + ")\">上頁</a></li>\n");
- }
- int showTag = 3; // 分頁標籤顯示數量
- int startTag = 1;
- if (currentPage > showTag) {
- startTag = currentPage - 1;
- }
- int endTag = startTag + showTag - 1;
- for (int i = startTag; i <= totalPage && i <= endTag; i++) {
- if (currentPage == i)
- sb.append("<li class=\"current\"><a>" + i + "</a></li>\n");
- else
- sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("
- + i + ")\">" + i + "</a></li>\n");
- }
- if (currentPage == totalPage) {
- sb.append(" <li><a>下頁</a></li>\n");
- sb.append(" <li><a>尾頁</a></li>\n");
- } else {
- sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("
- + (currentPage + 1) + ")\">下頁</a></li>\n");
- sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"nextPage("
- + totalPage + ")\">尾頁</a></li>\n");
- }
- sb.append(" <li><a>第" + currentPage + "頁</a></li>\n");
- sb.append(" <li><a>共" + totalPage + "頁</a></li>\n");
- // sb.append(" <li style=\"cursor:pointer;\"><a onclick=\"toTZ();\" class=\"btn btn-mini btn-success\">跳轉</a></li>\n");
- sb.append("</ul>\n");
- sb.append("<script type=\"text/javascript\">\n");
- sb.append("function nextPage(page){");
- sb.append(" if(true && document.forms[0]){\n");
- sb.append(" var url = document.forms[0].getAttribute(\"action\");\n");
- sb.append(" if(url.indexOf('?')>-1){url += \"&"
- + (entityOrField ? "currentPage" : "page.currentPage")
- + "=\";}\n");
- sb.append(" else{url += \"?"
- + (entityOrField ? "currentPage" : "page.currentPage")
- + "=\";}\n");
- sb.append(" document.forms[0].action = url+page;\n");
- sb.append(" document.forms[0].submit();\n");
- sb.append(" }else{\n");
- sb.append(" var url = document.location+'';\n");
- sb.append(" if(url.indexOf('?')>-1){\n");
- sb.append(" if(url.indexOf('currentPage')>-1){\n");
- sb.append(" var reg = /currentPage=\\d*/g;\n");
- sb.append(" url = url.replace(reg,'currentPage=');\n");
- sb.append(" }else{\n");
- sb.append(" url += \"&"
- + (entityOrField ? "currentPage" : "page.currentPage")
- + "=\";\n");
- sb.append(" }\n");
- sb.append(" }else{url += \"?"
- + (entityOrField ? "currentPage" : "page.currentPage")
- + "=\";}\n");
- sb.append(" document.location = url + page;\n");
- sb.append(" }\n");
- sb.append("}\n");
- sb.append("function toTZ(){");
- sb.append("var toPaggeVlue = document.getElementById(\"toGoPage\").value;");
- sb.append("if(toPaggeVlue == ''){document.getElementById(\"toGoPage\").value=1;return;}");
- sb.append("if(isNaN(Number(toPaggeVlue))){document.getElementById(\"toGoPage\").value=1;return;}");
- sb.append("nextPage(toPaggeVlue);");
- sb.append("}\n");
- sb.append("</script>\n");
- }
- pageStr = sb.toString();
- return pageStr;
- }
- public void setPageStr(String pageStr) {
- this.pageStr = pageStr;
- }
- public int getShowCount() {
- return showCount;
- }
- public void setShowCount(int showCount) {
- this.showCount = showCount;
- }
- public int getCurrentResult() {
- currentResult = (getCurrentPage() - 1) * getShowCount();
- if (currentResult < 0)
- currentResult = 0;
- return currentResult;
- }
- public void setCurrentResult(int currentResult) {
- this.currentResult = currentResult;
- }
- public boolean isEntityOrField() {
- return entityOrField;
- }
- public void setEntityOrField(boolean entityOrField) {
- this.entityOrField = entityOrField;
- }
- public PageData getPd() {
- return pd;
- }
- public void setPd(PageData pd) {
- this.pd = pd;
- }
- // 讀取檔案
- public String readTxtFile(String filePath) {
- try {
- String encoding = "utf-8";
- File file = new File(filePath);
- if (file.isFile() && file.exists()) { // 判斷檔案是否存在
- InputStreamReader read = new InputStreamReader(
- new FileInputStream(file), encoding);// 考慮到編碼格式
- BufferedReader bufferedReader = new BufferedReader(read);
- String lineTxt = null;
- while ((lineTxt = bufferedReader.readLine()) != null) {
- return lineTxt;
- }
- read.close();
- } else {
- System.out.println("找不到指定的檔案");
- }
- } catch (Exception e) {
- System.out.println("讀取檔案內容出錯");
- e.printStackTrace();
- }
- return "";
- }
- }