一、pom.xml
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>4.0.0</version>
</dependency>
二、Mybatis配置文件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>
<plugins>
<!-- com.github.pagehelper 为 PageHelper 类所在包名 -->
<plugin interceptor="com.github.pagehelper.PageHelper">
<!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL 六种数据库-->
<property name="dialect" value="mysql"/>
</plugin>
</plugins>
</configuration>
三、PageHelper使用service层
@Override
public PageResult findPage(TbBrand brand, int pageNum, int pageSize) {
PageHelper.startPage(pageNum,pageSize); // 通过Mybatis的PageHelper实现快速分页
TbBrandExample example = new TbBrandExample();
TbBrandExample.Criteria criteria = example.createCriteria();
if (brand != null){
if (brand.getName()!=null &&brand.getName().length() > 0){
criteria.andNameLike("%" + brand.getName() + "%");
}
if (brand.getFirstChar()!=null &&brand.getFirstChar().length() > 0){
criteria.andFirstCharLike("%" + brand.getFirstChar() + "%");
}
}
Page<TbBrand> page = (Page<TbBrand>) brandMapper.selectByExample(example);
return new PageResult(page.getTotal(),page.getResult());
}
参考资料:
https://www.cnblogs.com/kangoroo/p/7998433.html
http://blog.csdn.net/qq_33624284/article/details/72828977
http://blog.csdn.net/qq_26790807/article/details/62429290