Mybatis使用pageHelper步驟


1.在pom.xml中添加如下依賴:

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>最新版本</version>
</dependency>

 

2.配置攔截器插件,有兩種方法:

2.1 在spring的配置文件中配置攔截器插件:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <!-- 注意其他配置 -->
  <property name="plugins">
    <array>
      <bean class="com.github.pagehelper.PageInterceptor">
        <property name="properties">
          <!--使用下面的方式配置參數,一行配置一個,后面會有所有的參數介紹 -->
          <value>

        helperDialect=mysql
        reasonable=true
        supportMethodsArguments=true
        params=count=countSql
        autoRuntimeDialect=true

      </value>
        </property>
      </bean>
    </array>
  </property>
</bean>

2.2 在mybatis的配置文件中配置攔截器插件:

在SqlSessionFactoryBean中添加configLocation

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
    <property name="dataSource" ref="dataSource" />  
    <property name="mapperLocations" value="classpath:joker/itq/im/mapping/*.xml" />  
    <property name="configLocation" value="classpath:mybatis-config.xml" />  
</bean>

在mybatis-config.xml中添加plugin

<!-- 
    plugins在配置文件中的位置必須符合要求,否則會報錯,順序如下:
    properties?, settings?, 
    typeAliases?, typeHandlers?, 
    objectFactory?,objectWrapperFactory?, 
    plugins?, 
    environments?, databaseIdProvider?, mappers?
-->
<plugins>
    <!-- com.github.pagehelper為PageHelper類所在包名 -->
    <plugin interceptor="com.github.pagehelper.PageInterceptor">
        <!-- 使用下面的方式配置參數,后面會有所有的參數介紹 -->
        <property name="param1" value="value1"/>
    </plugin>
</plugins>

 

分頁插件參數介紹: 

  • helperDialect:分頁插件會自動檢測當前的數據庫鏈接,自動選擇合適的分頁方式。 你可以配置helperDialect屬性來指定分頁插件使用哪種方言。配置時,可以使用下面的縮寫值:

    oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby

    特別注意:使用 SqlServer2012 數據庫時,需要手動指定為 sqlserver2012,否則會使用 SqlServer2005 的方式進行分頁。

    你也可以實現 AbstractHelperDialect,然后配置該屬性為實現類的全限定名稱即可使用自定義的實現方法。

  • offsetAsPageNum:默認值為 false,該參數對使用 RowBounds 作為分頁參數時有效。 當該參數設置為 true 時,會將 RowBounds 中的 offset 參數當成 pageNum 使用,可以用頁碼和頁面大小兩個參數進行分頁。 
  • rowBoundsWithCount:默認值為false,該參數對使用 RowBounds 作為分頁參數時有效。 當該參數設置為true時,使用 RowBounds 分頁會進行 count 查詢。
  • pageSizeZero:默認值為 false,當該參數設置為 true 時,如果 pageSize=0 或者 RowBounds.limit = 0 就會查詢出全部的結果(相當於沒有執行分頁查詢,但是返回結果仍然是 Page 類型)。
  • reasonable:分頁合理化參數,默認值為false。當該參數設置為 true 時,pageNum<=0 時會查詢第一頁,pageNum>pages(超過總數時),會查詢最后一頁。默認false 時,直接根據參數進行查詢。
  • params:為了支持startPage(Object params)方法,增加了該參數來配置參數映射,用於從對象中根據屬性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默認值, 默認值為pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero
  • supportMethodsArguments:支持通過 Mapper 接口參數來傳遞分頁參數,默認值false,分頁插件會從查詢方法的參數值中,自動根據上面 params 配置的字段中取值,查找到合適的值時就會自動分頁。 使用方法可以參考測試代碼中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和 ArgumentsObjTest
  • autoRuntimeDialect:默認值為 false。設置為 true 時,允許在運行時根據多數據源自動識別對應方言的分頁 (不支持自動選擇sqlserver2012,只能使用sqlserver)。
  • closeConn:默認值為 true。當使用運行時動態數據源或沒有設置 helperDialect 屬性自動獲取數據庫類型時,會自動獲取一個數據庫連接, 通過該屬性來設置是否關閉獲取的這個連接,默認true關閉,設置為 false 后,不會關閉獲取的連接,這個參數的設置要根據自己選擇的數據源來決定 

 

3.在代碼中使用分頁插件,常用的使用方式有:

//第一種,RowBounds方式的調用
List<Country> list = sqlSession.selectList("x.y.selectIf", null, new RowBounds(0, 10));

//第二種,Mapper接口方式的調用,推薦這種使用方式。
PageHelper.startPage(1, 10);
List<Country> list = countryMapper.selectIf(1);

//第三種,Mapper接口方式的調用,推薦這種使用方式。
PageHelper.offsetPage(1, 10);
List<Country> list = countryMapper.selectIf(1);

//第四種,參數方法調用
//存在以下 Mapper 接口方法,你不需要在 xml 處理后兩個參數
public interface CountryMapper {
    List<Country> selectByPageNumSize(
            @Param("user") User user,
            @Param("pageNum") int pageNum, 
            @Param("pageSize") int pageSize);
}
//配置supportMethodsArguments=true
//在代碼中直接調用:
List<Country> list = countryMapper.selectByPageNumSize(user, 1, 10);

//第五種,參數對象
//如果 pageNum 和 pageSize 存在於 User 對象中,只要參數有值,也會被分頁
//有如下 User 對象
public class User {
    //其他fields
    //下面兩個參數名和 params 配置的名字一致
    private Integer pageNum;
    private Integer pageSize;
}
//存在以下 Mapper 接口方法,你不需要在 xml 處理后兩個參數
public interface CountryMapper {
    List<Country> selectByPageNumSize(User user);
}
//當 user 中的 pageNum!= null && pageSize!= null 時,會自動分頁
List<Country> list = countryMapper.selectByPageNumSize(user);

其中第二、三種方式比較常用,在你需要進行分頁的 MyBatis 查詢方法前調用 PageHelper.startPage 靜態方法即可,緊跟在這個方法后的第一個MyBatis 查詢方法會被進行分頁。

 

4.獲取分頁信息:

//獲取第1頁,10條內容,默認查詢總數count  
PageHelper.startPage(1, 10);  
List<Country> list = countryMapper.selectAll();  
//用PageInfo對結果進行包裝  
PageInfo page = new PageInfo(list);  
//PageInfo包含了非常全面的分頁屬性

 

 

 

更多實例請參考pageHelper文檔:

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/README_zh.md

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM