分页

自动分页:

  1. service层,controller正常写
   List<Plan> getPlan(Plan plan);

 @Override
    public List<Plan> getPlan(Plan plan) {
        PlanExample example = new PlanExample();
        PlanExample.Criteria criteria = example.createCriteria();
        if(BlankUtil.IsNotBlank(plan.getSender())){
            criteria.andSenderEqualTo(plan.getSender());
        }
        if(BlankUtil.IsNotBlank(plan.getReceiver())){
            criteria.andReceiverEqualTo(plan.getReceiver());
        }

        Page page = PageHandle.getPage();
        if(page!=null){
            PageHelper.startPage(page.getPageNum(),page.getPageSize());
            List<Plan> plans = planMapper.selectByExample(example);
            page.setCount(planMapper.selectByExample(example).size());
            return  plans;
        }
        return planMapper.selectByExample(example);
    }

  1. Page
package com.test.util;

public class Page {
    private int pageSize;
    private int pageNum;
    private long pageCount;
    private long count;

    public Page(Integer pageNum, Integer pageSize) {
        this.pageSize = pageSize;
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public long getPageCount() {
        return pageCount;
    }

    public void setPageCount(long pageCount) {
        this.pageCount = pageCount;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
        this.pageCount = count % pageSize > 0 ? (count / pageSize + 1) : (count / pageSize);
    }
}
  1. PageHandle
package com.test.util;

public class PageHandle {
    private final static ThreadLocal<Page> PAGE_LOCAL = new ThreadLocal<Page>();

    public static void page(Page page) {
        PAGE_LOCAL.set(page);
    }

    public static Page getPage() {
        return PAGE_LOCAL.get();
    }

    public static Page getPageAndRem() {
        try {
            return PAGE_LOCAL.get();
        } finally {
            PAGE_LOCAL.remove();
        }
    }
}

  1. WebConfig
package com.test.util;


import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@Configuration
@EnableWebMvc
public class WebConfig extends WebMvcConfigurerAdapter {

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new Intecepter());
    }


}

5.工具类 Result

package com.test.util;

public class Result {
    private String code;
    private Object data;
    private String msg;

    private final static transient String SUCCESS_CODE = "200";
    private final static transient String ERRO_CODE = "400";

    private int pageSize;
    private int pageNum;
    private long pageCount;
    private long count;

    public long getCount() {
        return count;
    }

    public Result setCount(long count) {
        this.count = count;
        return this;
    }

    public int getPageSize() {
        return pageSize;
    }

    public Result setPageSize(int pageSize) {
        this.pageSize = pageSize;
        return this;
    }

    public int getPageNum() {
        return pageNum;
    }

    public Result setPageNum(int pageNum) {
        this.pageNum = pageNum;
        return this;
    }

    public long getPageCount() {
        return pageCount;
    }

    public Result setPageCount(long pageCount) {
        this.pageCount = pageCount;
        return this;
    }

    private Result() {
    }

    public static Result success() {
        return new Result().setCode(SUCCESS_CODE);
    }

    public static Result success(Object obj) {
        Page page = PageHandle.getPageAndRem();
        Result result=new Result().setCode(SUCCESS_CODE).setData(obj);
        if(page==null){
            return result;
        }
        return result.setPageNum(page.getPageNum()).setPageSize(page.getPageSize()).setCount(page.getCount()).setPageCount(page.getPageCount());
    }

    public static Result erro(String msg) {
        return new Result().setCode(ERRO_CODE).setData(msg);
    }

    public String getCode() {
        return code;
    }

    public Result setCode(String code) {
        this.code = code;
        return this;
    }


    public Object getData() {
        return data;
    }

    public Result setData(Object data) {
        this.data = data;
        return this;
    }

    public String getMsg() {
        return msg;
    }

    public Result setMsg(String msg) {
        this.msg = msg;
        return this;
    }
}

  1. Intecepter
package com.test.util;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

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

public class Intecepter  implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        page(request);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
    private void page(HttpServletRequest request) {

        String pageNum = request.getParameter("pageNum");
        String pageSize = request.getParameter("pageSize");
        if(BlankUtil.IsBlank(pageNum) || BlankUtil.IsBlank(pageSize)){
            return ;
        }
        PageHandle.page(new Page(Integer.valueOf(pageNum),Integer.valueOf(pageSize)));
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值