MyBatis分页插件的使用

   01 原始案例

 

   

 

 

 

 

 2 JavaWeb分页技术

 

 02 封装PageBean

我们需要的元素?

currentPage(当前页),pageSize(每页记录数)

totalPage(总页数)

pageList(当前页面封装的数据集合)

 03 改造后端代码——BookInfoServlet

protected void doGet(HttpServletRequest request, HttpServletResponse response)

  throws ServletException, IOException {

  String action = request.getParameter("action");

  if ("list".equals(action)) {

  // 该方式作废

  listBook(request, response);

  } else if ("listByPage".equals(action)) {

                                 //分页查询列表

  listByPage(request, response);

  }

  }

private void listByPage(HttpServletRequest request, HttpServletResponse response) {

  // 获取请求参数:当前页码,每页信息数

  String cp = request.getParameter("currentPage");

  String is = request.getParameter("initSize");

  // 赋予默认初值

  Integer currentPage = 1;Integer initSize = 5;

  if (cp != null && !"".equals(cp)) {currentPage = Integer.valueOf(cp);}

  if (is != null && !"".equals(is)) {initSize = Integer.valueOf(is);}

  // 获取当前页的书籍记录

  List<BookInfo> bookPageList = bookInfoService.getPageList(currentPage, initSize);

  // 获取总记录数

  int count = bookInfoService.getBookCount();

  PageBean<BookInfo> bookPageBeans = new PageBean<BookInfo>(initSize, count, currentPage, bookPageList);

  request.setAttribute("bookPageBeans", bookPageBeans);

  System.out.println(bookPageBeans);

  try {request.getRequestDispatcher("index.jsp").forward(request, response);

  } catch (Exception e) {e.printStackTrace();}

  }

03 改造后端代码——Service

//获取总页数

public int getBookCount() {

  // TODO Auto-generated method stub

  return dao.getBookCount();

  }

//获取当前页记录列表 

public List<BookInfo> getPageList(Integer currentPage, Integer initSize) {

  // TODO Auto-generated method stub

  return dao.getPageList(currentPage, initSize);

  }

03 改造后端代码——  Dao

public int getBookCount() {

  String sql = "select count(*) from book_info;";

  PreparedStatement pstmt = null;

  ResultSet rs = null;

  try {

  pstmt = conn.prepareStatement(sql);

  rs = pstmt.executeQuery();

  rs.next();

  int count = rs.getInt(1);

  return count;

  } catch (SQLException e) {

  // TODO Auto-generated catch block

  e.printStackTrace();

  }

  return 0;

  }

04 改造前端代码

       <table class="table table-hover">

   <tr>

   <th>图书编号</th>

   <th>类别</th>

   <th>图书名称</th>

               <th>作者</th>

  <th>价格</th>

   <th>出版社</th>

   <th>出版时间</th>

  </tr>

           <c:forEach items="${bookPageBeans.pageList}" var="book">

             <tr>

              <td>${book.bookId}</td>

              <td>${book.typeName}</td>

              <td>${book.bookName}</td>

              <td>${book.author}</td>

              <td>${book.price}</td>

              <td>${book.publisher}</td>

              <td>${book.publishTime}</td>

             </tr>

            </c:forEach>

</table>

 05 效果展示

3 MyBatis分页插件

 MyBatis  jar

 

MySql驱动  jar

所需jar包汇总

 

02 案例准备

03 简单使用PageHelper插件

04 PageHelper插件说明

 

 

 

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值