一、引入pom依赖
<properties>
<mybatis.version>3.2.8</mybatis.version>
<mybatis.paginator.version>1.2.15</mybatis.paginator.version>
<pagehelper.version>5.0.0</pagehelper.version>
</properties>
<dependencies>
<dependency>
<groupId>com.github.miemiedev</groupId>
<artifactId>mybatis-paginator</artifactId>
<version>${mybatis.paginator.version}</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
</dependencies>
二、在springmvc配置文件中加入插件
sqlMapConfig.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<!--配置开启自动匹配驼峰-->
<settings>
<setting name="mapUnderscoreToCamelCase" value="true"/>
</settings>
<!--配置PageHelper分页插件拦截器-->
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<!-- <property name="offsetAsPageNum" value="true"/>-->
<!-- <property name="helperDialect" value="mysql"/>-->
<property name="helperDialect" value="mysql"/>
<!-- <property name="rowBoundsWithCount" value="true"/>-->
<property name="reasonable" value="false"/>
</plugin>
</plugins>
</configuration>
三、在代码中使用
@Override
public EUDataGridResult getContentList(Long id, int page, int rows) {
TbContentExample example = new TbContentExample();
TbContentExample.Criteria criteria = example.createCriteria();
criteria.andCategoryIdEqualTo(id);
// 分页处理
PageHelper.startPage(page, rows);
// 查询分类列表
List<TbContent> lists = tbContentMapper.selectByExample(example);
// 创建返回值对象
EUDataGridResult result = new EUDataGridResult();
result.setRows(lists);
// 取记录总数
PageInfo pageInfo = new PageInfo(lists);
result.setTotal(pageInfo.getTotal());
result.setPages(pageInfo.getPages());
return result;
}