SpringBoot整合分页助手
分页助手依赖:
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
导入后需要对助手进行配置,必须指定语言,其他可以使用默认值
配置类版:
@Configuration
public class PageHelperConfig {
@Bean
PageInterceptor pageInterceptor(){
PageInterceptor pageInterceptor = new PageInterceptor();
Properties properties = new Properties();
properties.setProperty("offsetAsPageNum","false");
properties.setProperty("rowBoundsWithCount","false");
properties.setProperty("pageSizeZero","true");
properties.setProperty("reasonable","false");
properties.setProperty("supportMethodsArguments","false");
properties.setProperty("returnPageInfo","none");
properties.setProperty("autoRuntimeDialect","true");
pageInterceptor.setProperties(properties);
return pageInterceptor;
}
}
XML版:
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="offsetAsPageNum" value="false" />
<property name="rowBoundsWithCount" value="false" />
<property name="pageSizeZero" value="true" />
<property name="reasonable" value="false" />
<property name="supportMethodsArguments" value="false" />
<property name="returnPageInfo" value="none" />
<!--<property name="dialect" value="mysql" />-->
<property name="autoRuntimeDialect" value="true" />
<!--<property name="autoDialect" value="true" />-->
</plugin>
</plugins>
完成后即可正常使用
助手会绑定在第一个执行sql的线程上对内容进行分页
使用:
PageHelper.startPage(1,3);
List<User> users = userInfoMapper.pageTest();
PageInfo<User> userPageInfo = new PageInfo<>(users);
List<User> list = userPageInfo.getList();
System.out.println(list);
sql:
@Select("SELECT * FROM ad_user")
List<User> pageTest();
PageInfo内容:
*PageInfo{pageNum=3, pageSize=3, size=1, startRow=7, endRow=7, total=7, pages=3, list=Page{count=true, pageNum=3, pageSize=3, startRow=6, endRow=9, total=7, pages=3, reasonable=false, pageSizeZero=true}, prePage=2, nextPage=0, isFirstPage=false, isLastPage=true, hasPreviousPage=true, hasNextPage=false, navigatePages=8, navigateFirstPage=1, navigateLastPage=3, navigatepageNums=[1, 2, 3]} *
pageNum:
需求页码
pageSize:
需求页长度
size:
实际内容长度
startRow:
起始行
endRow:
结束行
total:
总条数
pages:
总页数
list:
分页数据
手动分页公式;
page:1
size:5
limit (page-1)*size,size
、
==========
2022-5-19
mybatis-plus分页
配置类-分页拦截器
@Configuration
public class MybatisPlusConfig {
@Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() {
// 分页拦截器
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
return interceptor;
}
}
// 分页搜索课程
public Page<PzhAudioCourse> findPage(Integer pageNumber, Integer pageSize, String courseName, Long courseTypeId) {
Page<PzhAudioCourse> page = new Page<>(pageNumber, pageSize);
LambdaQueryWrapper<PzhAudioCourse> wrapper = new LambdaQueryWrapper<>();
if (StringUtils.isNotBlank(courseName))
wrapper.eq(PzhAudioCourse::getCourseSubject, courseName);
if (courseTypeId != null)
wrapper.eq(PzhAudioCourse::getCourseTypeId, courseTypeId);
wrapper.orderByDesc(PzhAudioCourse::getCreated);
return audioCourseMapper.selectPage(page, wrapper);
}