背景
startPage基于PageHelper来进行强化,在用户传入pagesize,pageNum等标准参数的时候不需要进行解析
步骤
1.通过ServletUtils工具类getRequestAttributes来获取当前线程的上下文信息
public static ServletRequestAttributes getRequestAttributes() {
try {
RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
return (ServletRequestAttributes)attributes;
} catch (Exception var1) {
return null;
}
}
- 通过getRequest方法来获取本次的HttpServletRequest请求
public static HttpServletRequest getRequest() {
try {
return getRequestAttributes().getRequest();
} catch (Exception var1) {
return null;
}
}
- 通过getParameter方法来获取本次request的请求参数
public static String getParameter(String name) {
return getRequest().getParameter(name);
}
- 在getPageDomain方法中,通过ServletUtils.getParameter方法获取到具体参数,若没有就使用默认值
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.lml.common.core.web.page;
import com.lml.common.core.text.Convert;
import com.lml.common.core.utils.ServletUtils;
public class TableSupport {
public static final String PAGE_NUM = "pageNum";
public static final String PAGE_SIZE = "pageSize";
public static final String ORDER_BY_COLUMN = "orderByColumn";
public static final String IS_ASC = "isAsc";
public static final String REASONABLE = "reasonable";
public TableSupport() {
}
public static PageDomain getPageDomain() {
PageDomain pageDomain = new PageDomain();
pageDomain.setPageNum(Convert.toInt(ServletUtils.getParameter("pageNum"), 1));
pageDomain.setPageSize(Convert.toInt(ServletUtils.getParameter("pageSize"), 10));
pageDomain.setOrderByColumn(ServletUtils.getParameter("orderByColumn"));
pageDomain.setIsAsc(ServletUtils.getParameter("isAsc"));
pageDomain.setReasonable(ServletUtils.getParameterToBool("reasonable"));
return pageDomain;
}
public static PageDomain buildPageRequest() {
return getPageDomain();
}
}
- 通过调用PageUtils.startPage方法来代替重复的PageHelper.startPage赋值
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.lml.common.core.utils;
import com.github.pagehelper.PageHelper;
import com.lml.common.core.utils.sql.SqlUtil;
import com.lml.common.core.web.page.PageDomain;
import com.lml.common.core.web.page.TableSupport;
public class PageUtils extends PageHelper {
public PageUtils() {
}
public static void startPage() {
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize();
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
Boolean reasonable = pageDomain.getReasonable();
PageHelper.startPage(pageNum, pageSize, orderBy).setReasonable(reasonable);
}
public static void clearPage() {
PageHelper.clearPage();
}
}
- 控制台层通过继承BaseController,然后调用startPage()方法来进行翻页
//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//
package com.lml.common.core.web.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lml.common.core.utils.DateUtils;
import com.lml.common.core.utils.PageUtils;
import com.lml.common.core.utils.StringUtils;
import com.lml.common.core.utils.sql.SqlUtil;
import com.lml.common.core.web.domain.AjaxResult;
import com.lml.common.core.web.page.PageDomain;
import com.lml.common.core.web.page.TableDataInfo;
import com.lml.common.core.web.page.TableSupport;
import java.beans.PropertyEditorSupport;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
public class BaseController {
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
public BaseController() {
}
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
public void setAsText(String text) {
this.setValue(DateUtils.parseDate(text));
}
});
}
protected void startPage() {
PageUtils.startPage();
}
protected void startOrderBy() {
PageDomain pageDomain = TableSupport.buildPageRequest();
if (StringUtils.isNotEmpty(pageDomain.getOrderBy())) {
String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
PageHelper.orderBy(orderBy);
}
}
protected void clearPage() {
PageUtils.clearPage();
}
protected TableDataInfo getDataTable(List<?> list) {
TableDataInfo rspData = new TableDataInfo();
rspData.setCode(200);
rspData.setRows(list);
rspData.setMsg("查询成功");
rspData.setTotal((new PageInfo(list)).getTotal());
return rspData;
}
public AjaxResult success() {
return AjaxResult.success();
}
public AjaxResult success(String message) {
return AjaxResult.success(message);
}
public AjaxResult success(Object data) {
return AjaxResult.success(data);
}
public AjaxResult error() {
return AjaxResult.error();
}
public AjaxResult error(String message) {
return AjaxResult.error(message);
}
public AjaxResult warn(String message) {
return AjaxResult.warn(message);
}
protected AjaxResult toAjax(int rows) {
return rows > 0 ? AjaxResult.success() : AjaxResult.error();
}
protected AjaxResult toAjax(boolean result) {
return result ? this.success() : this.error();
}
}