天天看點

mysql分頁插件springboot_SpringBoot內建MyBatis的分頁插件PageHelper--詳細步驟

1.pom中添加依賴包

com.github.pagehelper

pagehelper

5.1.2

com.github.pagehelper

pagehelper-spring-boot-autoconfigure

1.2.5

com.github.pagehelper

pagehelper-spring-boot-starter

1.2.5

2.配置分頁插件

下面二者選一配置

2.1.application.properties配置

在application.properties檔案中添加如下配置

#分頁插件

pagehelper.helper-dialect=MYSQL

pagehelper.reasonable=true

pagehelper.support-methods-arguments=true

pagehelper.params=count=countSql

2.2.配檔案配置對象

package com.qiaXXXXXX.config;

import com.github.pagehelper.PageHelper;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

import java.util.Properties;

@Configuration

public class PageHelperConfig {

@Bean

public PageHelper getPageHelper() {

PageHelper pageHelper = new PageHelper();

Properties properties = new Properties();

properties.setProperty("helperDialect", "mysql");

properties.setProperty("reasonable", "true");

properties.setProperty("supportMethodsArguments", "true");

properties.setProperty("params", "count=countSql");

pageHelper.setProperties(properties);

return pageHelper;

}

}

3.分頁實作

@Override

public PageInfo getMyOrder(MyOrderObj obj) {

if (StringUtils.isEmpty(obj.getQueryMonth())) {

String endMonth = DateUtil.getEndMonth();

obj.setStartMonth(DateUtil.getStartMonth(endMonth));

obj.setEndMonth(endMonth);

} else {

obj.setStartMonth(null);

obj.setEndMonth(null);

}

//設定分頁參數

PageHelper.startPage(obj.getPageNo(), obj.getPageSize());

//查詢清單資料

List list = userCenterMapper.getMyOrder(obj);

//擷取分頁對象

PageInfo pageInfo = new PageInfo<>(list);

return pageInfo;

}

注意:有了分頁插件,sql語句不需要寫limit,插件會在執行的sql中自動添加,也不需要自己單獨寫count語句擷取總共條數,分頁插件會自動擷取.