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代码 
  1. ...  
  2. /** 用于分页 */  
  3. private Pagination pagination = new Pagination();  
  4.   
  5. public Pagination getPagination() {  
  6.     return pagination;  
  7. }  
  8.   
  9. public void setPagination(Pagination pagination) {  
  10.     this.pagination = pagination;  
  11. }  

此处初始化了,是为了不用判断是否为null,相当于给一个初始值。 

 

2.  在mapper.xml文件中加入分页语句本例以mysql(limit #{offSet}, #{pageSize})为例

 

Xml代码 
  1. <select id="getUserInfoList" parameterType="com.jayung.pagination.domain.UserInfo"  
  2.      resultType="com.jayung.pagination.domain.UserInfo">  
  3.     select user_id userId, user_name userName, password password,   
  4.        age age   
  5.       from user_info   
  6.      <trim prefix="where" suffixOverrides="and">  
  7.         <if test="userId!=null and userId!=''">user_id = #{userId} and </if>  
  8.         <if test="userName!=null and userName!=''">user_name = #{userName} and </if>  
  9.         <if test="password!=null and password!=''">password = #{password} and </if>  
  10.         <if test="age!=null and age!=''">age = #{age} and </if>  
  11.      </trim>  
  12.      <span style="background-color: #ffff00;">limit #{pagination.startRow}, #{pagination.pageSize}</span>  
  13. </select>  

 其中limit #{pagination.startRow}, #{pagination.pageSize}是分页的两个参数;

 

 

3. 定义取总记录数的SQL

 

Xml代码 
  1. <select id="getUserInfoListCount" parameterType="com.jayung.pagination.domain.UserInfo"  
  2.      resultType="java.lang.Integer">  
  3.     select count(1)  
  4.       from user_info   
  5.      <trim prefix="where" suffixOverrides="and">  
  6.         <if test="userId!=null and userId!=''">user_id = #{userId} and </if>  
  7.         <if test="userName!=null and userName!=''">user_name = #{userName} and </if>  
  8.         <if test="password!=null and password!=''">password = #{password} and </if>  
  9.         <if test="age!=null and age!=''">age = #{age} and </if>  
  10.      </trim>  
  11. </select>  

 与上面SQL的差别是将查询结果集换成了count记录集的总数;

 

3. serviceimpl

Java代码 
  1. public List<UserInfo> getUserInfoList(UserInfo userInfo) {  
  2.     userInfo.getPagination().setTotalRow(userInfoMapper.getUserInfoListCount(userInfo));  
  3.     return userInfoMapper.getUserInfoList(userInfo);  
  4. }  

 

 4. controller

Java代码 
  1. @RequestMapping(value = "/demo/userInfo/search")  
  2. public String search(Model model, UserInfo userInfo, HttpServletRequest request) {  
  3.     // userInfo里含有pagination属性。  
  4.     model.addAttribute("userInfoList", userInfoService.getUserInfoList(userInfo));  
  5.     // pagination是分页对象  
  6.     model.addAttribute("pagination", userInfo.getPagination());  
  7.     // paginationForm是为了不丢失从查询页面传过来的查询参数,并保存当前页、每页显示数的信息  
  8.     model.addAttribute("paginationForm", PaginationUtil.getPaginationForm(request));  
  9.     return  "/demo/userInfo/userInfoList";  
  10. }  

 

5. paginationUtil用于保存来自源页面的参数,防止翻到第二页时丢失。

Java代码 
  1. public static String getPaginationForm(HttpServletRequest request) {  
  2.     StringBuffer form = new StringBuffer();  
  3.     form.append("<form name=\"_paginationForm\" id=\"_paginationForm\" method=\"post\" aciton=\""  
  4.             + new UrlPathHelper().getOriginatingRequestUri(request) + "\">\n");  
  5.     Enumeration paramNames = request.getParameterNames();  
  6.     while (paramNames.hasMoreElements()) {  
  7.         String paramName = (String) paramNames.nextElement();  
  8.         String paramValue = request.getParameter(paramName);  
  9.         if (!"pagination.pageSize".equals(paramName) && !"pagination.currentPage".equals(paramName)) {  
  10.             form.append("   <input type=\"hidden\" name=\"" + paramName + "\" value=\"" + paramValue + "\" />\n");  
  11.         }  
  12.     }  
  13.     String pageSize = (request.getParameter("pagination.pageSize") == null) ? Constant.PAGE_SIZE_DEFAULT.toString()  
  14.             : request.getParameter("pagination.pageSize");  
  15.     String currentPage = (request.getParameter("pagination.currentPage") == null) ? "1" : request  
  16.             .getParameter("pagination.currentPage");  
  17.     form.append("   <input type=\"hidden\" id=\"_pagination.pageSize\" name=\"pagination.pageSize\" value=\"" + pageSize + "\" />\n");  
  18.     form.append("   <input type=\"hidden\" id=\"_pagination.currentPage\" name=\"pagination.currentPage\" value=\"" + currentPage + "\" />\n");  
  19.     form.append("</form>");  
  20.     return form.toString();  
  21. }  

 

6. 分页页面

Java代码 
  1. <table class="tableList" id="userInfoList">  
  2. <tr>  
  3.         <th>userId</th>  
  4.         <th>userName</th>  
  5.         <th>password</th>  
  6.         <th>age</th>  
  7.     <th>操作</th>  
  8. <tr>  
  9.     <c:forEach items="${userInfoList}" var="userInfo" varStatus="status">  
  10.     <tr id="${status.index}">  
  11.         <td>${userInfo.userId}</td>  
  12.         <td>${userInfo.userName}</td>  
  13.         <td>${userInfo.password}</td>  
  14.         <td>${userInfo.age}</td>  
  15.         <td><a href="${userInfo.userId}">查看</a>   
  16.         <a href="${userInfo.userId}/edit">编辑</a>   
  17.         <a href="javascript:void();" οnclick="deleteRow('${userInfo.userId}','${status.index}');">删除</a></td>  
  18.     </tr>  
  19.     </c:forEach>  
  20. </table>  
  21. <div><span style="background-color: #ffff00;">${pagination.navigator}</span></div>  
  22. span style="background-color: #ffff00;">${paginationForm}</span>  

 ${pagination.navigator}是分页导航

${paginationForm}是分页表单

 

最后上传完整工程附件,含初始化sql脚本

欢迎大家讨论优化。


下载链接请转至iteye博客地址:

http://jayung.iteye.com/blog/2065112

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值