Pageable,PageImpl实现分页数据

    /**
     *Pageable,PageImpl引用包位置
     */

import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;

    /**
     *需要的参数:page_number,page_size
     */

    @RequestMapping(value = "page", method = RequestMethod.GET)
    @ResponseBody
    public PageResult> page(HttpServletRequest request) {
        Map params = WebUtils.getParametersStartingWith(
                request, "filter__");
        Pageable pageable = WebQueryUtils.buildPage(request);
        Page> result = service.page(params, pageable);
        return PageResult.getPageResult(result);
    }

    /**
     *WebQueryUtils是自己的一个工具类,使用其中的方法即可
     */

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;

public class WebQueryUtils {
	public static Pageable buildPage(HttpServletRequest request) {
		String pageNumber = request.getParameter("page_number");
		String pageSize = request.getParameter("page_size");
		int page = StringUtils.isBlank(pageNumber) ? 0 : Integer.parseInt(pageNumber)-1;
		int size = StringUtils.isBlank(pageSize) ? 10 : Integer.parseInt(pageSize);
		Pageable pageable = new PageRequest(page, size);
		return pageable;
	}
	public static Pageable buildPage(HttpServletRequest request,Sort sort) {
		String pageNumber = request.getParameter("page_number");
		String pageSize = request.getParameter("page_size");
		int page = StringUtils.isBlank(pageNumber) ? 0 : Integer.parseInt(pageNumber)-1;
		int size = StringUtils.isBlank(pageSize) ? 10 : Integer.parseInt(pageSize);
		Pageable pageable = new PageRequest(page, size ,sort);
		return pageable;
	}
}

    /**
     *service.page(params, pageable)方法中调用    

     */

 

public Page page(Map filter, Pageable page) {
        return PatientInfoJdbcDao.getPage(page, filter);
}

    /**
     *此处使用的是jdbc,通过查询vm文件的sql语句得到想要拿的分页数据,自己可以根据需要修改怎样查询得到想要的分页数据

     *最后通过得到的分页数据使用new PageImpl<>()即可得到页面想要的分页数据.

     */

@Component
public class PatientInfoJdbcDao {

	/**
	 * 分页查询
	 * 
	 * @param filter
	 * @return
	 */
	public Page> getPage(Pageable pageable,
			Map filter) {
		filter.put("start",pageable.getOffset());//page_number*page_size
		filter.put("end",pageable.getPageSize());
		String fileName = "tplsql/mysql/admission/patient.info.list.vm";
		String sizeFileName = "tplsql/mysql/admission/patient.info.list.size.vm";// 查询有多少数据的sql模板的文件名

		List> sizeList = DaoUtils.execute(sizeFileName,filter);
		List> dataList = DaoUtils.execute(fileName, filter);
		List> result = getResultList(dataList);
		int totalCount = 0;
		if (sizeList != null && sizeList.size() > 0) {
			totalCount = Integer.valueOf(sizeList.get(0).get("count")
					.toString());
		}
		Page page = new PageImpl>(result, pageable,
				totalCount);
		return page;
	}

 

 

 

你可能感兴趣的:(Pageable,PageImpl实现分页数据)