环境:windows10 idea maven ssm
详细应用参见官方应用文档
1、在pom文件添加jar包
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
2、这里只介绍在spring中的配置方式,在mybatis中的配置方式参见官方文档
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- 注意其他配置 -->
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<!--使用下面的方式配置参数,一行配置一个 -->
<value>
offsetAsPageNum=true <!-- offset参数当成pageNum使用 -->
rowBoundsWithCount=true <!-- 进行count查询 -->
pageSizeZero=true <!-- pageSize=0查询全部 -->
reasonable=true <!-- 分页合理化参数 -->
autoRuntimeDialect=true <!-- 多数据源自动识别对应方言 -->
<!-- 其它参数默认就好 -->
</value>
</property>
</bean>
</array>
</property>
</bean>
3、在程序中使用
//获取第1页,10条内容,默认查询总数count
PageHelper.startPage(1, 10);
//紧跟着的第一个select方法会被分页
List<Country> list = countryMapper.selectIf(1);
assertEquals(2, list.get(0).getId());
assertEquals(10, list.size());
//分页时,实际返回的结果list类型是Page<E>,如果想取出分页信息,需要强制转换为Page<E>
assertEquals(182, ((Page) list).getTotal());
其他的一些用法去官方文档看吧。