BlueXIII's Blog

热爱技术,持续学习

0%

研发工具-MybatisPageHelper

官网

pom.xml整合

1
2
3
4
5
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>

注意需要更新至最新版,以支持SQL排序

配置拦截器插件

公司框架中MyBatisConfigure.class已配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
@Bean
public PageInterceptor pageInterceptor() {
PageInterceptor pageInterceptor = new PageInterceptor();
Properties p = new Properties();
p.setProperty("offsetAsPageNum", "true");
p.setProperty("rowBoundsWithCount", "true");
p.setProperty("reasonable", "false");
p.setProperty("pageSizeZero", "true");
pageInterceptor.setProperties(p);
return pageInterceptor;
}

@Bean(
name = {"sqlSessionFactory"}
)
public SqlSessionFactoryBean sqlSessionFactory(@Qualifier("dataSourceRouter") DataSource dataSourceRouter) {
... ...
sqlSessionFactory.setPlugins(new Interceptor[]{this.pageInterceptor()});
... ...
return sqlSessionFactory;
}

基本使用示例

推荐使用PageHelper.startPage方式:

1
2
3
4
5
6
public PageInfo<ThreedutyCheckRecord> findByUnitCode(String unitCode, int pageNum, int pageSize, String orderBy) {
PageHelper.<ThreedutyCheckRecord>startPage(pageNum, pageSize, orderBy);
List<ThreedutyCheckRecord> result = threedutyCheckRecordMapper.findByUnitCode(unitCode);
PageInfo<ThreedutyCheckRecord> page = new PageInfo<>(result);
return page;
}

使用方式详解

https://github.com/pagehelper/Mybatis-PageHelper/blob/master/wikis/zh/HowToUse.md