1. 程式人生 > >pageHelper的spring配置及使用

pageHelper的spring配置及使用

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);
}