一、添加pom依赖
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>5.1.10</version>
</dependency>
二、使用方法
(service层去使用PageHelper类,controller和mapper层不变化)
@Service
public class EbookService {
private static final Logger LOG = LoggerFactory.getLogger(EbookService.class);
@Resource
private EbookMapper ebookMapper;
public List<EbookResp> findAll(EbookReq req){
EbookExample example = new EbookExample();
EbookExample.Criteria criteria = example.createCriteria();
if(!ObjectUtils.isEmpty(req.getName())){
criteria.andNameLike("%"+req.getName()+"%");
}
/**
* 开启分页
*/
PageHelper.startPage(1,3);
List<Ebook> list = ebookMapper.selectByExample(example);
PageInfo<Ebook> pageInfo = new PageInfo<>(list);
LOG.info("总行数:{}",pageInfo.getTotal());
LOG.info("总页数:{}",pageInfo.getPages());
//列表复制
List<EbookResp> lists = CopyUtil.copyList(list, EbookResp.class);
return lists;
}
}
注意点:
- mapper.xml中不需要添加limit,pageHelper帮我们解决了
- 只需要在service层中使用PageHelper.startPage()即可