*******************************************分页查询开始***************************************************
        /**
         * 多参传入的方法:
         *     1:多个不同类型的参数入参时,映射文件中可以不指定参数类型,
         *        接口中的方法使用注解,将注解指定的名字,传入映射文件相应属性中
         *     2:也可以把多个参数封装成对象,以对象类型入参
  * 分页查询
  * @param from  从第几条开始查...
  * @param pagesize  每页显示的记录数
  * @return  当前页对象的集合
  */
  public List<User> fenye(@Param("from")Integer from,@Param("pagesize")Integer pagesize);
******************
         <select >
  SELECT count(*) FROM users
  </select>

*****************************
    protected void service(HttpServletRequest req, HttpServletResponse res)
   throws ServletException, IOException {
  req.setCharacterEncoding("utf-8");
  res.setContentType("text/html;charset=utf-8");
  
  SqlSession session=MybatisUtil.getSession(); //获得mybatis的session对象
  try {
   Integer pagesize=3; //每页显示的记录数
   Integer page=null;  //当前页
   String pageQian=req.getParameter("page");
   if(pageQian !=null){
    page=Integer.parseInt(pageQian);
   }else{
    page=1;  //默认看第一页
   }
   /*
    * 数据库查询总页数
    */
   Integer count=session.getMapper(UserMapper.class).count();
   int totalyeshu=0;    //总页数
   if(count%pagesize==0){
    totalyeshu=count/pagesize;
   }else{
    totalyeshu=(count/pagesize)+1;
   }
   /*
    * 数据库查询当前页的数据
    */
   List<User> userList=session.getMapper(UserMapper.class).fenye((page-1)*pagesize, pagesize);
                        /*
    * 将数传给前台
    */
   req.setAttribute("USERLIST", userList);  //集合数据
   req.setAttribute("PAGE", page);  //当前页
   req.setAttribute("COUNT", totalyeshu);  //总页数
  } catch (Exception e) {
   e.printStackTrace();
  }finally{
   MybatisUtil.closeSession(session);
  }
  req.getRequestDispatcher("indexFenye.jsp").forward(req, res); //转发至主页
   }
*****************************
   <div>
      <h3 algin="right">
        &lt;
          <span style="font-size:12px;">共${COUNT }页 &nbsp;/&nbsp;第${PAGE}页   &nbsp;</span>
          <c:if test="${PAGE>1 }">
                <a href="fenye?page=1">首页</a>&nbsp;
                <a href="fenye?page=${PAGE-1}">上页</a>&nbsp;
          </c:if>
          <c:forEach begin="1" end="${COUNT }" var="i">
             <a href="fenye?page=${i}" >${i}</a>
          </c:forEach>
          <c:if test="${PAGE<COUNT }">
               <a href="fenye?page=${PAGE+1}">下页</a>&nbsp;
               <a href="fenye?page=${COUNT}">尾页</a>&nbsp;
          </c:if>
     
          <span style="font-size:12px;">GO</span>&nbsp;
          <select +page;
        }
     </script>
   </div>

*******************************************分页查询结束***************************************************

相关文章: