天天看点

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语句获取总共条数,分页插件会自动获取.