你的位置:首页 > Java教程

[Java教程]SpringMVC+MyBatis分页插件简单实现


1.封装分页Page类

复制代码
 1 package com.framework.common.page.impl; 2  3 import java.io.Serializable; 4  5 import com.framework.common.page.IPage; 6 /** 7 *  8 *  9 * 10 */ 11 public abstract class BasePage implements IPage, Serializable { 12 13   /** 14   * 15   */ 16   private static final long serialVersionUID = -3623448612757790359L; 17   18   public static int DEFAULT_PAGE_SIZE = 20; 19   private int pageSize = DEFAULT_PAGE_SIZE; 20   private int currentResult; 21   private int totalPage; 22   private int currentPage = 1; 23   private int totalCount = -1; 24 25   public BasePage(int currentPage, int pageSize, int totalCount) { 26     this.currentPage = currentPage; 27     this.pageSize = pageSize; 28     this.totalCount = totalCount; 29   } 30 31   public int getTotalCount() { 32     return this.totalCount; 33   } 34 35   public void setTotalCount(int totalCount) { 36     if (totalCount < 0) { 37       this.totalCount = 0; 38       return; 39     } 40     this.totalCount = totalCount; 41   } 42 43   public BasePage() { 44   } 45 46   public int getFirstResult() { 47     return (this.currentPage - 1) * this.pageSize; 48   } 49 50   public void setPageSize(int pageSize) { 51     if (pageSize < 0) { 52       this.pageSize = DEFAULT_PAGE_SIZE; 53       return; 54     } 55     this.pageSize = pageSize; 56   } 57 58   public int getTotalPage() { 59     if (this.totalPage <= 0) { 60       this.totalPage = (this.totalCount / this.pageSize); 61       if ((this.totalPage == 0) || (this.totalCount % this.pageSize != 0)) { 62         this.totalPage += 1; 63       } 64     } 65     return this.totalPage; 66   } 67 68   public int getPageSize() { 69     return this.pageSize; 70   } 71 72   public void setPageNo(int currentPage) { 73     this.currentPage = currentPage; 74   } 75 76   public int getPageNo() { 77     return this.currentPage; 78   } 79 80   public boolean isFirstPage() { 81     return this.currentPage <= 1; 82   } 83 84   public boolean isLastPage() { 85     return this.currentPage >= getTotalPage(); 86   } 87 88   public int getNextPage() { 89     if (isLastPage()) { 90       return this.currentPage; 91     } 92     return this.currentPage + 1; 93   } 94 95   public int getCurrentResult() { 96     this.currentResult = ((getPageNo() - 1) * getPageSize()); 97     if (this.currentResult < 0) { 98       this.currentResult = 0; 99     }100     return this.currentResult;101   }102 103   public int getPrePage() {104     if (isFirstPage()) {105       return this.currentPage;106     }107     return this.currentPage - 1;108   }109 110 111 }
复制代码

 

复制代码
 1 package com.framework.common.page.impl; 2 3 import java.util.List; 4 /** 5 * 6 * 7 * 8 */ 9 public class Page extends BasePage {10 11   /**12   * 13   */14   private static final long serialVersionUID = -970177928709377315L;15 16   public static ThreadLocal<Page> threadLocal = new ThreadLocal<Page>();17 18   private List<?> data; 19   20   public Page() {21   }22 23   public Page(int currentPage, int pageSize, int totalCount) {24     super(currentPage, pageSize, totalCount);25   }26 27   public Page(int currentPage, int pageSize, int totalCount, List<?> data) {28     super(currentPage, pageSize, totalCount);29     this.data = data;30   }31 32   public List<?> getData() {33     return data;34   }35 36   public void setData(List<?> data) {37     this.data = data;38   }39   40 41 }
复制代码

2.封装分页插件

复制代码
 1 package com.framework.common.page.plugin; 2  3 import java.sql.Connection; 4 import java.sql.PreparedStatement; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 import java.util.List; 8 import java.util.Properties; 9 10 import javax.复制代码

3.MyBatis配置文件:mybatis-config.复制代码

 1 <?复制代码

4.分页拦截器

复制代码
 1 package com.framework.common.page.interceptor; 2 3 import javax.servlet.http.HttpServletRequest; 4 import javax.servlet.http.HttpServletResponse; 5 6 import org.apache.commons.lang3.math.NumberUtils; 7 import org.springframework.web.servlet.ModelAndView; 8 import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; 9 10 import com.framework.common.page.impl.Page;11 /**12 * 13 * 14 *15 */16 public class PageInterceptor extends HandlerInterceptorAdapter {17 18   @Override19   public void postHandle(HttpServletRequest request,20       HttpServletResponse response, Object handler,21       ModelAndView modelAndView) throws Exception {22     super.postHandle(request, response, handler, modelAndView);23     Page page = Page.threadLocal.get();24     if (page != null) {25       request.setAttribute("page", page);26     }27     Page.threadLocal.remove();28   }29 30   @Override31   public boolean preHandle(HttpServletRequest request,32       HttpServletResponse response, Object handler) throws Exception {33     String pageSize = request.getParameter("pageSize");34     String pageNo = request.getParameter("pageNo");35     Page page = new Page();36     if (NumberUtils.isNumber(pageSize)) {37       page.setPageSize(NumberUtils.toInt(pageSize));38     }39     if (NumberUtils.isNumber(pageNo)) {40       page.setPageNo(NumberUtils.toInt(pageNo));41     }42     Page.threadLocal.set(page);43     return true;44   }45 46 }
复制代码

5.Spring配置

复制代码
 1   <!-- =================================================================== 2   - Load property file 3   - =================================================================== --> 4   <context:property-placeholder location="classpath:application.properties" /> 5   6   <bean id="sqlSessionFactory" > 7     <property name="dataSource" ref="dataSource" /> 8     <property name="configLocation" value="classpath:mybatis-config.复制代码

6.SpringMVC配置拦截器

复制代码
 1 <!-- 分页拦截器 --> 2   <bean id="pageInterceptor" ></bean> 3   4   <!-- 配置拦截器 --> 5   <bean > 6     <property name="interceptors"> 7       <list> 8         <ref bean="pageInterceptor" /> 9       </list>10     </property>11   </bean>
复制代码

 

请参看以下资料:

http://www.itnose.net/detail/6074493.html