开发者社区> 问答> 正文

mybatis分页的

各位用mybatis,是怎么分页的呢

是直接用sql语句份,还是用插件

展开
收起
a123456678 2016-03-18 14:24:49 2431 0
2 条回答
写回答
取消 提交回答
  • 1,前端和后端之间约定的信息应该是PageSize、PageNum。
    2,后端在分页之前先查询一把数据总数。
    3,基于数据总数、PageSize以及PageNum就可以计算出MySQL中需要的Limit和OffSet值。

    代码示例:
    一,查询基类
    package com.freedom.dal.common;

    /**

    • 分页查询的基类
    • */
    1. class QueryBase {

         //数据总量

      private Integer totalSize;
      //每页大小
      private Integer pageSize;
      //当前页数
      private Integer currentPage;

      // for paging
      private int startRow;
      private int endRow;

      protected Integer getDefaultPageSize() {

         return defaultPageSize;

      }

      public boolean isFirstPage() {

         return this.getCurrentPage().intValue() == 1;

      }

      public int getPreviousPage() {

         int back = this.getCurrentPage().intValue() - 1;
      
         if (back <= 0) {
             back = 1;
         }
      
         return back;

      }

      public boolean isLastPage() {

         return this.getTotalPage() == this.getCurrentPage().intValue();

      }

      public int getNextPage() {

         int back = this.getCurrentPage().intValue() + 1;
      
         if (back > this.getTotalPage()) {
             back = this.getTotalPage();
         }
      
         return back;

      }

      public Integer getCurrentPage() {

         if (currentPage == null) {
             return defaultFristPage;
         }
      
         return currentPage;

      }

      public void setCurrentPageString(String pageString) {

         if (isBlankString(pageString)) {
             this.setCurrentPage(defaultFristPage);
         }
      
         try {
             Integer integer = new Integer(pageString);
      
             this.setCurrentPage(integer);
         } catch (NumberFormatException ignore) {
             this.setCurrentPage(defaultFristPage);
         }

      }

      public void setCurrentPage(Integer cPage) {

         if ((cPage == null) || (cPage.intValue() <= 0)) {
             this.currentPage = null;
         } else {
             this.currentPage = cPage;
         }
         setStartEndRow();

      }

      private void setStartEndRow() {

         this.startRow = this.getPageSize().intValue() * (this.getCurrentPage().intValue() - 1) + 1;
         this.endRow = this.startRow + this.getPageSize().intValue() - 1;

      }

      public Integer getPageSize() {

         if (pageSize == null) {
             return getDefaultPageSize();
         }
      
         return pageSize;

      }

      public boolean hasSetPageSize() {

         return pageSize != null;

      }

      public void setPageSizeString(String pageSizeString) {

         if (isBlankString(pageSizeString)) {
             return;
         }
      
         try {
             Integer integer = new Integer(pageSizeString);
      
             this.setPageSize(integer);
         } catch (NumberFormatException ignore) {
         }

      }

      private boolean isBlankString(String pageSizeString) {

         if (pageSizeString == null) {
             return true;
         }
      
         if (pageSizeString.trim().length() == 0) {
             return true;
         }
      
         return false;

      }

      public void setPageSize(Integer pSize) {

         if ((pSize == null) || (pSize.intValue() <= 0)) {
             this.pageSize = null;
         } else {
             this.pageSize = pSize;
         }
         setStartEndRow();

      }

      public Integer getTotalItem() {

         if (totalSize == null) {
             return defaultTotleItem;
         }
      
         return totalSize;

      }

      public void setTotalItem(Integer tItem) {

         if (tItem == null) {
             throw new IllegalArgumentException("TotalItem can't be null.");
         }
      
         this.totalSize = tItem;
      
         int current  = this.getCurrentPage().intValue();
         int lastPage = this.getTotalPage();
      
         if (current > lastPage) {
             this.setCurrentPage(new Integer(lastPage));
         }

      }

      public int getTotalPage() {

         int pgSize = this.getPageSize().intValue();
         int total  = this.getTotalItem().intValue();
         int result = total / pgSize;
      
         if ((total == 0) || ((total % pgSize) != 0)) {
             result++;
         }
      
         return result;

      }

      public int getPageFristItem() {

         int cPage = this.getCurrentPage().intValue();
      
         if (cPage == 1) {
             return 1;
         }
      
         cPage--;
      
         int pgSize = this.getPageSize().intValue();
      
         return (pgSize * cPage) + 1;

      }

      public int getPageLastItem() {

         int cPage      = this.getCurrentPage().intValue();
         int pgSize     = this.getPageSize().intValue();
         int assumeLast = pgSize * cPage;
         int totalItem  = getTotalItem().intValue();
      
         if (assumeLast > totalItem) {
             return totalItem;
         } else {
             return assumeLast;
         }

      }

      public int getEndRow() {

         return endRow;

      }

      public void setEndRow(int endRow) {

         this.endRow = endRow;

      }

      public int getStartRow() {

         return startRow;

      }

      public void setStartRow(int startRow) {

         this.startRow = startRow;

      }

      protected String getSQLBlurValue(String value) {

         if (value == null) {
             return null;
         }
      
         return value + '%';

      }

      public boolean nextPage() {

          if(this.currentPage != null && this.currentPage.intValue() >= this.getTotalPage()) return false;
          if(this.currentPage == null) {
              this.setCurrentPage(defaultFristPage);
          } else {
              this.setCurrentPage(getNextPage());
          }
          return true;
      }
      

      public int getStart() {

         int startRow = getStartRow() - 1;
         return startRow > 0 ? startRow : 0;

      }
      }

    二,DAO中基本逻辑
    1,生成查询基类,并将前端传递过来的pageSize和pageNum设置给查询基类。
    2,查询数据总量,将数据总量设置到查询基类中。
    3,查询。

    三,xml配置
    SELECT

            <include refid="ConfigDOColums"></include>
        FROM
            CONFIGS
        WHERE
            <include refid="configQueryConditions"></include>
        LIMIT #{start}, #{pageSize}
    2019-07-17 19:06:37
    赞同 展开评论 打赏
  • 创建一个分页类pageBean,所有的类可以继承它,在你的Action或者Controller里面,全部将实体类参数全部封装到Map,mybatis的参数全部采用Map传递比较好,然后写一个通用的sql头和sql尾,把你的业务逻辑sql都写在头和尾之间,就可以了。这也是一个笨方法,看你怎么写了

    2019-07-17 19:06:37
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
Java Spring Boot开发实战系列课程【第6讲】:Spring Boot 2.0实战MyBatis与优化(Java面试题) 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载