R
R
Rocket-API
Search…
自定义分页构造器
分页涉及参数:
  1. 1.
    pageNo 当前页
  2. 2.
    pageSize 每页大小
  3. 3.
    index 查询起始位置,Rocket-APi自动通过pageNo,pageSize计算得来,不需要手动定义

自动分页

1
db.pager("select * from user")
Copied!

手动分页

1
countSql = """
2
select count(1) from user
3
"""
4
5
querySql = """
6
select * from user limit #{index},#{pageSize}
7
"""
8
9
total = db.count(countSql);
10
list = db.find(querySql);
11
return Pager.build(total,list);
Copied!
自定义分页构建,实现接口com.github.alenfive.rocketapi.extend.IApiPager
1
/**
2
* 默认分页数据构建器
3
*/
4
@Component
5
public class DefaultApiPager implements IApiPager {
6
7
@Autowired
8
private UtilsFunction utilsFunction;
9
10
@Override
11
public Object buildPager(Long totalRecords, List data, ApiInfo apiInfo, ApiParams apiParams) {
12
Map<String,Object> pager = new HashMap<>();
13
Integer pageSize = Integer.valueOf(utilsFunction.val(this.getPageSizeVarName()).toString());
14
Integer pageNo = Integer.valueOf(utilsFunction.val(this.getPageNoVarName()).toString());
15
Integer index = Integer.valueOf(utilsFunction.val(this.getIndexVarName()).toString());
16
17
pager.put("totalRecords",totalRecords);
18
pager.put("totalPages",Integer.valueOf((int) ((totalRecords + pageSize - 1) / pageSize)));
19
pager.put("data",data);
20
pager.put(this.getPageNoVarName(),pageNo);
21
pager.put(this.getPageSizeVarName(),pageSize);
22
pager.put(this.getIndexVarName(),index);
23
return pager;
24
}
25
26
@Override
27
public String getPageSizeVarName() {
28
return "pageSize";
29
}
30
31
@Override
32
public String getPageNoVarName() {
33
return "pageNo";
34
}
35
36
@Override
37
public String getIndexVarName() {
38
return "index";
39
}
40
41
@Override
42
public Integer getIndexVarValue(Integer pageSize,Integer pageNo) {
43
return (pageNo-1)*pageSize;
44
}
45
46
@Override
47
public Integer getPageSizeDefaultValue() {
48
return 15;
49
}
50
51
@Override
52
public Integer getPageNoDefaultValue() {
53
return 1;
54
}
55
}
56
Copied!
Last modified 10mo ago
Copy link
Contents