流程
1,maven 依赖
2,在 mybatis 配置文件启用插件
3,修改 service 层
依赖
<!-- https://mvnrepository.com/artifact/com.github.pagehelper/pagehelper -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.2</version>
</dependency>
启用插件
<plugins>
<plugin interceptor="com.github.pagehelper.PageInterceptor">
<property name="helperDialect" value="mysql" />
</plugin>
</plugins>
service 层代码示例
@Service("accountService")
public class AccountServiceImpl implements IAccountService {
@Autowired
private AccountMapper am;
@Override
public PageInfo<Account> selectBySearch(Account record, String startDate, String endDate, String companyName,
Integer currentPage, Integer pageSize) {
// 如果没有传递分页属性值,默认第一页,每页10条数据
if(currentPage == null) {
currentPage = 1;
}
if(pageSize == null) {
pageSize = 10;
}
// 这是关键,设置分页属性
PageHelper.startPage(currentPage, pageSize);
// 和 mapper 接口文件中的方法一致
List<Account> list = am.selectBySearch(record, startDate, endDate, companyName);
// 分页成功,返回
PageInfo<Account> pageInfo = new PageInfo<>(list);
return pageInfo;
}
}