Mybatis_PageHelper的使用

来源:互联网 发布:人员优化方案 编辑:程序博客网 时间:2024/05/21 05:17
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @RequestMapping("list.html")  
  2. public String list(Model model, HttpServletRequest request) {  
  3.     String sqlId = "menu.mysql.selectMenu";  
  4. /       list = MybatisUtils.selectList(sqlId, SpringUtils.getParameter(request));  
  5.     Page<Object> list = (Page<Object>) MybatisUtils.selectListByPage(sqlId, SpringUtils.getParameter(request));  
  6. /       model.addAttribute("list"new PageInfo(list));  
  7.     model.addAttribute("pageNum", list.getPageNum());  
  8.     model.addAttribute("pages", list.getPages());  
  9.     model.addAttribute("list", list);  
  10.     return "menu/list";  
  11. }  
[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. @SuppressWarnings("unchecked")  
  2. public static <E> List<E> selectListByPage(String sqlId,Object parameter){  
  3.     Map<String, Object> map = (Map<String, Object>) parameter;  
  4.     String page = (String) map.get("page");  
  5.     int pageNum = 1;  
  6.     if(page != null && page.trim().length() != 0){  
  7.         pageNum = Integer.parseInt(page);  
  8.     }  
  9.     PageHelper.startPage(pageNum, Constant.pageSize, true);  
  10.     List<Object> result = getSqlSession(sqlId).selectList(sqlId, parameter);  
  11.     return dealMapResult(result);  
  12. }  


list.jsp
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <c:forEach items="${list}" var="item">  
  2.     <tr id='<c:out value="${item.menuId}"/>' align="left">  
  3.         <td><c:out value="${item.parentId}"/></td>  
  4.         <td><c:out value="${item.menuId}"/></td>  
  5.         <td><c:out value="${item.menuName}"/></td>  
  6.         <td><c:out value="${item.url}"/></td>  
  7.         <td><c:out value="${item.title}"/></td>  
  8.         <td><c:out value="${item.leaf}"/></td>  
  9.         <td><c:out value="${item.orderSeq}"/></td>  
  10.         <td onclick='goEdit(<c:out value="${item.menuId}"/>)'>编辑</td>  
  11.         <td onclick='doDelete(<c:out value="${item.menuId}"/>)'>删除</td>  
  12.     </tr>  
  13. </c:forEach>  
  14.     <tfoot>  
  15.         <tr>  
  16.             <td colspan="9" align="center" class="p">  
  17.                 <c:if test="${pageNum != 1}">  
  18.                     <a href="list.html?query=a&page=${pageNum - 1}">上一页</a>  
  19.                 </c:if>  
  20.                 <c:if test="${pages != 1}">  
  21.                     <c:forEach var="pageIndex" begin="1" end="${pages}">  
  22.                         <c:choose>  
  23.                             <c:when test="${pageNum == pageIndex}">  
  24.                                 <a>${pageIndex}</a>  
  25.                             </c:when>  
  26.                             <c:otherwise>  
  27.                                 <a href="list.html?query=a&page=${pageIndex}">${pageIndex}</a>  
  28.                             </c:otherwise>  
  29.                         </c:choose>  
  30.                     </c:forEach>  
  31.                 </c:if>  
  32.                 <c:if test="${pageNum != pages}">  
  33.                     <a href="list.html?query=a&page=${pageNum+1}">下一页</a>  
  34.                 </c:if>  
  35.             </td>  
  36.         </tr>  
  37.     </tfoot>  


mybatis-config.xml
[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. <plugins>  
  2.     <!-- packageName为PageHelper类所在包名 -->  
  3.     <plugin interceptor="org.me.common.util.PageHelper">  
  4.         <property name="dialect" value="mysql"/>  
  5.     </plugin>  
  6. </plugins>  
统一返回值为Page<E>(可以直接按List使用),方便在页面使用EL表达式,如${page.pageNum},${page.total}
对这种方式使用,一种是如果直接用Page包装list,还要单独get出来,再设置回去model.addAttribute("pageNum", list.getPageNum());
第二种是使用PageInfo对list再包装一次,页面就可以直接使用model.addAttribute("list", new PageInfo(list));
Mybatis_PageHelper
0 0