天天看點

Springboot中使用PageHelper插件實作分頁效果

最近在寫mybatis+springboot項目的時候要用到分頁插件,在看了網上很多的文章之後,發現在springboot後端直接使用PageHelper插件還是比較簡單的

  • 首先,為項目添加依賴

    <dependency>
         <groupId>com.github.pagehelper</groupId>
         <artifactId>pagehelper</artifactId>
         <version>4.1.6</version>
     </dependency>`
               
  • 在Controller中編寫代碼,調用PageHelper.start(page,pagesize參數)

    public List<MsgReturn> countMsg(@RequestParam("page"),
    								@RequsetParam("pageSize")){
    	List<MsgReturn> msgReturns  = listingsService.countMsg();//查找傳回所有對象
    	PageHepler.startPage(page,pageSize);//調用分頁方法
    	return msgReturn;//傳回分頁後的結果
    }
               

    沒錯,就是這麼簡單,隻需要調用一個方法,就可以顯示資料庫傳回資料後的分頁結果。