1. 程式人生 > 程式設計 >pagehelper外掛顯示total為-1或1的問題

pagehelper外掛顯示total為-1或1的問題

簡單講下用法:

	//引依賴
		<dependency>
			<groupId>tk.mybatis</groupId>
			<artifactId>mapper-spring-boot-starter</artifactId>
			<version>2.1.5</version>
		</dependency>
		<dependency>
			<groupId>com.github.pagehelper</groupId>
			<artifactId>pagehelper-spring-boot-starter</artifactId>
			<version>1.2.10</version>
		</dependency>
//使用步驟
PageHelper.startPage(page,limit,true);
PageInfo<對應實體類> pageInfo = new PageInfo(對應實體類查出的list查出所有);

首先total為-1的問題:上面新增分頁引數時要加上true,判斷是否輸出真實的總數
total為1:分頁外掛對應的方法應該緊跟在PageHelper.startPage下一行,中間不能插入其他方法。

yml可以加上這些配置引數

pagehelper:
 # dialect: ①
 # 分頁外掛會自動檢測當前的資料庫連結,自動選擇合適的分頁方式(可以不設定)
 helper-dialect: oracle
 # 上面資料庫設定後,下面的設定為true不會改變上面的結果(預設為true)
 auto-dialect: true
 page-size-zero: false # ②
 reasonable: true # ③
 # 預設值為 false,該引數對使用 RowBounds 作為分頁引數時有效。(一般用不著)
 offset-as-page-num: false
 # 預設值為 false,RowBounds是否進行count查詢(一般用不著)
 row-bounds-with-count: false
 #params: ④
 #support-methods-arguments: 和params配合使用,具體可以看下面的講解
 # 預設值為 false。設定為 true 時,允許在執行時根據多資料來源自動識別對應方言的分頁
 auto-runtime-dialect: false # ⑤
 # 與auto-runtime-dialect配合使用
 close-conn: true
 # 用於控制預設不帶 count 查詢的方法中,是否執行 count 查詢,這裡設定為true後,total會為-1
 default-count: false
 #dialect-alias: ⑥

ps:PageHelper新手使用教程

剛剛使用了PageHelper分頁工具,簡單寫一下

如果是SpringBoot工程,大家可以省略第一步

1.首先先配置一下mybatis.xml檔案,然後再配置檔案中寫入以下程式碼

<plugins> 
   <plugin interceptor="com.github.pagehelper.PageInterceptor" />
</plugins>

2.其實就可以在Controller裡呼叫PageHelper

public Result getall(@RequestParam(value="page",defaultValue="1")Integer page,@RequestParam(value = "limit",defaultValue = "10") Integer limit) {

    //這個一定要放在第一行,否則無法進行分頁
    PageHelper.startPage(page,limit);  

    List<User> userList =userService.getAll();
    
    //分頁
    PageInfo pageInfo = new PageInfo(userList);
   
    //pageInfo.getTotal資料總條數
    return Result.success(userList,pageInfo .getTotal());

}

總結

到此這篇關於pagehelper外掛顯示total為-1或1的文章就介紹到這了,更多相關pagehelper外掛顯示total為-1或1內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!