自动分页:
- 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);
}
- 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);
}
}
- 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();
}
}
}
- 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;
}
}
- 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)));
}
}