前言
因为写课设要用到分页功能于是就学着去写了一下。
相比之下我的项目实现能力确实不够,而且代码习惯不好总是丢三落四,一定要好好总结反思!
相关资料
集成方法
大概是这样。
这肯定直接Maven啊。
pom.xml
代码
<!-- 分页插件 -->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.3.0</version>
</dependency>
详情
page对象
private int pageNum; //当前页码
private int pageSize; //每页数据的数量
private int startRow; //始页首行行号
private int endRow; //尾页尾行行号
private long total; //总记录数
private int pages; //总页数
private Boolean reasonable; //分页合理化
private Boolean pageSizeZero; //当设置为true的时候,如果pagesize设置为0(或RowBounds的limit=0),就不执行分页,返回全部结果
PageInfo对象
private int pageNum;
//当前页
private int pageSize;
//每页显示数据条数
private int size;
//当前页的数量
private int startRow;
//始页首行行号
private int endRow;
//尾页尾行行号
private long total;
//总记录数
private int pages;
//总页数
private List<T> list;
//查询结果的数据
private int firstPage;
//首页
private int prePage;
//上一页
private int nextPage;
// 下一页
private int lastPage;
//最后一页
private boolean isFirstPage;
//是不是第一页
private boolean isLastPage;
//是不是最后一页
private boolean hasPreviousPage;
//有没有上一页
private boolean hasNextPage;
//有没有下一页
private int navigatePages;
//所有导航页号
private int[] navigatepageNums;
//导航页码数
实现代码:
controller层
@PostMapping("/pageList/{start}/{limit}")
private ResponseBean<PageInfo<PolicyDto>> selectPolicyPage(@RequestBody PolicySelectDto policySelectDto, @PathVariable("start") Integer start, @PathVariable("limit") Integer limit) {
PageInfo<PolicyDto> result = policyService.selectPolicyPage(policySelectDto,start,limit);
return new ResponseBean<>(result);
}
-
PolicySelectDto policySelectDto,
用前端传过来的类就要用@RequestBody
转化 -
@PathVariable(“start”) Integer start(==pagenum)
-
@PathVariable(“limit”) Integer limit(==pageSize)
@RequestParam
和 @PathVariable
注解是用于从request
中接收请求的,两个都可以接收参数,关键点不同的是@RequestParam
是从request
里面拿取值,而 @PathVariable
是从一个URI模板里面来填充。
@RequestParam 支持下面四种参数
- defaultValue
如果本次请求没有携带这个参数,或者参数为空,那么就会启用默认值 - name
绑定本次参数的名称,要跟URL上面的一样 - required
这个参数是不是必须的 - value
跟name一样的作用,是name属性的一个别名
@RequestParam和@PathVariable都能够完成类似的功能——因为本质上,它们都是用户的输入,只不过输入的部分不同,一个在URL路径部分,另一个在参数部分。要访问一篇博客文章,这两种URL设计都是可以的:
通过@PathVariable,例如/blogs/1
通过@RequestParam,例如blogs?blogId=1
那么究竟应该选择哪一种呢?建议:
1、当URL指向的是某一具体业务资源(或资源列表),例如博客,用户时,使用@PathVariable
2、当URL需要对资源或者资源列表进行过滤,筛选时,用@RequestParam
Service层
public PageInfo<PolicyDto> selectPolicyPage(PolicySelectDto policySelectDto,Integer start,Integer limit) {
PageHelper.startPage(start, limit);
Page<PolicyDto> tmp = policyMapper.selectPolicyPage(policySelectDto);
//进行下类型转换
PageInfo<PolicyDto> result=new PageInfo<>(tmp);
return result;
}
PageHelper.startPage(start, limit);
关键代码也就这个。
Mapper层
Page<PolicyDto> selectPolicyPage(PolicySelectDto policySelectDto);
结束
无了。
我要好好学打代码。
这周得把课设全整了!
加油!