Spring boot Mybatis-XML方式分頁查詢PageHelper(五)

PageHelper介紹

  • PageHelper是Github上有位開發(fā)者寫了一個分頁插件,可以很方便的添加到MyBatis的攔截器接口中。
    Github項(xiàng)目地址

pom.xml添加依賴

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

配置文件編寫

@Configuration
public class MyBatisConfiguration {
    @Bean
    public PageHelper pageHelper(){
        PageHelper pageHelper=new PageHelper();
        Properties p=new Properties();
        // 當(dāng)該參數(shù)設(shè)置為 true 時,會將 RowBounds 中的 offset 參數(shù)當(dāng)成 pageNum 使用,可以用頁碼和頁面大小兩個參數(shù)進(jìn)行分頁。
        p.setProperty("offsetAsPageNum","true");
        // 當(dāng)該參數(shù)設(shè)置為true時,使用 RowBounds 分頁會進(jìn)行 count 查詢。
        p.setProperty("rowBoundsWithCount","true");
        //當(dāng)該參數(shù)設(shè)置為 true 時,pageNum<=0 時會查詢第一頁, pageNum>pages(超過總數(shù)時),會查詢最后一頁。
        p.setProperty("reasonable","true");
        pageHelper.setProperties(p);
        return pageHelper;
    }
}

測試類

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;
    @RequestMapping("/queryStudentList")
    public List<Student> queryStudentList(int pageNum,int pageSize){
        //pageNum頁碼,pageSize條數(shù)
        PageHelper.startPage(pageNum,pageSize);
        return studentService.queryList();
    }
}

測試

  • 所有數(shù)據(jù)
所有數(shù)據(jù).png
  • 分頁數(shù)據(jù)
分頁數(shù)據(jù).png

使用pagehelper-spring-boot-starter代替PageHelper

PageHelper-Spring-Boot-Starter 幫助你集成分頁插件到 Spring Boot比pagehelper更簡單

替換依賴

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

#替換為
<dependency>
      <groupId>com.github.pagehelper</groupId>
      <artifactId>pagehelper-spring-boot-starter</artifactId>
     <version>1.2.1</version>
</dependency>

yml文件配置

spring:
  ########################################################
  ###Druid -- mysql的數(shù)據(jù)庫配置.
  ########################################################
  datasource:
    druid:
      url: jdbc:mysql://localhost:3306/test?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true
      username: root
      password: root
      driver-class-name: com.mysql.jdbc.Driver
      #下面為連接池補(bǔ)充設(shè)置應(yīng)用到上面所有數(shù)據(jù)源中
      #初始化大小,最小,最大
      initialSize: 5
      minIdle: 5
      maxActive: 20
      #配置獲取連接等待超時的時間
      maxWait: 60000
      #配置間隔多久才進(jìn)行一次檢測,檢測需要關(guān)閉的空閑連接,單位是毫秒
      timeBetweenEvictionRunsMillis: 60000
      # 配置一個連接在池中最小生存的時間,單位是毫秒
      minEvictableIdleTimeMillis: 300000
      validationQuery: SELECT 1
      testWhileIdle: true
      testOnBorrow: false
      testOnReturn: false
      # 打開PSCache,并且指定每個連接上PSCache的大小
      poolPreparedStatements: true
      maxPoolPreparedStatementPerConnectionSize: 20
      # 配置監(jiān)控統(tǒng)計攔截的filters,去掉后監(jiān)控界面sql無法統(tǒng)計,'wall'用于防火墻
      filters: stat,wall,log4j
      # 通過connectProperties屬性來打開mergeSql功能;慢SQL記錄
      connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000
      # 合并多個DruidDataSource的監(jiān)控數(shù)據(jù)
      useGlobalDataSourceStat: true
      web-stat-filter:
        url-pattern: /*
        exclusions: /druid/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico
      stat-view-servlet:
        url-pattern: /druid/*
        #登陸賬號密碼
        login-username: guoadmin
        login-password: guoadmin
        #是否可以重置
        reset-enable: false
        #白名單
        #allow: 127.0.0.1
        #黑名單
        #deny: 127.0.0.1




########################################################
###mybatis配置.
########################################################

mybatis:
    mapperLocations: classpath:mybatis/*/*.xml
    # 給實(shí)體類起別名
    type-aliases-package: com.springboot.backstage.entity

########################################################
###分頁插件
########################################################
pagehelper:
  helper-dialect: mysql
  #當(dāng)該參數(shù)設(shè)置為 true 時,pageNum<=0 時會查詢第一頁, pageNum>pages(超過總數(shù)時),會查詢最后一頁。
  reasonable: true
  #支持通過 Mapper 接口參數(shù)來傳遞分頁參數(shù),默認(rèn)值false,分頁插件會從查詢方法的參數(shù)值中,自動根據(jù)上面 params 配置的字段中取值,查找到合適的值時就會自動分頁
  support-methods-arguments: true
  #為了支持startPage(Object params)方法,增加了該參數(shù)來配置參數(shù)映射,用于從對象中根據(jù)屬性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable
  params: count=countSql

測試

可以重復(fù)上面的測試方法看是否有效

PageHelper的參數(shù)詳解和PageInfo的使用

參數(shù)說明

  • helperDialect:分頁插件會自動檢測當(dāng)前的數(shù)據(jù)庫鏈接,自動選擇合適的分頁方式。 你可以配置helperDialect屬性來指定分頁插件使用哪種方言。配置時,可以使用下面的縮寫值:oracle,mysql,mariadb,sqlite,hsqldb,postgresql,db2,sqlserver,informix,h2,sqlserver2012,derby
    特別注意:使用 SqlServer2012 數(shù)據(jù)庫時,需要手動指定為 sqlserver2012,否則會使用 SqlServer2005 的方式進(jìn)行分頁。
    你也可以實(shí)現(xiàn) AbstractHelperDialect,然后配置該屬性為實(shí)現(xiàn)類的全限定名稱即可使用自定義的實(shí)現(xiàn)方法。
  • offsetAsPageNum:默認(rèn)值為 false,該參數(shù)對使用 RowBounds 作為分頁參數(shù)時有效。 當(dāng)該參數(shù)設(shè)置為 true 時,會將 RowBounds 中的 offset 參數(shù)當(dāng)成 pageNum 使用,可以用頁碼和頁面大小兩個參數(shù)進(jìn)行分頁。
  • rowBoundsWithCount:默認(rèn)值為false,該參數(shù)對使用 RowBounds 作為分頁參數(shù)時有效。 當(dāng)該參數(shù)設(shè)置為true時,使用 RowBounds 分頁會進(jìn)行 count 查詢。
  • pageSizeZero:默認(rèn)值為 false,當(dāng)該參數(shù)設(shè)置為 true 時,如果 pageSize=0 或者 RowBounds.limit = 0 就會查詢出全部的結(jié)果(相當(dāng)于沒有執(zhí)行分頁查詢,但是返回結(jié)果仍然是 Page 類型)。
  • reasonable:分頁合理化參數(shù),默認(rèn)值為false。當(dāng)該參數(shù)設(shè)置為 true 時,pageNum<=0 時會查詢第一頁, pageNum>pages(超過總數(shù)時),會查詢最后一頁。默認(rèn)false 時,直接根據(jù)參數(shù)進(jìn)行查詢。
  • params:為了支持startPage(Object params)方法,增加了該參數(shù)來配置參數(shù)映射,用于從對象中根據(jù)屬性名取值, 可以配置 pageNum,pageSize,count,pageSizeZero,reasonable,不配置映射的用默認(rèn)值, 默認(rèn)值為pageNum=pageNum;pageSize=pageSize;count=countSql;reasonable=reasonable;pageSizeZero=pageSizeZero。
  • supportMethodsArguments:支持通過 Mapper 接口參數(shù)來傳遞分頁參數(shù),默認(rèn)值false,分頁插件會從查詢方法的參數(shù)值中,自動根據(jù)上面 params 配置的字段中取值,查找到合適的值時就會自動分頁。 使用方法可以參考測試代碼中的 com.github.pagehelper.test.basic 包下的 ArgumentsMapTest 和 ArgumentsObjTest。
  • autoRuntimeDialect:默認(rèn)值為 false。設(shè)置為 true 時,允許在運(yùn)行時根據(jù)多數(shù)據(jù)源自動識別對應(yīng)方言的分頁 (不支持自動選擇sqlserver2012,只能使用sqlserver),用法和注意事項(xiàng)參考下面的場景五。
    closeConn:默認(rèn)值為 true。當(dāng)使用運(yùn)行時動態(tài)數(shù)據(jù)源或沒有設(shè)置 helperDialect 屬性自動獲取數(shù)據(jù)庫類型時,會自動獲取一個數(shù)據(jù)庫連接, 通過該屬性來設(shè)置是否關(guān)閉獲取的這個連接,默認(rèn)true關(guān)閉,設(shè)置為 false 后,不會關(guān)閉獲取的連接,這個參數(shù)的設(shè)置要根據(jù)自己選擇的數(shù)據(jù)源來決定。

pageInfo的使用

在返回分頁的list后面創(chuàng)建一個pageInfo我們來看一下PageInfo返回了哪些參數(shù)

@RestController
public class StudentController {
    @Autowired
    private StudentService studentService;

    @RequestMapping("/queryStudentList")
    public PageInfo<Student> queryStudentList(int pageNum, int pageSize){
        //pageNum頁碼,pageSize條數(shù)
        PageHelper.startPage(pageNum,pageSize);
        List<Student>list=studentService.queryList();
        PageInfo<Student> pageInfo=new PageInfo<Student>(list);
        return pageInfo;
    }
}

查看返回參數(shù)

image.png
  • endRow:當(dāng)前頁面最后一個元素在數(shù)據(jù)庫中的行號
  • firstPage:第一頁
  • hasNextPage:是否有下一頁
  • hasPreviousPage:是否有前一頁
  • isFirstPage:是否為第一頁
  • isLastPage:是否為最后一頁
  • lastPage:最后一頁
  • list:結(jié)果集
  • navigatepageNums:所有導(dǎo)航號
  • nextPage:下一頁
  • pageNum:當(dāng)前頁
  • pageSize:每頁數(shù)量
  • pages:總頁數(shù)
  • prePage:前一頁
  • size:當(dāng)前頁數(shù)量
  • startRow:當(dāng)前頁面第一個元素在數(shù)據(jù)庫中的行號
  • total:數(shù)據(jù)總數(shù)量
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。