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程式碼- ...
- /** 用於分頁 */
-
private Pagination pagination = new Pagination();
- public Pagination getPagination() {
- return pagination;
- }
- public void setPagination(Pagination pagination) {
- this.pagination = pagination;
- }
此處初始化了,是為了不用判斷是否為null,相當於給一個初始值。
2. 在mapper.xml檔案中加入分頁語句本例以mysql(limit #{offSet}, #{pageSize})為例
Xml程式碼-
<select id="getUserInfoList"
- resultType="com.jayung.pagination.domain.UserInfo">
- select user_id userId, user_name userName, password password,
- age age
- from user_info
- <trim prefix="where" suffixOverrides="and">
- <if test="userId!=null and userId!=''">user_id = #{userId} and </if>
- <if test="userName!=null and userName!=''">user_name = #{userName} and </if>
- <if test="password!=null and password!=''">password = #{password} and </if>
- <if test="age!=null and age!=''">age = #{age} and </if>
- </trim>
- <span style="background-color: #ffff00;">limit #{pagination.startRow}, #{pagination.pageSize}</span>
- </select>
其中limit #{pagination.startRow}, #{pagination.pageSize}是分頁的兩個引數;
3. 定義取總記錄數的SQL
Xml程式碼- <select id="getUserInfoListCount" parameterType="com.jayung.pagination.domain.UserInfo"
- resultType="java.lang.Integer">
- select count(1)
- from user_info
- <trim prefix="where" suffixOverrides="and">
- <if test="userId!=null and userId!=''">user_id = #{userId} and </if>
- <if test="userName!=null and userName!=''">user_name = #{userName} and </if>
- <if test="password!=null and password!=''">password = #{password} and </if>
- <if test="age!=null and age!=''">age = #{age} and </if>
- </trim>
- </select>
與上面SQL的差別是將查詢結果集換成了count記錄集的總數;
3. serviceimpl
Java程式碼- public List<UserInfo> getUserInfoList(UserInfo userInfo) {
- userInfo.getPagination().setTotalRow(userInfoMapper.getUserInfoListCount(userInfo));
- return userInfoMapper.getUserInfoList(userInfo);
- }
4. controller
Java程式碼- @RequestMapping(value = "/demo/userInfo/search")
- public String search(Model model, UserInfo userInfo, HttpServletRequest request) {
- // userInfo裡含有pagination屬性。
- model.addAttribute("userInfoList", userInfoService.getUserInfoList(userInfo));
- // pagination是分頁物件
- model.addAttribute("pagination", userInfo.getPagination());
- // paginationForm是為了不丟失從查詢頁面傳過來的查詢引數,並儲存當前頁、每頁顯示數的資訊
- model.addAttribute("paginationForm", PaginationUtil.getPaginationForm(request));
- return "/demo/userInfo/userInfoList";
- }
5. paginationUtil用於儲存來自源頁面的引數,防止翻到第二頁時丟失。
Java程式碼- public static String getPaginationForm(HttpServletRequest request) {
- StringBuffer form = new StringBuffer();
- form.append("<form name=\"_paginationForm\" id=\"_paginationForm\" method=\"post\" aciton=\""
- + new UrlPathHelper().getOriginatingRequestUri(request) + "\">\n");
- Enumeration paramNames = request.getParameterNames();
- while (paramNames.hasMoreElements()) {
- String paramName = (String) paramNames.nextElement();
- String paramValue = request.getParameter(paramName);
- if (!"pagination.pageSize".equals(paramName) && !"pagination.currentPage".equals(paramName)) {
- form.append(" <input type=\"hidden\" name=\"" + paramName + "\" value=\"" + paramValue + "\" />\n");
- }
- }
- String pageSize = (request.getParameter("pagination.pageSize") == null) ? Constant.PAGE_SIZE_DEFAULT.toString()
- : request.getParameter("pagination.pageSize");
- String currentPage = (request.getParameter("pagination.currentPage") == null) ? "1" : request
- .getParameter("pagination.currentPage");
- form.append(" <input type=\"hidden\" id=\"_pagination.pageSize\" name=\"pagination.pageSize\" value=\"" + pageSize + "\" />\n");
- form.append(" <input type=\"hidden\" id=\"_pagination.currentPage\" name=\"pagination.currentPage\" value=\"" + currentPage + "\" />\n");
- form.append("</form>");
- return form.toString();
- }
6. 分頁頁面
Java程式碼- <table class="tableList" id="userInfoList">
- <tr>
- <th>userId</th>
- <th>userName</th>
- <th>password</th>
- <th>age</th>
- <th>操作</th>
- <tr>
- <c:forEach items="${userInfoList}" var="userInfo" varStatus="status">
- <tr id="${status.index}">
- <td>${userInfo.userId}</td>
- <td>${userInfo.userName}</td>
- <td>${userInfo.password}</td>
- <td>${userInfo.age}</td>
- <td><a href="${userInfo.userId}">檢視</a>
- <a href="${userInfo.userId}/edit">編輯</a>
- <a href="javascript:void();" onclick="deleteRow('${userInfo.userId}','${status.index}');">刪除</a></td>
- </tr>
- </c:forEach>
- </table>
- <div><span style="background-color: #ffff00;">${pagination.navigator}</span></div>
- span style="background-color: #ffff00;">${paginationForm}</span>
${pagination.navigator}是分頁導航
${paginationForm}是分頁表單
最後上傳完整工程附件,含初始化sql指令碼
歡迎大家討論優化。
下載連結請轉至iteye部落格地址: