PageHelper之排序

PageHelper是MyBatis的分页插件。关于MyBatis的分页插件如何使用和注意事项,可以参考我的这篇博客:

MyBatis分页插件失效问题之解决

今天说到的是利用Pagehelper排序,非常简单。

核心代码如下:

1
PageHelper.startPage(reqDTO.getCurPage(), reqDTO.getPageSize(), columAutoOrder(reqDTO.getOrderColumn(), reqDTO.getSort()));

其中最关键的核心方法,columAutoOrder(param1,param2)内容如下:

1
2
3
4
5
6
7
8
9
10
11
public static String getOrderBy(String orderByColumn, String sort) {

if ("0".equals(sort)) {
sort = "desc";
}

if ("1".equals(sort)) {
sort = "asc";
}
return orderByColumn + " " + sort;
}

合在一起完整代码如下:

1
2
3
PageHelper.startPage(reqDTO.getCurPage(), reqDTO.getPageSize(), PageUtil.getOrderBy(reqDTO.getOrderColumn(), reqDTO.getSort()));

BasePageVo<T> pageInfo = new BasePageVo(userMapper.selectUserList(companyCode);

BasePageVo.java:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@Data
@NoArgsConstructor
public class BasePageVo<T> {

private List<T> pageList;
private int curPage;
private int pageSize;
private long total;
private int pages;

public BasePageVo(List<T> list) {

if (list instanceof Page) {
Page page = (Page) list;
this.curPage = page.getPageNum();
this.pageSize = page.getPageSize();
this.pages = page.getPages();
this.pageList = page;
this.total = page.getTotal();
} else if (list instanceof Collection) {

this.curPage = 1;
this.pageSize = 10;
this.pages = 1;
this.pageList = list;
this.total = (long) list.size();
}


}
}

文章目录