1. 程式人生 > >PageHelper+Bootstrap Paginator實現分頁效果

PageHelper+Bootstrap Paginator實現分頁效果

最近需要做一個分頁,找了挺多的前端分頁效果,發現Bootstrap Paginator分頁外掛效果不錯,而我頁面也是用BootStrap做的響應式頁面,就做了實現一個簡單的分頁效果。

在SpringBoot+Mybatis做分頁

1.首先新增maven依賴

<dependency>
  <groupId>com.github.pagehelper</groupId>
  <artifactId>pagehelper</artifactId>
  <version>4.1.6</version>
</dependency>

2.新增一個PageHelperConfig配置類

@Configuration
public class PageHelperConfig {
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper = new PageHelper();
        Properties properties = new Properties();
        properties.setProperty("offsetAsPageNum", "true");
        properties.setProperty("rowBoundsWithCount", "true");
        properties.setProperty("reasonable", "true");
        properties.setProperty("reasonable","true");
        properties.setProperty("pageSizeZero", "true");
        pageHelper.setProperties(properties);
        return pageHelper;
    }
}

3.寫一個用來儲存分頁資訊的物件

public class PageBean<T> implements Serializable {
    private static final long serialVersionUID = 1L;
    private long total; //總記錄數
    private List<T> list; //結果集
    private int pageNum; //第幾頁
    private int pageSize; //每頁記錄數
    private int pages; // 總頁數
    private int size; //當前頁的數量<=pageSize

    public PageBean(List<T> list){
        if (list instanceof Page){
            Page<T> page = (Page<T>) list;
            this.pageNum = page.getPageNum();
            this.pageSize = page.getPageSize();
            this.total = page.getTotal();
            this.pages = page.getPages();
            this.list = page;
            this.size = page.size();
        }
    }
    public long getTotal() {
        return total;
    }
    public void setTotal(long total) {
        this.total = total;
    }
    public List<T> getList() {
        return list;
    }

    public void setList(List<T> list) {
        this.list = list;
    }
    public int getSize() {
        return size;
    }
    public void setSize(int size) {
        this.size = size;
    }
    public int getPageNum() {
        return pageNum;
    }
    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }
    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public int getPages() {
        return pages;
    }
    public void setPages(int pages) {
        this.pages = pages;
    }
}

4.在Service介面和實現上,我們可以查詢到的把資料傳到PageBean

public interface ArticleService {
    PageBean<Article> selectArticleList(int pageNum, int pageSize);
}

@Service
public class ArticleServiceImpl implements ArticleService {
    @Autowired
    private ArticleMapper articleMapper;
    public PageBean<Article> selectArticleList(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Article> list = this.articleMapper.selectArticleList();
        return new PageBean<>(list);
    }
}

5.Controller

@RestController
public class ArticleJsonController {
    @Autowired
    private ArticleService articleService;
    @GetMapping("/articleManage/articlePageBean")
    public PageBean articlePageBean(int pageNum, int pageSize) {
        PageBean<Article> pageBean = this.articleService.selectArticleList(pageNum, pageSize);
        return pageBean;
    }
}

6.頁面用Ajax無重新整理分頁,新增Bootstrap Paginator外掛,bootstrap-paginator.js和bootstrap-responsive.min.css樣式
<div id="content" class="container"></div>
<div class="container">
    <div class="panel col-sm-10 panel-default">
        <div class="panel-body reply-top media">
            <div id="page"></div>
        </div>
    </div>
</div>

function pagehtml(pageNum) {
    var articleid = $("#articleNum").val();
    if (pageNum == 0){
        pageNum=1;
    } else {
        pageNum=pageNum;
    }
    $.ajax({
        url:"/TsengBlog/article/replyPage",
        type:"get",
        dataType:"json",
        data: {pageNum : pageNum, pageSize : 5, articleid : articleid },
        success:function (data) {
            var options = {
                alignment:"center",//居中顯示
          currentPage: data.pageNum,//當前頁數
          totalPages: data.pages,//總頁數 注意不是總條數
          pageUrl: function(type, page, current){
                    if (page==current) {
                        return "javascript:void(0)";
                    } else {
                        return "javascript:pagehtml("+page+")";
                    }
                }
            };
            var html="";
            $.each(data.list, function (i, obj) {
                html += '<div class="panel col-sm-10 panel-default">'+
                        '<div class="panel-body reply-top media">'+
                        '<div class="col-sm-3 col-md-2 media-left">'+
                        '<img src="image/Tseng.png" class="img-circle img-thumbnail" />'+
                        '<p class="p-css">'+obj.name+'</p>'+
                        '</div>'+
                        '<div class="col-sm-10">'+
                        '<div class="text-right">#'+i+'</div>'+
                        '<hr/>'+
                        '<div class="reply-content">'+
                        '<span>'+obj.content+'</span>'+
                        '</div>'+
                        '</div>'+
                        '</div>'+
                        '</div>';
            });
            $("#content").empty();
            $("#content").append(html);
            $("#page").bootstrapPaginator(options);
        },
        error:function (error) {
            alert("error");
        }
    });
}

如果是Maven+SSM的,SSM整合就不多說了,網上有很多部落格和例子整合。

1.mybatis.xml配置

<configuration>
    <plugins>
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <property name="dialect" value="mysql" />
            <property name="pageSizeZero" value="true" />
        <property name="offsetAsPageNum" value="true" />  
        <property name="reasonable" value="true" />
        <property name="rowBoundsWithCount" value="true" />
        </plugin>
    </plugins>
</configuration>

2.service
public PageInfo page(pageNum, pageSize){
    PageHelper.startPage(pageNum, pageSize);
    List<ShopNav> lists = navService.getAllNav();
    PageInfo<ShopNav> pageInfo = new PageInfo<ShopNav>(lists);
  return PageInfo; 
}

頁面效果展示