SpringBoot使用PageHelper

SpringBoot使用PageHelper

SpringBoot使用PageHelper

引依赖

关于SpringBoot使用PageHelper最重要的是有两个依赖,如果漏了pagehelper-spring-boot-autoconfigure会导致分页失效,而且不报错,很头疼

<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper</artifactId>
    <version>5.1.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper-spring-boot-autoconfigure -->
<dependency>
    <groupId>com.github.pagehelper</groupId>
    <artifactId>pagehelper-spring-boot-autoconfigure</artifactId>
    <version>1.2.12</version>
</dependency>

配置

使用配置类的方式配置PageHelper

@Configuration
public class PageHelperConfig {
	@Bean
	public PageHelper pageHelper(){
		PageHelper pageHelper = new PageHelper();
		Properties properties = new Properties();
		properties.setProperty("offsetAsPageNum","true");
		properties.setProperty("rowBoundsWithCount","true");
		properties.setProperty("reasonable","true");
		//配置mysql数据库的方言
		properties.setProperty("dialect","mysql");
		pageHelper.setProperties(properties);
		return pageHelper;
	}
}

使用

使用PageHelper.startPage 静态方法调用startPage :

例如Service层代码:

public PageInfo<ActualLayoutScheme> listByPage(Integer current, Integer pageSize) {
    // 分页,传递两个参数当前页和分页大小
    PageHelper.startPage(current, pageSize);

    ActualLayoutSchemeExample example = new ActualLayoutSchemeExample();
    ActualLayoutSchemeExample.Criteria criteria = example.createCriteria();
    criteria.andDeletedEqualTo(DeleteConstant.RETAIN);
    List<ActualLayoutScheme> actualLayoutSchemes = layoutSchemeMapper.selectByExample(example);

    // 使用PageInfo包装查询的list结果
    return new PageInfo<>(actualLayoutSchemes);
}
# Java 

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×