乐趣区

关于java:PageHelper插件

增加依赖

<dependency>
 <groupId>com.github.pagehelper</groupId>
 <artifactId>pagehelper-spring-boot-starter</artifactId>
 <version>1.3.0</version>
</dependency>

间接写在控制器层:

1.PageHelper.startPage(1,3)
                .doSelectPageInfo(new ISelect() {
                    @Override
                    public void doSelect() {sysNoticeService.findNotices(notice);
                    }
2. return new JsonResult(PageHelper.startPage(1, 3)
                  .doSelectPageInfo(()->//lambda
                     sysNoticeService.findNotices(notice)));*/
                  

定义工具类:

 获取申请对象
         ServletRequestAttributes sra=
         (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
         HttpServletRequest request=sra.getRequest();
 获取申请中的分页参数对象
           String pageCurrentStr=request.getParameter("pageCurrent");
           String pageSizeStr=request.getParameter("pageSize");
           int pageCurrent=pageCurrentStr?1:Integer.parseInt(pageCurrentStr);
           int pageSize==pageSizeStr?3:Integer.parseInt(pageSizeStr);
 启动分页查问
           return PageHelper.startPage(pageCurrent, pageSize);

代码不反复编写,创立多个类:

 创立 PageUtile 类:package com.cy.pj.common.util;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import javax.servlet.http.HttpServletRequest;

/**
 * 定义分页操作工具类,在此类办法外部启动分页查问
 */
public class PageUtil{public static <T>Page<T> startPage(){
           // 获取申请对象
           HttpServletRequest request=ServletUtil.getRequest();
           // 获取申请中的分页参数对象
           String pageCurrentStr=request.getParameter("pageCurrent");
           String pageSizeStr=request.getParameter("pageSize");
           int pageCurrent=StringUtil.isEmpty(pageCurrentStr)?1:Integer.parseInt(pageCurrentStr);
           int pageSize=StringUtil.isEmpty(pageSizeStr)?3:Integer.parseInt(pageSizeStr);
           // 启动分页查问
           return PageHelper.startPage(pageCurrent, pageSize);
       }
}

创立 ServletUtil 类:package com.cy.pj.common.util;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ServletUtil {

    /**
     * 获取 HttpServletRequest 申请对象
     * @return
     */
    public static HttpServletRequest getRequest(){return getServletRequestAttributes().getRequest();}
    public static HttpServletResponse getResponse(){return getServletRequestAttributes().getResponse();}

    /**
     * 通过 spring 提供的 RequestContextHolder 对象获取申请属性对象
     * @return
     */
    public static ServletRequestAttributes getServletRequestAttributes(){
       // String className=
        // RequestContextHolder.getRequestAttributes().getClass().getName();
        //System.out.println("className="+className);
        return (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();}

}
创立 StringUtil 类:package com.cy.pj.common.util;

public class StringUtil {
    /**
     * 断定字符串是否为空 (null,"")
     * @param content
     * @return
     */
    public static boolean isEmpty(String content){return content==null||"".equals(content);
    }
    //....
}
退出移动版