- 在pom.xml中添加分页插件库依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.11</version>
</dependency>
- 在mybatis-config.xml 文件中添加插件配置
<plugins>
<!-- com.github.pagehelper为PageHelper类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- 使用下面的方式配置参数,后面会有所有的参数介绍 -->
<property name="helperDialect" value="mysql"/>
<property name="supportMethodsArguments" value="true"/>
<property name="rowBoundsWithCount" value="true"/>
<property name="offsetAsPageNum" value="true"/>
<property name="pageSizeZero" value="false"/>
<property name="reasonable" value="true"/>
</plugin>
</plugins>
3. 在映射器接口的分页方法加上多参数注解,pageNum 与 pageSize 这两个参数是提供给PageIntercepto 插件类使用的,在xml映射文件中无需使用,PageInterceptor插件会自动分析数据库的类型 helperDialect=mysql 而使用相应的分页查询方式, 如mysql数据库会在sql 语句上自动加上 LIMIT ?, ? 限制条件,这里的两个占位符就是pageNum 与 pageSize这两个参数值。
XML映射文件中的查询语句正常编写,多表连接的查询语句也正常编写即可。分页插件会在执行时根据数据库语句的分页方式自动加上分页的条件,如mysql 会在语句后加上LIMIT ?, ?;oracle 数据会自动生成三层嵌套的语句。
<select id="selectByPage" resultType="com.sdl.model.Food">
select * from foodinfo
</select>
- 测试
第一页,页大小为4