pageHelper使用的好处是sql里不需要再使用limit
maven项目的pom.xml中加入依赖:
controller:
@RequestMapping("/resourcesList.do")
public Map<String,Object> resourcesList(Resources resources,String draw,
@RequestParam(required = false, defaultValue = “1”) int start,
@RequestParam(required = false, defaultValue = “10”) int length){
Map<String,Object> map = new HashMap<String,Object>();
PageInfo pageInfo = resourcesService.selectByPage(resources, start, length);
map.put(“draw”,draw);
map.put(“recordsTotal”,pageInfo.getTotal());
map.put(“recordsFiltered”,pageInfo.getTotal());
map.put(“data”, pageInfo.getList());
return map;
}
serviceimpl:
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
@Override
public PageInfo<Resources> selectByPage(Resources resources, int start, int length) {
int page = start/length+1;
PageHelper.startPage(page, length);
List<Resources> resourcelist = resourcesDao.queryAll(resources);
return new PageInfo<>(resourcelist);
}