1. 程式人生 > 其它 >Spring Boot 中PageHelper 外掛使用配置

Spring Boot 中PageHelper 外掛使用配置

  • 使用思路:
    1.引入myabtis和pagehelper依賴

    2.yml中配置mybatis掃描和實體類

    1. 這2行程式碼
      pageNum:當前第幾頁
      pageSize:顯示多少條資料
      userList:資料庫查詢的資料資料列表

    PageHelper.startPage(pageNum, pageSize);
    PageInfo pageInfo = new PageInfo(userList);
    最後返回一個pageInfo 物件即可,pageInfo 這個物件中只有資料一些資訊,但是,沒有成功失敗的狀態或者提示語。
    真實企業中會封裝一個返回物件,把pageInfo 放到物件中

1.pom依賴

方法一:使用原生的PageHelper

1.在pom.xml中引入依賴,重新整理自動載入jar
`

      <dependency>

        <groupId>com.github.pagehelper</groupId>

        <artifactId>pagehelper</artifactId>

        <version>5.2.1</version>

    </dependency>

`

方法二 使用 PageHelper的starter

1.匯入pom.xml依賴

`

     <dependency>

        <groupId>com.github.pagehelper</groupId>

        <artifactId>pagehelper-spring-boot-starter</artifactId>

        <version>1.2.12</version>

    </dependency>

`

2.在application.properties或者application.yml格式配置pagehelper的屬性

二選一

`
#pagehelper分頁外掛配置

        pagehelper.helper-dialect=mysql

        pagehelper.reasonable=true

        pagehelper.support-methods-arguments=true

        pagehelper.params=count=countSql

`

`

   hepagehelper:

          lperDialect: mysql

          reasonable: true

          supportMethodsArguments: true

          params: count=countSql

`

Controller層呼叫 測試

`

    @RequestMapping("findallCar")

public String findallCar(Model model, HttpSession session) {

    PageHelper.startPage(1,5);

    List<CarTable> carTables = service.findallCar();

    PageInfo<CarTable> page = new PageInfo<CarTable>(carTables);

    System.out.println(page);

    model.addAttribute("carall", carTables);

    session.setAttribute("caralls", carTables);

    return "carinsert";
}

`

     PageHelper.startPage(1,5);

     List<CarTable> carTables = service.findallCar();

    PageInfo<CarTable> page = new PageInfo<CarTable>(carTables);

    System.out.println(page);